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

Handle GitHub API rate limits #3366

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

martinhoyer
Copy link
Collaborator

As per discussion on chat. Trying to handle rate limits for GitHub api.
https://stackoverflow.com/questions/66522261/does-github-rate-limit-access-to-public-raw-files

fail: Failed to fetch remote playbook 'https://raw.githubusercontent.com/teemtee/tmt/main/tests/prepare/ansible/data/playbook.yml'.
403 Client Error: Forbidden for url: https://raw.githubusercontent.com/teemtee/tmt/22a46a4a6760517e3eadbbff0c9bebdb95442760/tests/core/env/data/vars.yaml

Pull Request Checklist

  • implement the feature
  • extend the test coverage

@martinhoyer martinhoyer added the code | utils Various utility functions and classes used across the code label Nov 18, 2024
@martinhoyer martinhoyer self-assigned this Nov 18, 2024
@@ -4358,6 +4358,33 @@ def increment(

raise GeneralError(message) from error

# Handle GitHub-specific responses
# https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#exceeding-the-rate-limit
response = kwargs.get('response')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to use cast() to introduce some types? See error above, it should be possible to do something like response = cast(Optional[requests.Response], kwargs.get('response')). The same applies to headers below, headers: dict[str, str] should help linters & limit the amount of Any running around.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe headers we would get for free, response.headers should exist all the time, even if it'd be empty, i.e. no need for getattr?

Copy link
Collaborator Author

@martinhoyer martinhoyer Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@happz Thanks, adding cast already found an issue :)
I'm not sure about the headers, as that already has CaseInsensitiveDict[str] type.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, once linters become aware of the response type, I suppose they would get a better view of its attributes, and we wouldn't need to bother with cast calls.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, they are already aware? it was pre-commit mypy that caught that when I tried to do dict[str]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect kwargs.get('response') returns Any, because of **kwargs: Any. As soon as cast(requests.Response, ...) is applied, mypy should be able to infer the rest, but without this hint, it'd all be just Any.

@psss psss added the ci | full test Pull request is ready for the full test execution label Nov 18, 2024
@psss
Copy link
Collaborator

psss commented Nov 18, 2024

/packit test

Copy link
Collaborator

@psss psss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@psss psss added this to the 1.39 milestone Nov 18, 2024
@@ -4358,6 +4358,30 @@ def increment(

raise GeneralError(message) from error

# Handle GitHub-specific responses
# https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#exceeding-the-rate-limit
response = cast(requests.Response, kwargs.get('response'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be Optional, get() may return None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci | full test Pull request is ready for the full test execution code | utils Various utility functions and classes used across the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants