Skip to content

Commit 9880ff7

Browse files
committed
First version
1 parent a12edf5 commit 9880ff7

13 files changed

+1872
-4
lines changed

LICENSE

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Copyright (c) 2020 Laurent Savaete
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
7+
in the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+
of the Software, and to permit persons to whom the Software is furnished to do
10+
so, subject to the following conditions:
1111

1212
The above copyright notice and this permission notice shall be included in all
1313
copies or substantial portions of the Software.

README.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# SlowApi
2+
3+
A rate limiting library for Starlette and FastAPI adapted from [flask-limiter](http://github.com/alisaifee/flask-limiter).
4+
5+
Note: this is alpha quality code still, the API may change, and things may fall apart while you try it.
6+
7+
# Quick start
8+
9+
## Starlette
10+
11+
```python
12+
from starlette.applications import Starlette
13+
from slowapi import Limiter, _rate_limit_exceeded_handler
14+
15+
limiter = Limiter(key_func=get_remote_address)
16+
app = Starlette()
17+
app.state.limiter = limiter
18+
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
19+
20+
@limiter.limit("5/minute")
21+
async def homepage(request: Request):
22+
return PlainTextResponse("test")
23+
24+
app.add_route("/home", homepage)
25+
```
26+
27+
The above app will have a route `t1` that will accept up to 5 requests per minute. Requests beyond this limit will be answered with an HTTP 429 error, and the body of the view will not run.
28+
29+
## FastAPI
30+
31+
```python
32+
from fastapi import FastAPI
33+
from slowapi import Limiter, _rate_limit_exceeded_handler
34+
35+
limiter = Limiter(key_func=get_remote_address)
36+
app = FastAPI()
37+
app.state.limiter = limiter
38+
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
39+
40+
@app.get("/home")
41+
@limiter.limit("5/minute")
42+
async def homepage(request: Request):
43+
return PlainTextResponse("test")
44+
```
45+
46+
This will provide the same result, but with a FastAPI app.
47+
48+
# Features
49+
50+
Most feature are coming from (will come from) FlaskLimiter and the underlying [limits](https://limits.readthedocs.io/).
51+
52+
Supported now:
53+
- Single and multiple `limit` decorator on endpoint functions to apply limits
54+
- redis, memcached and memory backends to track your limits (memory as a fallback)
55+
- support for sync and async HTTP endpoints
56+
- Support for shared limits across a set of routes
57+
58+
59+
# Limitations and known issues
60+
61+
* There is no support for default limits yet (in other words, the only default limit supported is "unlimited")
62+
63+
* The `request` argument must be explicitly passed to your endpoint, or `slowapi` won't be able to hook into it. In other words, write:
64+
65+
```python
66+
@limiter.limit("5/minute")
67+
async def myendpoint(request: Request)
68+
pass
69+
```
70+
71+
and not:
72+
73+
```python
74+
@limiter.limit("5/minute")
75+
async def myendpoint()
76+
pass
77+
```
78+
79+
* `websocket` endpoints are not supported yet.
80+
81+
# Developing and contributing
82+
83+
PRs are more than welcome! Please include tests for your changes :)
84+
85+
The package uses [poetry](https://python-poetry.org) to manage dependencies. To setup your dev env:
86+
87+
```bash
88+
$ poetry install
89+
```
90+
91+
To run the tests:
92+
```bash
93+
$ pytest
94+
```
95+
96+
# Credits
97+
98+
Credits go to [flask-limiter](https://github.com/alisaifee/flask-limiter) of which SlowApi is a (still partial) adaptation to Starlette and FastAPI.
99+
It's also important to mention that the actual rate limiting work is done be [limits](https://github.com/alisaifee/limits/), `slowapi` is just a wrapper around it.

0 commit comments

Comments
 (0)