diff --git a/cads_api_client/api_client.py b/cads_api_client/api_client.py index f35a6c3..0867842 100644 --- a/cads_api_client/api_client.py +++ b/cads_api_client/api_client.py @@ -10,7 +10,7 @@ import cads_api_client -from . import catalogue, config, processing, profile +from . import __version__, catalogue, config, processing, profile @attrs.define(slots=False) @@ -69,11 +69,12 @@ def __attrs_post_init__(self) -> None: warnings.warn(str(exc), UserWarning) def _get_headers(self, key_is_mandatory: bool = True) -> dict[str, str]: - if self.key is None: - if key_is_mandatory: - raise ValueError("The API key is needed to access this resource") - return {} - return {"PRIVATE-TOKEN": self.key} + headers = {"User-Agent": f"cads-api-client/{__version__}"} + if self.key is not None: + headers["PRIVATE-TOKEN"] = self.key + elif key_is_mandatory: + raise ValueError("The API key is needed to access this resource") + return headers @property def _retry_options(self) -> dict[str, Any]: @@ -99,10 +100,10 @@ def _request_options(self) -> dict[str, Any]: } def _get_request_kwargs( - self, mandatory_key: bool = True + self, key_is_mandatory: bool = True ) -> processing.RequestKwargs: return processing.RequestKwargs( - headers=self._get_headers(key_is_mandatory=mandatory_key), + headers=self._get_headers(key_is_mandatory=key_is_mandatory), session=self.session, retry_options=self._retry_options, request_options=self._request_options, @@ -116,7 +117,7 @@ def _get_request_kwargs( def _catalogue_api(self) -> catalogue.Catalogue: return catalogue.Catalogue( f"{self.url}/catalogue", - **self._get_request_kwargs(mandatory_key=False), + **self._get_request_kwargs(key_is_mandatory=False), ) @functools.cached_property diff --git a/tests/integration_test_60_api_client.py b/tests/integration_test_60_api_client.py index 0f8c604..81b2eec 100644 --- a/tests/integration_test_60_api_client.py +++ b/tests/integration_test_60_api_client.py @@ -24,12 +24,14 @@ def test_api_client_get_process(api_anon_client: ApiClient) -> None: process = api_anon_client.get_process("test-adaptor-dummy") assert isinstance(process, processing.Process) assert process.id == "test-adaptor-dummy" + assert set(process.headers) == {"User-Agent", "PRIVATE-TOKEN"} def test_api_client_get_remote(api_anon_client: ApiClient) -> None: request_uid = api_anon_client.submit("test-adaptor-dummy").request_uid remote = api_anon_client.get_remote(request_uid) assert remote.request_uid == request_uid + assert set(remote.headers) == {"User-Agent", "PRIVATE-TOKEN"} def test_api_client_get_results(api_anon_client: ApiClient) -> None: