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

Commit

Permalink
Add User-Agent in headers (#108)
Browse files Browse the repository at this point in the history
* Add User-Agent in headers

* cleanup
  • Loading branch information
malmans2 authored Oct 24, 2024
1 parent aaed7f3 commit 6faa836
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 10 additions & 9 deletions cads_api_client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]:
Expand All @@ -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,
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/integration_test_60_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6faa836

Please sign in to comment.