Skip to content

Commit 009961d

Browse files
committed
Set GitHub API version via __init__
This replaces the setter method with a named parameter of __init__.
1 parent 50f1845 commit 009961d

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/github3/github.py

Lines changed: 14 additions & 13 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:
@@ -2596,17 +2608,6 @@ def set_client_id(self, id, secret):
25962608
"""
25972609
self.session.params = {"client_id": id, "client_secret": secret}
25982610

2599-
def set_api_version(self, api_version):
2600-
"""Allow to set a specific API version.
2601-
2602-
:param str api_version:
2603-
API version to send with X-GitHub-Api-Version header.
2604-
See https://docs.github.com/en/rest/overview/api-versions?apiVersion=2022-11-28 for details on API versions.
2605-
""" # noqa: E501
2606-
if not api_version:
2607-
return
2608-
self.session.headers.update({"X-GitHub-Api-Version": api_version})
2609-
26102611
def set_user_agent(self, user_agent):
26112612
"""Allow the user to set their own user agent string.
26122613

tests/unit/test_github.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -727,12 +727,6 @@ def test_set_user_agent_required_user_agent(self):
727727

728728
self.session.headers.update.called is False
729729

730-
def test_set_api_version(self):
731-
self.instance.set_api_version("2022-11-28")
732-
self.session.headers.update.assert_called_once_with(
733-
{"X-GitHub-Api-Version": "2022-11-28"}
734-
)
735-
736730
def test_set_user_agent(self):
737731
self.instance.set_user_agent("github3py")
738732
self.session.headers.update.assert_called_once_with(
@@ -1396,6 +1390,14 @@ def test_search_users(self):
13961390
headers={},
13971391
)
13981392

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+
13991401

14001402
class TestGitHubRequiresAuthentication(
14011403
helper.UnitRequiresAuthenticationHelper

0 commit comments

Comments
 (0)