Skip to content

Commit 50f1845

Browse files
committed
Allow to set GitHub API version
This adds a new set_api_version method the GitHub class that accepts an API version string as defined in [1]. The version is added to the session headers. Sessions headers remain unchanged if no version string was given. Addresses #1121. [1]: https://docs.github.com/en/rest/overview/api-versions?apiVersion=2022-11-28
1 parent 0321e7c commit 50f1845

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/github3/github.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,17 @@ def set_client_id(self, id, secret):
25962596
"""
25972597
self.session.params = {"client_id": id, "client_secret": secret}
25982598

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+
25992610
def set_user_agent(self, user_agent):
26002611
"""Allow the user to set their own user agent string.
26012612

tests/unit/test_github.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,12 @@ 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+
730736
def test_set_user_agent(self):
731737
self.instance.set_user_agent("github3py")
732738
self.session.headers.update.assert_called_once_with(

0 commit comments

Comments
 (0)