Conditions | 4 |
Total Lines | 24 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import functools |
||
48 | def rate_limit(limit, per, scope_func=lambda: request.remote_addr): |
||
49 | def decorator(f): |
||
1 ignored issue
–
show
|
|||
50 | @functools.wraps(f) |
||
1 ignored issue
–
show
|
|||
51 | def wrapped(*args, **kwargs): |
||
1 ignored issue
–
show
|
|||
52 | if current_app.config['USE_RATE_LIMITS']: |
||
1 ignored issue
–
show
|
|||
53 | key = 'rate-limit/%s/%s/' % (f.__name__, scope_func()) |
||
1 ignored issue
–
show
|
|||
54 | limiter = RateLimit(key, limit, per) |
||
1 ignored issue
–
show
|
|||
55 | if not limiter.over_limit: |
||
1 ignored issue
–
show
|
|||
56 | rv = f(*args, **kwargs) |
||
1 ignored issue
–
show
|
|||
57 | else: |
||
1 ignored issue
–
show
|
|||
58 | rv = too_many_requests('You have exceeded your request rate') |
||
1 ignored issue
–
show
|
|||
59 | # rv = make_response(rv) |
||
60 | g.headers = { |
||
1 ignored issue
–
show
|
|||
61 | 'X-RateLimit-Remaining': str(limiter.remaining), |
||
62 | 'X-RateLimit-Limit': str(limiter.limit), |
||
63 | 'X-RateLimit-Reset': str(limiter.reset) |
||
64 | } |
||
65 | return rv |
||
1 ignored issue
–
show
|
|||
66 | else: |
||
1 ignored issue
–
show
|
|||
67 | return f(*args, **kwargs) |
||
1 ignored issue
–
show
|
|||
68 | |||
69 | return wrapped |
||
1 ignored issue
–
show
|
|||
70 | |||
71 | return decorator |
||
1 ignored issue
–
show
|
|||
72 | |||
170 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.