Skip to content
View prashikdewtale10's full-sized avatar
🏠
Working from home
🏠
Working from home

Block or report prashikdewtale10

Block user

Prevent this user from interacting with your repositories and sending you notifications. Learn more about blocking users.

You must be logged in to block users.

Please don't include any personal information such as legal names or email addresses. Maximum 100 characters, markdown supported. This note will be visible to only you.
Report abuse

Contact GitHub support about this user’s behavior. Learn more about reporting abuse.

Report abuse
prashikdewtale10/README.md

Here's an example of how you can create a rate limit middleware in Esmerald:

from esmerald import Middleware
from esmerald.requests import Request
from esmerald.responses import Response
from starlette.datastructures import Headers
from typing import Callable
from functools import wraps
from collections import defaultdict

class RateLimitMiddleware(Middleware):
    def __init__(self, max_calls: int, time_window: int):
        self.max_calls = max_calls
        self.time_window = time_window
        self.cache = defaultdict(int)

    async def __call__(self, request: Request, next: Callable[[Request], Response]) -> Response:
        ip = request.client.host
        current_time = int(request.timestamp)
        if current_time - self.cache[ip] >= self.time_window:
            self.cache[ip] = current_time
            self.cache[(ip, 'count')] = 1
        elif self.cache[(ip, 'count')] < self.max_calls:
            self.cache[(ip, 'count')] += 1
        else:
            headers = Headers({"Retry-After": str(self.time_window)})
            return Response(429, headers=headers)

        return await next(request)

def rate_limit(max_calls: int, time_window: int):
    def decorator(func):
        @wraps(func)
        async def wrapped(*args, **kwargs):
            return await func(*args, **kwargs)
        return wrapped
    return decorator

This middleware uses a simple in-memory cache to store the IP addresses and their corresponding request counts. You can customize the max_calls and time_window parameters to suit your needs.

To use this middleware in your Esmerald API, you can add it to your application like this:

from esmerald import Esmerald
from my_rate_limit_middleware import RateLimitMiddleware

app = Esmerald(
    ...
    middleware=[RateLimitMiddleware(max_calls=10, time_window=60)],
    ...
)

This will apply the rate limit middleware to all routes in your application. If you want to apply it to specific routes only, you can use the rate_limit decorator:

from esmerald import Route
from my_rate_limit_middleware import rate_limit

@rate_limit(max_calls=5, time_window=30)
@app.route("/")
async def my_route():
    ...

This will apply the rate limit to the specific route only.

Pinned Loading

  1. Github-Jobs-App-using_reactjs Github-Jobs-App-using_reactjs Public

    This is uses Github's jobs api to provided day to day updated jobs list with actual location description.

    JavaScript

  2. Github_userSearch_Reactjs Github_userSearch_Reactjs Public

    This Project Uses Github's User's and Repository API to get Data and implement it into React Js

    JavaScript 1

  3. Prashik_Links_Launcher Prashik_Links_Launcher Public

    This is the Basic Chrome Extension which will help to get instant access to social media links.

    HTML 1

  4. Recepies_app_using_Reactjs Recepies_app_using_Reactjs Public

    JavaScript

  5. Website_Screenshot_app Website_Screenshot_app Public

    This project uses screenshot api to work this we need input name of website of which we want to capture along with width and height

    HTML 1 1

  6. COWIN-Nodejs_CLI_App COWIN-Nodejs_CLI_App Public

    this app is based on various modules of nodejs . which can be use to check covid vaccination center in entire India by state wise , district wise and available slots for vaccination

    JavaScript 1