Skip to content

Commit ce43e6e

Browse files
authored
Merge pull request #1132 from anz-ableton/fix-1121/github-api-version-header
Allow to set GitHub API version
2 parents 0321e7c + 009961d commit ce43e6e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/github3/github.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,21 @@ class GitHub(models.GitHubCore):
5959
call the GitHub object with authentication parameters.
6060
"""
6161

62-
def __init__(self, username="", password="", token="", session=None):
63-
"""Create a new GitHub instance to talk to the API."""
62+
def __init__(
63+
self, username="", password="", token="", session=None, api_version=""
64+
):
65+
"""Create a new GitHub instance to talk to the API.
66+
67+
:param str api_version:
68+
API version to send with X-GitHub-Api-Version header.
69+
See https://docs.github.com/en/rest/overview/api-versions
70+
for details about API versions.
71+
"""
6472
super().__init__({}, session or self.new_session())
73+
74+
if api_version:
75+
self.session.headers.update({"X-GitHub-Api-Version": api_version})
76+
6577
if token:
6678
self.login(username, token=token)
6779
elif username and password:

tests/unit/test_github.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,14 @@ def test_search_users(self):
13901390
headers={},
13911391
)
13921392

1393+
def test_api_version_header(_):
1394+
gh = GitHub(api_version="2022-11-28")
1395+
assert gh.session.headers.get("X-GitHub-Api-Version") == "2022-11-28"
1396+
1397+
def test_api_version_header_not_defined(_):
1398+
gh = GitHub()
1399+
assert gh.session.headers.get("X-GitHub-Api-Version") is None
1400+
13931401

13941402
class TestGitHubRequiresAuthentication(
13951403
helper.UnitRequiresAuthenticationHelper

0 commit comments

Comments
 (0)