Skip to content

Allow to set GitHub API version #1132

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

Merged
merged 2 commits into from
Mar 2, 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
16 changes: 14 additions & 2 deletions src/github3/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,21 @@ class GitHub(models.GitHubCore):
call the GitHub object with authentication parameters.
"""

def __init__(self, username="", password="", token="", session=None):
"""Create a new GitHub instance to talk to the API."""
def __init__(
self, username="", password="", token="", session=None, api_version=""
):
"""Create a new GitHub instance to talk to the API.

:param str api_version:
API version to send with X-GitHub-Api-Version header.
See https://docs.github.com/en/rest/overview/api-versions
for details about API versions.
"""
super().__init__({}, session or self.new_session())

if api_version:
self.session.headers.update({"X-GitHub-Api-Version": api_version})

if token:
self.login(username, token=token)
elif username and password:
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,14 @@ def test_search_users(self):
headers={},
)

def test_api_version_header(_):
gh = GitHub(api_version="2022-11-28")
assert gh.session.headers.get("X-GitHub-Api-Version") == "2022-11-28"

def test_api_version_header_not_defined(_):
gh = GitHub()
assert gh.session.headers.get("X-GitHub-Api-Version") is None


class TestGitHubRequiresAuthentication(
helper.UnitRequiresAuthenticationHelper
Expand Down