Skip to content

Commit e72dae2

Browse files
authored
fix: allow all client args to be null (#5)
1 parent 559c452 commit e72dae2

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

nodestream_github/client/githubclient.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
An async client for accessing GitHub.
44
"""
55

6+
import json
67
import logging
78
from collections.abc import AsyncGenerator
89
from enum import Enum
@@ -27,6 +28,7 @@
2728
DEFAULT_MAX_RETRIES = 20
2829
DEFAULT_PAGE_SIZE = 100
2930
DEFAULT_MAX_RETRY_WAIT_SECONDS = 300 # 5 minutes
31+
DEFAULT_GITHUB_HOST = "api.github.com"
3032

3133

3234
logger = get_plugin_logger(__name__)
@@ -41,36 +43,39 @@ def __init__(self, url: str | httpx.URL):
4143
super().__init__(f"Rate limited when calling {url}")
4244

4345

46+
def _safe_get_json_error_message(response: httpx.Response) -> str:
47+
try:
48+
return response.json().get("message")
49+
except AttributeError:
50+
# ignore if no message
51+
return json.dumps(response.json())
52+
except ValueError:
53+
# ignore if no json
54+
return response.text
55+
56+
4457
def _fetch_problem(title: str, e: httpx.HTTPError):
45-
if isinstance(e, httpx.HTTPStatusError):
46-
error_message = None
47-
try:
48-
error_message = e.response.json().get("message")
49-
except AttributeError:
50-
# ignore if no message
51-
pass
52-
except ValueError:
53-
# ignore if no json
54-
pass
55-
56-
logger.warning(
57-
"%s %s - %s%s",
58-
e.response.status_code,
59-
e.response.reason_phrase,
60-
e.request.url.path,
61-
f" - {error_message}" if error_message else "",
62-
stacklevel=2,
63-
)
64-
else:
65-
logger.warning("Problem fetching %s", title, exc_info=e, stacklevel=2)
58+
match e:
59+
case httpx.HTTPStatusError(response=response):
60+
error_message = _safe_get_json_error_message(response)
61+
logger.warning(
62+
"%s %s - %s%s",
63+
response.status_code,
64+
response.reason_phrase,
65+
e.request.url.path,
66+
f" - {error_message}" if error_message else "",
67+
stacklevel=2,
68+
)
69+
case _:
70+
logger.warning("Problem fetching %s", title, exc_info=e, stacklevel=2)
6671

6772

6873
class GithubRestApiClient:
6974
def __init__(
7075
self,
71-
auth_token: str,
72-
github_hostname: str = "api.github.com",
7376
*,
77+
auth_token: str | None = None,
78+
github_hostname: str | None = None,
7479
user_agent: str | None = None,
7580
per_page: int | None = None,
7681
max_retries: int | None = None,
@@ -90,15 +95,18 @@ def __init__(
9095
raise ValueError(msg)
9196

9297
self._auth_token = auth_token
93-
if github_hostname != "api.github.com":
94-
self._base_url = f"https://{github_hostname}/api/v3"
95-
else:
98+
if github_hostname == "api.github.com" or github_hostname is None:
9699
self._base_url = "https://api.github.com"
100+
else:
101+
self._base_url = f"https://{github_hostname}/api/v3"
102+
97103
self._per_page = per_page
98104
self._limit_storage = MemoryStorage()
105+
if not self.auth_token:
106+
logger.warning("Missing auth_token.")
99107
self._default_headers = httpx.Headers({
100108
"Accept": "application/vnd.github+json",
101-
"Authorization": "Bearer " + self.auth_token,
109+
"Authorization": f"Bearer {self.auth_token}",
102110
"X-GitHub-Api-Version": "2022-11-28",
103111
})
104112
if user_agent:

nodestream_github/github_teams.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
node_id: !jmespath 'node_id'
1515
additional_indexes:
1616
- slug
17-
- name
1817
- html_url
1918
- type: properties
2019
properties:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "nodestream-plugin-github"
3-
version = "0.13.1-beta.3"
3+
version = "0.13.1-beta.4"
44
description = ""
55
authors = ["Jon Bristow <[email protected]>"]
66
packages = [

tests/client/test_githubclient.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,8 @@ async def test_pagination(httpx_mock: HTTPXMock):
8383

8484
items = [item async for item in client._get_paginated("example")]
8585
assert items == ["a", "b", "c", "d"]
86+
87+
88+
def test_all_null_args():
89+
# noinspection PyTypeChecker
90+
assert GithubRestApiClient(auth_token=None, github_hostname=None)

0 commit comments

Comments
 (0)