Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ $ pip install redis-ratelimit
The library itself is framework agnostic, but let's assume you want to use it with [Flask](http://flask.pocoo.org/docs/0.12/):

```python
from http import HTTPStatus # python 3.4 +
from flask import Flask, jsonify
from flask import make_response
from redis_ratelimit import ratelimit
from redis_ratelimit.exceptions import RateLimited

app = Flask(__name__)

@app.route('/')
@ratelimit(rate='10/m', key='ccc')
@ratelimit(rate='2/s', key='ccc')
def index():
return jsonify({'status': 'OK'})

def over_limit(e):
code = HTTPStatus.TOO_MANY_REQUESTS # 429
body = jsonify({"message":str(e)})
return make_response(body, code.value)

app.register_error_handler(RateLimited, over) # flask error handler

if __name__ == '__main__':
app.run()
```

This will allow a total of 10 requests in any given minute, but no faster than 2 requests a second.
Expand Down