Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@alecavallo
Copy link

@alecavallo alecavallo commented Oct 1, 2025

Fixes #214

Problem

When a GitHub repository is moved/renamed, the GitHub API returns a 3xx redirect response with a Location header pointing to the new repository's API URL. However, the RedirectionException raised by gidgethub doesn't provide access to the response headers, making it impossible for users to programmatically follow redirects.

Solution

  • Modified HTTPException base class to accept an optional headers parameter
  • Updated decipher_response() function in sansio.py to pass headers to exceptions
  • Added Mapping import to support the new headers parameter type

Changes

  • gidgethub/__init__.py: Added headers parameter to HTTPException.__init__() and imported Mapping from typing
  • gidgethub/sansio.py: Modified decipher_response() to pass headers when raising exceptions

Usage

Users can now access redirect information from RedirectionException:

try:
    # GitHub API call that might redirect
    pass
except RedirectionException as e:
    location = e.headers.get('location')
    if location:
        # Follow the redirect to the new repository location
        pass

Testing

  • All existing tests continue to pass
  • New functionality maintains backward compatibility
  • Headers are stored as empty dict when not provided

This enhancement enables proper handling of repository moves/renames in GitHub API clients built with gidgethub.

…update response handling in decipher_response to pass headers. Update tests to verify header propagation.
…dundant header assignment in decipher_response
self,
status_code: http.HTTPStatus,
*args: Any,
headers: Optional[Mapping[str, str | None]] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

The type for the headers doesn't line up with what decipher_response(), but they probably should.

Suggested change
headers: Optional[Mapping[str, str | None]] = None,
headers: Optional[Mapping[str, str]] = None,

Probably a better solution, though, would be a TypeVar that is used in both places.

@brettcannon
Copy link
Collaborator

Apologies for the review delay, @alecavallo ! Having a toddler does not shrink the review queue. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The RedirectionException doesn't provide the location header

3 participants