Skip to content

achalpathak/django-api-ratelimiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Version Requirements

Description

Django API RateLimiter is a rate-limiting decorator for Django APIs using Redis as a caching layer to speed up the checking and updating of API hits made by a user

Configure Settings

# Configure Redis Cache Backend
REDIS_SETTINGS = {
    "REDIS_HOST": "localhost",
    "REDIS_PORT": "6379",
    "REDIS_DB": "1",
    "REDIS_PASSWORD": "",
}

# Configure enable/disable rate limiting
RATE_LIMITING_ENABLED = True 

# Configure rate-limiting error message
RATE_LIMITING_MSG = "Request was throttled. Too many requests. Please wait."

Usage

Example 1

from rate_limiter.decorators import rate_limiter

@rate_limiter(key="ip", rate="5/m")
def myview(request):
    # Allows 5 requests per minute per IP.
    pass

Example 2

from rate_limiter.decorators import rate_limiter

@rate_limiter(key="header:X-Client-Code", rate="35/s")
def myview(request):
    # Allows 35 requests per second per X-Client-Code header.
    pass

Keys and Rate Configurations

KEYS

'ip' - Use the request IP address (i.e. request.META['REMOTE_ADDR'])
'header:X-X' - Use the value of request.META.get('HTTP_X_X', '')

RATE

's' - second
'm' - minute
'h' - hour
'd' - day
Example
'100/s' - Limits requests to 100 per second
'45/h' - Limits requests to 45 per hour

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages