Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 6faa836

Browse files
authored
Add User-Agent in headers (#108)
* Add User-Agent in headers * cleanup
1 parent aaed7f3 commit 6faa836

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

cads_api_client/api_client.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import cads_api_client
1212

13-
from . import catalogue, config, processing, profile
13+
from . import __version__, catalogue, config, processing, profile
1414

1515

1616
@attrs.define(slots=False)
@@ -69,11 +69,12 @@ def __attrs_post_init__(self) -> None:
6969
warnings.warn(str(exc), UserWarning)
7070

7171
def _get_headers(self, key_is_mandatory: bool = True) -> dict[str, str]:
72-
if self.key is None:
73-
if key_is_mandatory:
74-
raise ValueError("The API key is needed to access this resource")
75-
return {}
76-
return {"PRIVATE-TOKEN": self.key}
72+
headers = {"User-Agent": f"cads-api-client/{__version__}"}
73+
if self.key is not None:
74+
headers["PRIVATE-TOKEN"] = self.key
75+
elif key_is_mandatory:
76+
raise ValueError("The API key is needed to access this resource")
77+
return headers
7778

7879
@property
7980
def _retry_options(self) -> dict[str, Any]:
@@ -99,10 +100,10 @@ def _request_options(self) -> dict[str, Any]:
99100
}
100101

101102
def _get_request_kwargs(
102-
self, mandatory_key: bool = True
103+
self, key_is_mandatory: bool = True
103104
) -> processing.RequestKwargs:
104105
return processing.RequestKwargs(
105-
headers=self._get_headers(key_is_mandatory=mandatory_key),
106+
headers=self._get_headers(key_is_mandatory=key_is_mandatory),
106107
session=self.session,
107108
retry_options=self._retry_options,
108109
request_options=self._request_options,
@@ -116,7 +117,7 @@ def _get_request_kwargs(
116117
def _catalogue_api(self) -> catalogue.Catalogue:
117118
return catalogue.Catalogue(
118119
f"{self.url}/catalogue",
119-
**self._get_request_kwargs(mandatory_key=False),
120+
**self._get_request_kwargs(key_is_mandatory=False),
120121
)
121122

122123
@functools.cached_property

tests/integration_test_60_api_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ def test_api_client_get_process(api_anon_client: ApiClient) -> None:
2424
process = api_anon_client.get_process("test-adaptor-dummy")
2525
assert isinstance(process, processing.Process)
2626
assert process.id == "test-adaptor-dummy"
27+
assert set(process.headers) == {"User-Agent", "PRIVATE-TOKEN"}
2728

2829

2930
def test_api_client_get_remote(api_anon_client: ApiClient) -> None:
3031
request_uid = api_anon_client.submit("test-adaptor-dummy").request_uid
3132
remote = api_anon_client.get_remote(request_uid)
3233
assert remote.request_uid == request_uid
34+
assert set(remote.headers) == {"User-Agent", "PRIVATE-TOKEN"}
3335

3436

3537
def test_api_client_get_results(api_anon_client: ApiClient) -> None:

0 commit comments

Comments
 (0)