API Reference
django_ratelimiter.decorator
ratelimit
ratelimit(
rate: Union[str, Callable[[HttpRequest], str]],
key: Union[str, Callable[[HttpRequest], str], None] = None,
methods: Union[str, Sequence[str], None] = None,
strategy: Literal[
"fixed-window", "fixed-window-elastic-expiry", "moving-window"
] = "fixed-window",
response: Optional[HttpResponse] = None,
storage: Optional[Storage] = None,
cache: Optional[str] = None,
) -> Callable[[ViewFunc], ViewFunc]
Rate limiting decorator for wrapping views.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rate |
Union[str, Callable[[HttpRequest], str]]
|
rate string (i.e. |
required |
key |
Union[str, Callable[[HttpRequest], str], None]
|
request attribute or callable that returns a string to be used as identifier |
None
|
methods |
Union[str, Sequence[str], None]
|
only rate limit specified method(s) |
None
|
strategy |
Literal['fixed-window', 'fixed-window-elastic-expiry', 'moving-window']
|
a name of rate limiting strategy |
'fixed-window'
|
response |
Optional[HttpResponse]
|
custom rate limit response instance |
None
|
storage |
Optional[Storage]
|
override default rate limit storage |
None
|
cache |
Optional[str]
|
override default cache name if using django cache storage backend |
None
|
Source code in django_ratelimiter/decorator.py
django_ratelimiter.middleware
AbstractRateLimiterMiddleware
Bases: ABC
Abstract base class for rate limiting middleware.
Attributes:
Name | Type | Description |
---|---|---|
STRATEGY |
str
|
default rate limiter strategy. Defaults to |
Source code in django_ratelimiter/middleware.py
keys_for
By default, this will use middleware name for all requests, effectively this means global rate limiting for all requests.
Override this method to rate-limit based on a request attribute like a path, user, etc.
Source code in django_ratelimiter/middleware.py
rate_for
abstractmethod
Returns a rate for given request.
If None
is returned, request is not rate-limited.
ratelimit_response
Override to return a custom response when rate limit is exceeded.
storage_for
strategy_for
django_ratelimiter.storage
CacheStorage
Bases: Storage
Rate limiting storage with django cache backend.
Source code in django_ratelimiter/storage.py
django_ratelimiter.utils
build_identifiers
build_identifiers(func: ViewFunc, methods: Union[str, Sequence[str], None] = None) -> list[str]
Build view identifiers for storage cache key using function signature and list of methods.
Source code in django_ratelimiter/utils.py
get_rate_limiter
Return a ratelimiter instance for given strategy.
Source code in django_ratelimiter/utils.py
get_storage
cached
Returns a default storage backend instance, defined by either DJANGO_RATELIMITER_CACHE
or DJANGO_RATELIMITER_STORAGE
.
Source code in django_ratelimiter/utils.py
django_ratelimiter.types.ViewFunc
module-attribute
ViewFunc = Callable[Concatenate[HttpRequest, P], HttpResponse]