From c7a69191d75c4292483452759852fbc35133b7ab Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Thu, 15 Feb 2024 22:04:28 +0100 Subject: [PATCH] Github client: accept request headers --- coverage_comment/github_client.py | 13 +++++++++++-- tests/unit/test_github_client.py | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/coverage_comment/github_client.py b/coverage_comment/github_client.py index 19165b64..e6eb6efa 100644 --- a/coverage_comment/github_client.py +++ b/coverage_comment/github_client.py @@ -47,7 +47,6 @@ def __getattr__(self, attr): class GitHub: - """ GitHub client. """ @@ -58,9 +57,18 @@ def __init__(self, session: httpx.Client): def __getattr__(self, attr): return _Callable(self, "/%s" % attr) - def _http(self, method, path, *, bytes=False, **kw): + def _http( + self, + method: str, + path: str, + *, + bytes: bool = False, + headers: dict[str, str] | None = None, + **kw, + ): _method = method.lower() requests_kwargs = {} + header_kwargs = {"headers": headers} if headers else {} if _method == "get" and kw: requests_kwargs = {"params": kw} @@ -71,6 +79,7 @@ def _http(self, method, path, *, bytes=False, **kw): _method.upper(), path, timeout=TIMEOUT, + **header_kwargs, **requests_kwargs, ) if bytes: diff --git a/tests/unit/test_github_client.py b/tests/unit/test_github_client.py index 8aa147eb..94f1ce9b 100644 --- a/tests/unit/test_github_client.py +++ b/tests/unit/test_github_client.py @@ -13,6 +13,15 @@ def test_github_client__get(session, gh): assert gh.repos("a/b").issues().get(a=1) == {"foo": "bar"} +def test_github_client__get_headers(session, gh): + session.register("GET", "/repos/a/b/issues", timeout=60, params={"a": 1})( + json={"foo": "bar"}, + headers={"X-foo": "yay"}, + ) + + assert gh.repos("a/b").issues().get(a=1, headers={"X-foo": "yay"}) == {"foo": "bar"} + + def test_github_client__post_non_json(session, gh): session.register("POST", "/repos/a/b/issues", timeout=60, json={"a": 1})()