Skip to content

Commit 9628e78

Browse files
authored
feat: add ability to inject httpx.Client (#79)
Closes #78 This PR adds an optional `httpx_client: httpx.Client` input to the `CsdaClient` creation methods (init and `open()` helper) to facilitate using `httpx.Client` with custom setup (e.g., disabling SSL verification for local dev testing).
1 parent f886497 commit 9628e78

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/csda_client/client.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,42 @@ class CsdaClient:
4949
"""
5050

5151
@classmethod
52-
def open(cls, auth: Auth, url: str = PRODUCTION_URL) -> Self:
52+
def open(
53+
cls, auth: Auth, url: str = PRODUCTION_URL, httpx_client: Client | None = None
54+
) -> Self:
5355
"""Opens and logs in a CSDA client.
5456
5557
Args:
5658
auth: A [`httpx.Auth`](https://www.python-httpx.org/advanced/authentication/) that will be used for [Earthdata login](https://urs.earthdata.nasa.gov).
5759
We recommend either `BasicAuth` or `NetrcAuth`.
5860
url: The CSDA instance to use for queries.
61+
httpx_client: Optionally, inject a `httpx.Client` to use for making requests.
5962
6063
Returns:
6164
A logged-in client.
6265
"""
63-
client = cls(url)
66+
client = cls(url, httpx_client=httpx_client)
6467
client.login(auth)
6568
return client
6669

67-
def __init__(self, url: str = PRODUCTION_URL) -> None:
70+
def __init__(
71+
self, url: str = PRODUCTION_URL, httpx_client: Client | None = None
72+
) -> None:
6873
"""Creates a new, un-logged-in CSDA client.
6974
7075
Once you've created a client, use [login][csda_client.CsdaClient.login] to get an auth token.
7176
7277
Args:
7378
url: The CSDA instance to use for queries.
79+
httpx_client: Optionally, inject a `httpx.Client` to use for making requests.
7480
7581
Returns:
7682
An un-logged-in client.
7783
"""
78-
self.client = Client()
84+
self.client = httpx_client or Client()
7985
self.url = url
8086

81-
def __enter__(self) -> CsdaClient:
87+
def __enter__(self) -> Self:
8288
return self
8389

8490
def __exit__(

tests/test_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from pathlib import Path
22

3+
import httpx
34
import pytest
5+
from pydantic import SecretStr
46

57
from csda_client.client import CsdaClient
68

@@ -24,3 +26,17 @@ def test_download(client: CsdaClient, tmp_path: Path) -> None:
2426
def test_profile(basic_auth_client: CsdaClient, earthdata_username: str) -> None:
2527
profile = basic_auth_client.profile(earthdata_username)
2628
assert profile.earthdata_username == earthdata_username
29+
30+
31+
@pytest.mark.with_earthdata_login
32+
def test_httpx_client_injection(
33+
earthdata_username: str, earthdata_password: SecretStr
34+
) -> None:
35+
httpx_client = httpx.Client(verify=False)
36+
37+
csda_client = CsdaClient(httpx_client=httpx_client)
38+
assert csda_client.client is httpx_client
39+
40+
auth = httpx.BasicAuth(earthdata_username, earthdata_password.get_secret_value())
41+
csda_client = CsdaClient.open(auth=auth, httpx_client=httpx_client)
42+
assert csda_client.client is httpx_client

0 commit comments

Comments
 (0)