Skip to content

Commit

Permalink
Change: Allow to pass full URL to GitHub API client methods
Browse files Browse the repository at this point in the history
Allow to request full URLs when using the GitHub REST API client class.
This allows for easier handling of Hypermedia references.
  • Loading branch information
bjoernricks committed Oct 25, 2022
1 parent e58f02d commit 62863f8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
44 changes: 20 additions & 24 deletions pontos/github/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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

Expand All @@ -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(
Expand All @@ -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
)
Expand All @@ -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
)
Expand Down
40 changes: 40 additions & 0 deletions tests/github/api/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
Expand Down Expand Up @@ -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"})

Expand All @@ -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")

Expand Down

0 comments on commit 62863f8

Please sign in to comment.