Skip to content
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

Add support for API version #157

Merged
merged 1 commit into from
Dec 4, 2022
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ isort-check:
black:
@poetry run black --fast aiogithubapi

example:
@poetry run python example.py

black-check:
@poetry run black --check --fast aiogithubapi
3 changes: 3 additions & 0 deletions aiogithubapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ def __init__(
self,
session: aiohttp.ClientSession,
token: str | None = None,
*,
api_version: str | None = None,
**kwargs: Dict[GitHubClientKwarg, Any],
) -> None:
"""Initialise the GitHub API client."""
self._base_request_data = GitHubBaseRequestDataModel(
token=token,
api_version=api_version,
kwargs=kwargs,
)
self._session = session
Expand Down
5 changes: 5 additions & 0 deletions aiogithubapi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
# but it is adviced to use your own when building out your application
DEFAULT_USER_AGENT = f"aiogithubapi/{PROJECT_VERSION}"

# https://docs.github.com/en/rest/overview/api-versions
HEADER_GITHUB_API_VERSION = "X-GitHub-Api-Version"
DEFAULT_API_VERSION = "2022-11-28"

BASE_API_URL = "https://api.github.com"
BASE_GITHUB_URL = "https://github.com"
OAUTH_DEVICE_LOGIN_PATH = "/login/device/code"
Expand Down Expand Up @@ -199,4 +203,5 @@ class DeviceFlowError(str, Enum):
ACCEPT: GitHubRequestAcceptHeader.BASE.value,
CONTENT_TYPE: HttpContentType.JSON.value,
USER_AGENT: DEFAULT_USER_AGENT,
HEADER_GITHUB_API_VERSION: DEFAULT_API_VERSION,
}
15 changes: 14 additions & 1 deletion aiogithubapi/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def __init__(
self,
token: str = None,
session: aiohttp.ClientSession = None,
*,
api_version: str | None = None,
**kwargs: Dict[GitHubClientKwarg, Any],
) -> None:
"""
Expand Down Expand Up @@ -67,7 +69,7 @@ def __init__(
token = os.getenv("GITHUB_TOKEN")

self._session = session
self._client = GitHubClient(token=token, session=session, **kwargs)
self._client = GitHubClient(token=token, session=session, api_version=api_version, **kwargs)

# Namespaces
self._repos = GitHubReposNamespace(self._client)
Expand Down Expand Up @@ -150,6 +152,17 @@ async def emojis(
"""
return await self._client.async_call_api(endpoint="/emojis", **kwargs)

async def versions(
self,
**kwargs: Dict[GitHubRequestKwarg, Any],
) -> GitHubResponseModel[list[str]]:
"""
Get all supported GitHub API versions.

https://docs.github.com/en/rest/meta#get-all-api-versions
"""
return await self._client.async_call_api(endpoint="/versions", **kwargs)

async def markdown(
self,
text: str,
Expand Down
4 changes: 4 additions & 0 deletions aiogithubapi/models/request_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
BASE_API_HEADERS,
BASE_API_URL,
DEFAULT_USER_AGENT,
HEADER_GITHUB_API_VERSION,
GitHubClientKwarg,
)
from .base import GitHubBase
Expand All @@ -21,6 +22,7 @@ class GitHubBaseRequestDataModel(GitHubBase):

kwargs: Dict[GitHubClientKwarg, Any]
token: str | None = None
api_version: str | None = None

def __post_init__(self):
"""Check user agent."""
Expand Down Expand Up @@ -53,4 +55,6 @@ def headers(self) -> Dict[str, str]:
headers.update(kwarg_headers)
if client_name := self.kwargs.get(GitHubClientKwarg.CLIENT_NAME):
headers[USER_AGENT] = client_name
if self.api_version is not None:
headers[HEADER_GITHUB_API_VERSION] = self.api_version
return headers
1 change: 1 addition & 0 deletions aiogithubapi/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class GitHubResponseHeadersModel(GitHubDataModelBase):
x_commonmarker_version: str | None = None
x_content_type_options: str | None = None
x_frame_options: str | None = None
x_github_api_version_selected: str | None = None
x_github_media_type: str | None = None
x_github_request_id: str | None = None
x_oauth_client_id: str | None = None
Expand Down
14 changes: 13 additions & 1 deletion tests/client/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

from aiogithubapi.client import GitHubClient
from aiogithubapi.const import BASE_API_HEADERS, DEFAULT_USER_AGENT, GitHubClientKwarg
from aiogithubapi.const import BASE_API_HEADERS, DEFAULT_USER_AGENT, HEADER_GITHUB_API_VERSION, GitHubClientKwarg

from tests.common import TOKEN

Expand Down Expand Up @@ -41,6 +41,18 @@ async def test_client_constrution_with_token(client_session: ClientSession):
}


@pytest.mark.asyncio
async def test_client_constrution_with_api_version(client_session: ClientSession):
client = GitHubClient(session=client_session, token=TOKEN, api_version="3000-01-01")
base_request_data = client._base_request_data
assert base_request_data.token == TOKEN
assert base_request_data.headers == {
**BASE_API_HEADERS,
HEADER_GITHUB_API_VERSION: "3000-01-01",
"Authorization": "token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
}


@pytest.mark.asyncio
async def test_client_constrution_with_kwargs_timeout(client_session: ClientSession):
client = GitHubClient(session=client_session, **{GitHubClientKwarg.TIMEOUT: 10})
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/headers.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"github-authentication-token-expiration": "1970-01-01 01:00:00 UTC",
"X-GitHub-Media-Type": "github.v3; param=raw; format=json",
"Link": "<https://api.github.com/repositories/1/issues?page=1>; rel=\"prev\", <https://api.github.com/repositories/1/issues?page=3>; rel=\"next\", <https://api.github.com/repositories/1/issues?page=46>; rel=\"last\"",
"X-GitHub-Api-Version-Selected": "2022-11-28",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4999",
"X-RateLimit-Reset": "1",
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"2022-11-28"
]
6 changes: 6 additions & 0 deletions tests/github/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ async def test_token():
assert GitHubAPI(token=TOKEN)._client._base_request_data.token == TOKEN
with patch("os.environ", {"GITHUB_TOKEN": TOKEN}):
assert GitHubAPI()._client._base_request_data.token == TOKEN


@pytest.mark.asyncio
async def test_api_version():
assert GitHubAPI()._client._base_request_data.token is None
assert GitHubAPI(token=TOKEN, api_version="3000-01-01")._client._base_request_data.api_version == "3000-01-01"
23 changes: 23 additions & 0 deletions tests/github/test_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Test zen."""
# pylint: disable=missing-docstring
from aiohttp.hdrs import CONTENT_TYPE
import pytest

from aiogithubapi import GitHubAPI
from aiogithubapi.const import HttpContentType

from tests.common import MockedRequests, MockResponse


@pytest.mark.asyncio
async def test_versions(
github_api: GitHubAPI,
mock_response: MockResponse,
mock_requests: MockedRequests,
):
response = await github_api.versions()
assert response.status == 200
assert response.data[0] == "2022-11-28"
assert response.headers.x_github_api_version_selected == "2022-11-28"
assert mock_requests.called == 1
assert mock_requests.last_request["url"] == "https://api.github.com/versions"