Rate-limiting for django
Project description
django-ratelimiter
TBD
Synopsis
from django.http import HttpRequest, HttpResponse
from django_ratelimiter import ratelimit
from limits.storage import RedisStorage
# defaults, limit all requests
@ratelimit("5/minute")
def view(request: HttpRequest) -> HttpResponse:
return HttpResponse("OK")
# limit per-user (request attribute key)
@ratelimit("5/minute", key="user")
def view(request: HttpRequest) -> HttpResponse:
return HttpResponse("OK")
# custom key function
@ratelimit("5/minute", key=lambda r: r.user.username)
def view(request: HttpRequest) -> HttpResponse:
return HttpResponse("OK")
# limit only specific methods
@ratelimit("5/minute", methods=["POST", "PUT"])
def view(request: HttpRequest) -> HttpResponse:
return HttpResponse("OK")
# custom response
@ratelimit("5/minute", response=HttpResponse("Too many requests", status=400))
def view(request: HttpRequest) -> HttpResponse:
return HttpResponse("OK")
# Using `limits.storage` storages
@ratelimit("5/minute", storage=RedisStorage(uri="redis://localhost:6379/0"))
def view(request: HttpRequest) -> HttpResponse:
return HttpResponse("OK")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django_ratelimiter-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56b17830d9ab7969228e8d59b3c24dbfde64b0542b870cb875146e8d2dcd5b03 |
|
MD5 | dd60b2831813f798687f9e1d8f7c542e |
|
BLAKE2b-256 | 9c0019be1d2864f79fefadbb412d17c0d4c56008c12782105d93c4fffdbf9ec3 |