Skip to content

Commit

Permalink
Refactor args_for -> scope_for in wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Dec 29, 2022
1 parent 82fdc55 commit 0da026b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
11 changes: 7 additions & 4 deletions flask_limiter/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def render_strategy(strategy: RateLimiter) -> str:
def render_limit_state(
limiter: Limiter, endpoint: str, limit: Limit, key: str, method: str
) -> str:
args = limit.args_for(endpoint, key, method)
args = [key, limit.scope_for(endpoint, method)]
if not limiter.storage or (limiter.storage and not limiter.storage.check()):
return ": [error]Storage not available[/error]"
test = limiter.limiter.test(limit.limit, *args)
Expand Down Expand Up @@ -530,7 +530,8 @@ def clear(
for limit in application_limits:
limiter.limiter.clear(
limit.limit,
*limit.args_for("", key, method),
key,
limit.scope_for("", method),
)
node.add(f"{render_limit(limit)}: [success]Cleared[/success]")
console.print(node)
Expand All @@ -547,12 +548,14 @@ def clear(
for rule_method in details["rule"].methods:
limiter.limiter.clear(
limit.limit,
*limit.args_for(endpoint, key, rule_method),
key,
limit.scope_for(endpoint, rule_method),
)
else:
limiter.limiter.clear(
limit.limit,
*limit.args_for(endpoint, key, method),
key,
limit.scope_for(endpoint, method),
)
node.add(
f"{render_limit(limit)}: [success]Cleared[/success]"
Expand Down
7 changes: 1 addition & 6 deletions flask_limiter/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,15 +926,10 @@ def __evaluate_limits(self, endpoint: str, limits: List[Limit]) -> None:
limit_for_header: Optional[RequestLimit] = None
view_limits: List[RequestLimit] = []
for lim in sorted(limits, key=lambda x: x.limit):
if not lim.shared and lim.scope:
limit_scope = f"{endpoint}:{lim.scope}"
else:
limit_scope = lim.scope or endpoint
if lim.is_exempt or lim.method_exempt:
continue

if lim.per_method:
limit_scope += f":{flask.request.method}"
limit_scope = lim.scope_for(endpoint, flask.request.method)
limit_key = lim.key_func()
args = [limit_key, limit_scope]
kwargs = {}
Expand Down
21 changes: 13 additions & 8 deletions flask_limiter/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,25 @@ def method_exempt(self) -> bool:

return self.methods is not None and request.method.lower() not in self.methods

def args_for(self, endpoint: str, key: str, method: Optional[str]) -> List[str]:
if self.scope:
if not self.shared:
scope = self.scope
def scope_for(self, endpoint: str, method: Optional[str]) -> str:
"""
Derive final bucket (scope) for this limit given the endpoint
and request method. If the limit is shared between multiple
routes, the scope does not include the endpoint.
"""
limit_scope = self.scope
if limit_scope:
if self.shared:
scope = limit_scope
else:
scope = f"{endpoint}:{self.scope}"
scope = f"{endpoint}:{limit_scope}"
else:
scope = self.scope or endpoint
scope = endpoint

if self.per_method:
assert method
scope += f":{method.upper()}"
args = [key, scope]
return args
return scope


@dataclasses.dataclass(eq=True, unsafe_hash=True)
Expand Down

0 comments on commit 0da026b

Please sign in to comment.