From 62863f82cf4426a2fdf719a3685cb131e18fc0e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Mon, 17 Oct 2022 12:05:21 +0200 Subject: [PATCH] Change: Allow to pass full URL to GitHub API client methods Allow to request full URLs when using the GitHub REST API client class. This allows for easier handling of Hypermedia references. --- pontos/github/api/client.py | 44 +++++++++++++++------------------ tests/github/api/test_client.py | 40 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/pontos/github/api/client.py b/pontos/github/api/client.py index 27d17b791..e901f3fcd 100644 --- a/pontos/github/api/client.py +++ b/pontos/github/api/client.py @@ -96,26 +96,14 @@ def _request_kwargs( kwargs["content"] = content return kwargs - def _request_api_url(self, api) -> str: + def _request_api_url(self, api: str) -> str: return f"{self.url}{api}" - async def _get( - self, - url: str, - *, - params: Optional[Params] = None, - ) -> httpx.Response: - """ - Internal get request. Requires the full API URL. - """ - headers = self._request_headers() - kwargs = self._request_kwargs() - return await self._client.get( - url, - headers=headers, - params=params, - follow_redirects=True, - **kwargs, + def _request_url(self, api_or_url: str) -> str: + return ( + api_or_url + if api_or_url.startswith("https") + else self._request_api_url(api_or_url) ) async def get( @@ -131,8 +119,16 @@ async def get( api: API path to use for the get request params: Optional params to use for the get request """ - url = self._request_api_url(api) - return await self._get(url, params=params) + url = self._request_url(api) + headers = self._request_headers() + kwargs = self._request_kwargs() + return await self._client.get( + url, + headers=headers, + params=params, + follow_redirects=True, + **kwargs, + ) async def get_all( self, @@ -154,7 +150,7 @@ async def get_all( next_url = _get_next_url(response) while next_url: - response = await self._get(next_url, params=params) + response = await self.get(next_url, params=params) yield response @@ -171,7 +167,7 @@ async def delete( params: Optional params to use for the delete request """ headers = self._request_headers() - url = self._request_api_url(api) + url = self._request_url(api) return await self._client.delete(url, params=params, headers=headers) async def post( @@ -186,7 +182,7 @@ async def post( data: Optional data to include in the post request """ headers = self._request_headers() - url = self._request_api_url(api) + url = self._request_url(api) return await self._client.post( url, params=params, headers=headers, json=data ) @@ -199,7 +195,7 @@ def stream(self, api: str) -> AsyncContextManager[httpx.Response]: api: API path to use for the post request """ headers = self._request_headers() - url = api if api.startswith("https") else self._request_api_url(api) + url = self._request_url(api) return self._client.stream( "GET", url, headers=headers, follow_redirects=True ) diff --git a/tests/github/api/test_client.py b/tests/github/api/test_client.py index a9cc4f022..0b4bc1ab3 100644 --- a/tests/github/api/test_client.py +++ b/tests/github/api/test_client.py @@ -44,6 +44,19 @@ async def test_get(self): follow_redirects=True, ) + async def test_get_url(self): + await self.client.get("https://github.com/foo/bar") + + self.http_client.get.assert_awaited_once_with( + "https://github.com/foo/bar", + headers={ + "Accept": "application/vnd.github.v3+json", + "Authorization": "token token", + }, + params=None, + follow_redirects=True, + ) + async def test_get_all(self): url = "https://foo.bar" response1 = MagicMock(links={"next": {"url": url}}) @@ -96,6 +109,18 @@ async def test_delete(self): params=None, ) + async def test_delete_url(self): + await self.client.delete("https://github.com/foo/bar") + + self.http_client.delete.assert_awaited_once_with( + "https://github.com/foo/bar", + headers={ + "Accept": "application/vnd.github.v3+json", + "Authorization": "token token", + }, + params=None, + ) + async def test_post(self): await self.client.post("/foo/bar", data={"foo": "bar"}) @@ -109,6 +134,21 @@ async def test_post(self): params=None, ) + async def test_post_url(self): + await self.client.post( + "https://github.com/foo/bar", data={"foo": "bar"} + ) + + self.http_client.post.assert_awaited_once_with( + "https://github.com/foo/bar", + headers={ + "Accept": "application/vnd.github.v3+json", + "Authorization": "token token", + }, + json={"foo": "bar"}, + params=None, + ) + def test_stream(self): self.client.stream("/foo/bar")