slowapi icon indicating copy to clipboard operation
slowapi copied to clipboard

feat: allow Requests to be sent to exempt_when

Open colin99d opened this issue 1 year ago • 19 comments

Creates a way to use the request object in the exempt_when function.

Now allows functions like this to be used:

def test_exempt(x) -> bool:
    print(x.headers.get("User-Agent"))
    return False

Fixes #155

Lines 702 and 706 were automatically removed by my linter because they are unused. Let me know if you want them back. Also, let me know if you want ruff added to CI for code linting.

colin99d avatar Apr 11 '23 15:04 colin99d

+1

Today we return None as part of the key_func function in order to bypass the limit, instead of using the exempt_when function. Then we get the error kipping limit: %s. Empty value found in parameters..

This PR would enable us to use exempt_when and make the code cleaner.

davidstrouk avatar Apr 30 '23 13:04 davidstrouk

@laurentS anything else you want to see here for this to be mergeable?

colin99d avatar May 23 '23 18:05 colin99d

@laurentS anything else you want to see here for this to be mergeable?

@colin99d thanks for submitting this PR! I just took a quick look, but I saw the PR changes the arguments to a method that's probably public, which means it would probably break some users' code (at least their typechecks would complain). Can the extra arg be made optional to avoid that?

laurentS avatar May 24 '23 14:05 laurentS

@laurentS im so sorry I just sorry your comment, changes added!!!

colin99d avatar Jul 12 '23 14:07 colin99d

@laurentS anything else you want to see before this is merged?

colin99d avatar Aug 21 '23 14:08 colin99d

@laurentS anything else you want to see before this is merged?

@colin99d I merged @Rested 's suggestions, can you just check that tests pass, etc..., and I think we're good to go then.

laurentS avatar Aug 21 '23 15:08 laurentS

@laurentS it looks like the change by @Rested causes all the tests to fail. Any idea how to fix it?

colin99d avatar Aug 21 '23 18:08 colin99d

Also, I got a little carried away and switch from Flake8 to Ruff. Its pretty much Flake8 but way better in every way.

colin99d avatar Aug 21 '23 18:08 colin99d

Maybe we should try to do one thing per PR. Let's focus this one on the feature you initially proposed, and we can add ruff in a separate one. I won't have time to look into this for the next few days, but it looks like there's a bug in the github ci file preventing the checks from running, no?

laurentS avatar Aug 22 '23 07:08 laurentS

I reverted ruff changes. All tests fail because of this line: self._exempt_when_takes_request = len(inspect.signature(self.exempt_when).parameters) == 1

colin99d avatar Aug 22 '23 12:08 colin99d

I reverted ruff changes. All tests fail because of this line: self._exempt_when_takes_request = len(inspect.signature(self.exempt_when).parameters) == 1

It just needs reformatting. Can you run black on your code? I can't commit to your branch, so you'll have to do it.

It should probably look like this:

        self._exempt_when_takes_request = (
            len(inspect.signature(self.exempt_when).parameters) == 1
        )

laurentS avatar Aug 22 '23 15:08 laurentS

Not related to this PR but maybe we could implement pre-commit on the repo, so that anyone wanting to contribute would just need to do one pre-commit install when first cloning their repo to have all code formatting applied automatically when they commit

thentgesMindee avatar Aug 22 '23 16:08 thentgesMindee

@laurentS changes in, can we merge now?

colin99d avatar Oct 05 '23 12:10 colin99d

@laurentS are we waiting on anything else here?

colin99d avatar Jan 05 '24 12:01 colin99d

@laurentS are we waiting on anything else here?

I think CI checks should be green before merging :) Logs seem to have been removed by github, but I think there was something pretty clear in there as to what needs fixing.

laurentS avatar Jan 05 '24 13:01 laurentS

Can you please rerun them so I can see what it is?

colin99d avatar Jan 05 '24 14:01 colin99d

Can you please rerun them so I can see what it is?

I can't seem to rerun CI on github, weird. I pulled your changes locally, try poetry run mypy . and you'll see it is not happy about an unhandled case (when exempt_when is None) in wrappers.py:33. And in general, you should be able to trigger a CI run by pushing an empty commit (or just doing some cosmetic change in the code).

laurentS avatar Jan 05 '24 14:01 laurentS

@laurentS done!

colin99d avatar Jan 05 '24 17:01 colin99d

Ok, where would you like the note added?

colin99d avatar Mar 08 '24 14:03 colin99d

@colin99d I would assume it should be added to the 'CHANGELOG.md'.

But mostly when doing the next release, so feels like this could merged @laurentS / @Rested ?

ecly avatar May 16 '24 11:05 ecly

@laurentS yeah just let me know exactly where you want this note added so we can get this merged.

colin99d avatar May 31 '24 17:05 colin99d

Yes, the changelog is a good place for that. If you make sure to include "breaking change" in the wording, it'll probably help users as well. Thanks for your contribution!

laurentS avatar Jun 04 '24 19:06 laurentS

@laurentS done!

colin99d avatar Jun 04 '24 19:06 colin99d

Looks great! think adding some usage docs here https://github.com/laurentS/slowapi/blob/6547679463b6a6fa87d9e254a8558bbc6075e263/slowapi/extension.py#L807 and https://github.com/laurentS/slowapi/blob/master/docs/examples.md and a small test around here https://github.com/laurentS/slowapi/blob/6547679463b6a6fa87d9e254a8558bbc6075e263/tests/test_starlette_extension.py#L195 would be ideal before merging!

Rested avatar Jun 04 '24 22:06 Rested

@laurentS @Rested improved the docstring and added a comprehensive set of tests, can we merge this now?

colin99d avatar Jun 27 '24 16:06 colin99d

@laurentS any plans to publish a new version to Pypi with this change (and a few others that have landed since the v0.1.9 release from February)?

ecly avatar Aug 15 '24 09:08 ecly