Giter Site home page Giter Site logo

byunjuneseok / fastapi-frl Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 1.0 574 KB

⚡️⏲ Rate limiter library for FastAPI. (Work in progress..)

Home Page: https://byunjuneseok.github.io/fastapi-frl/

License: Apache License 2.0

Python 100.00%
python3 fastapi rate-limiting rate-limiter redis aioredis

fastapi-frl's Introduction

fastapi-frl

pypi versions

Rate limiter library for FastAPI.

diagram

Prerequisite

  • Redis server

Installation

pip install fastapi-frl

Getting Started

from fastapi import APIRouter, Depends, FastAPI

from fastapi_frl import LimiterBackend, Limiter
from fastapi_frl.algorithms import SimpleAlgorithm, SlidingWindowCounter
from fastapi_frl.key import KeyByIP, KeyByPath

app = FastAPI()

# You only need to initialize backend of limiter once.
limiter_backend = LimiterBackend('redis://localhost')

# Define the limiter.
limiter = Limiter(
    name='hello_world_limiter',
    algorithm=SimpleAlgorithm(threshold=10),
    key_generator=KeyByIP()
)

# Apply it to a single api endpoint.
@app.get('/', status_code=200, dependencies=[Depends(limiter)])
async def hello_world():
    return {'message': 'Hello World'}

# You can define multiple limiter with different rules.
api_book_limiter = Limiter(
    name='api_book_limiter',
    algorithm=SlidingWindowCounter(10, 10),
    key_generator=KeyByPath(),
)

# Also, you can apply it to the api router.
api_book_router = APIRouter(prefix='/books', tags=['Book'], dependencies=[Depends(api_book_limiter)])

...

app.include_router(api_book_router)

Documentation

The API documentation is in progress.

fastapi-frl's People

Contributors

byunjuneseok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

djun

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.