mirror of
https://github.com/laurentS/slowapi.git
synced 2026-03-13 09:10:20 +08:00
Sending the request
This commit is contained in:
@@ -482,7 +482,7 @@ class Limiter:
|
||||
limit_for_header = None
|
||||
for lim in limits:
|
||||
limit_scope = lim.scope or endpoint
|
||||
if lim.is_exempt:
|
||||
if lim.is_exempt(request):
|
||||
continue
|
||||
if lim.methods is not None and request.method.lower() not in lim.methods:
|
||||
continue
|
||||
@@ -699,11 +699,9 @@ class Limiter:
|
||||
else:
|
||||
self._route_limits.setdefault(name, []).extend(static_limits)
|
||||
|
||||
connection_type: Optional[str] = None
|
||||
sig = inspect.signature(func)
|
||||
for idx, parameter in enumerate(sig.parameters.values()):
|
||||
if parameter.name == "request" or parameter.name == "websocket":
|
||||
connection_type = parameter.name
|
||||
break
|
||||
else:
|
||||
raise Exception(
|
||||
|
||||
@@ -2,6 +2,7 @@ import inspect
|
||||
from typing import Callable, Iterator, List, Optional, Union
|
||||
|
||||
from limits import RateLimitItem, parse_many # type: ignore
|
||||
from starlette.requests import Request
|
||||
|
||||
|
||||
class Limit(object):
|
||||
@@ -31,13 +32,18 @@ class Limit(object):
|
||||
self.cost = cost
|
||||
self.override_defaults = override_defaults
|
||||
|
||||
@property
|
||||
def is_exempt(self) -> bool:
|
||||
def is_exempt(self, request: Request) -> bool:
|
||||
"""
|
||||
Check if the limit is exempt.
|
||||
Return True to exempt the route from the limit.
|
||||
"""
|
||||
return self.exempt_when() if self.exempt_when is not None else False
|
||||
if self.exempt_when is None:
|
||||
return False
|
||||
params = inspect.signature(self.exempt_when).parameters
|
||||
param_len = len(params)
|
||||
if param_len == 1:
|
||||
return self.exempt_when(request)
|
||||
return self.exempt_when()
|
||||
|
||||
@property
|
||||
def scope(self) -> str:
|
||||
|
||||
Reference in New Issue
Block a user