Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix view's request handling #2

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Views Example
@routes.view("/")
class Home(View):
@limiter.limit("1/second")
def get(self: web.Request):
def get(self):
return web.Response(text="hello")
```

Expand Down
12 changes: 7 additions & 5 deletions aiohttplimiter/limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ def __init__(self, db: Storage, path_id: str, keyfunc: Callable,

def __call__(self, func: Union[Callable, Awaitable]) -> Callable[[Union[Request, View]], Coroutine[Any, Any, Response]]:
@wraps(func)
async def wrapper(request: Union[Request, View]) -> Response:
if isinstance(request, View):
request = request.request
async def wrapper(ctx: Union[Request, View]) -> Response:
if isinstance(ctx, View):
request = ctx.request
else:
request = ctx
key = self.keyfunc(request)
db_key = f"{key}:{self.path_id or request.path}"

Expand Down Expand Up @@ -94,8 +96,8 @@ async def wrapper(request: Union[Request, View]) -> Response:
await self.moving_window.hit(self.item, db_key)
# Returns normal response if the user did not go over the rate limit
if asyncio.iscoroutinefunction(func):
return await func(request)
return await func(ctx)
else:
return func(request)
return func(ctx)

return wrapper