diff --git a/README.md b/README.md index d77a675..cfe92d7 100644 --- a/README.md +++ b/README.md @@ -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.