-
Notifications
You must be signed in to change notification settings - Fork 79
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
add middleware and exempt decorator #7
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Rested this is great! Thanks for putting it together! I like that you made it a non-breaking change.
I've made a minor comment, and I think it will need a bit more testing (which I think we can add later) but otherwise I'm happy to merge this.
tests/test_starlette_extension.py
Outdated
return PlainTextResponse("test") | ||
|
||
with TestClient(app) as cli: | ||
resp = cli.get("/t2", headers={"X_FORWARDED_FOR": "127.0.0.11"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is testing an exempt route, the original IP should be able to GET this as well without being throttled. How would you feel about changing this IP to 127.0.0.10
instead?
Hey sorry I kind of forgot about this PR! I had a look at my code today and I seemed to be having bit of trouble getting a test to pass that I added for default and decorator limit merging 272ae50. It looks like I was referencing |
@Rested Thanks for coming back to this! Looking at your test, I think the reason it doesn't behave as you expect is in About your question on default limits, |
# Conflicts: # slowapi/extension.py merge with master conforming to new docstring style
for route in app.routes: | ||
match, _ = route.matches(request.scope) | ||
if match.FULL and hasattr(route, "endpoint"): | ||
handler = route.endpoint # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i couldn't figure out how to make mypy happy here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know either. I was hoping that breaking the if
statement above might help mypy see that your last line is fine, but no. Let's keep it like it is, I think it's ok!
combined_defaults = all( | ||
not limit.override_defaults for limit in route_limits | ||
) | ||
if ( | ||
not route_limits | ||
and not (in_middleware and name in self.__marked_for_limiting) | ||
or combined_defaults | ||
): | ||
all_limits += list(itertools.chain(*self._default_limits)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does less than the original https://github.com/alisaifee/flask-limiter/blob/55df08f14143a7e918fc033067a494248ab6b0c5/flask_limiter/extension.py#L592 but i think it's sufficient for currently supported functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can find a mistake in the logic either (It doesn't mean there isn't one 😉 ). I think it would be worth spending some time commenting this bit of code, as I find it pretty obscure, with all these xxxx_limits
variables. And we should add some more tests, but I'm happy to merge this at this point, as the middleware is pretty cool functionality.
Is that ok with you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good to me :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think one useful thing to do might be to implement some more of the flask-limiter tests with @pytest.mark.xfail
so it's more obvious how much of the api is implemented and how far from feature parity we are
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have a go at that when I find some time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added #17 off the back of this
just a sketch - addresses #1
idea is that it doesn't change the existing usage
but builds on it with
this means it wouldn't be a breaking change