From 42483236cd707347f257b6fee4a9abca103ca224 Mon Sep 17 00:00:00 2001 From: malmans2 Date: Thu, 24 Oct 2024 13:08:35 +0200 Subject: [PATCH 1/2] Add User-Agent in headers --- cads_api_client/api_client.py | 19 ++++++++++--------- tests/integration_test_60_api_client.py | 3 +++ 2 files changed, 13 insertions(+), 9 deletions(-) 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..82d3b44 100644 --- a/tests/integration_test_60_api_client.py +++ b/tests/integration_test_60_api_client.py @@ -24,6 +24,7 @@ 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: @@ -53,11 +54,13 @@ def test_api_client_retrieve( def test_api_client_submit(api_anon_client: ApiClient) -> None: remote = api_anon_client.submit("test-adaptor-dummy") assert isinstance(remote, Remote) + assert set(remote.headers) == {"User-Agent", "PRIVATE-TOKEN"} def test_api_client_submit_and_wait_on_results(api_anon_client: ApiClient) -> None: results = api_anon_client.submit_and_wait_on_results("test-adaptor-dummy") assert isinstance(results, Results) + assert set(results.headers) == {"User-Agent", "PRIVATE-TOKEN"} def test_api_client_verify(api_root_url: str, api_anon_key: str) -> None: From c470dd3d3d1aeaf4ec30bc214bc5fd276019bd0d Mon Sep 17 00:00:00 2001 From: malmans2 Date: Thu, 24 Oct 2024 13:12:20 +0200 Subject: [PATCH 2/2] cleanup --- tests/integration_test_60_api_client.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/integration_test_60_api_client.py b/tests/integration_test_60_api_client.py index 82d3b44..81b2eec 100644 --- a/tests/integration_test_60_api_client.py +++ b/tests/integration_test_60_api_client.py @@ -31,6 +31,7 @@ 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: @@ -54,13 +55,11 @@ def test_api_client_retrieve( def test_api_client_submit(api_anon_client: ApiClient) -> None: remote = api_anon_client.submit("test-adaptor-dummy") assert isinstance(remote, Remote) - assert set(remote.headers) == {"User-Agent", "PRIVATE-TOKEN"} def test_api_client_submit_and_wait_on_results(api_anon_client: ApiClient) -> None: results = api_anon_client.submit_and_wait_on_results("test-adaptor-dummy") assert isinstance(results, Results) - assert set(results.headers) == {"User-Agent", "PRIVATE-TOKEN"} def test_api_client_verify(api_root_url: str, api_anon_key: str) -> None: