Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions cmr/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from inspect import getmembers, ismethod
from re import search
from typing import Iterator
from importlib.metadata import version

from typing_extensions import (
Any,
Expand Down Expand Up @@ -59,6 +60,7 @@ def __init__(self, route: str, mode: str = CMR_OPS):
self.mode(mode)
self.concept_id_chars: Set[str] = set()
self.headers: MutableMapping[str, str] = {}
self.headers.update({"Client-Id": f"python_cmr-v{version('python_cmr')}"})
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.headers.update({"Client-Id": f"python_cmr-v{version('python_cmr')}"})
self.client_id()


@deprecated("Use the 'results' method instead, but note that it produces an iterator.")
def get(self, limit: int = 2000) -> Sequence[Any]:
Expand Down Expand Up @@ -365,6 +367,26 @@ def bearer_token(self, bearer_token: str) -> Self:

return self

def client_id(self, id_: str) -> Self:
"""
Set the value of this query's `Client-Id` header.

Otherwise, set the header value to the specified value along with
the suffix `(python_cmr-vX.Y.Z)`, separated by a space character.

:param client_id
:returns self
"""

if not id_:
return self

self.headers.update(
{"Client-Id": f"{id_} python_cmr-v{version('python_cmr')}"}
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not id_:
return self
self.headers.update(
{"Client-Id": f"{id_} python_cmr-v{version('python_cmr')}"}
)
python_cmr_id = f"python_cmr-v{version('python_cmr')}"
self.headers["Client-Id"] = f"{id_} ({python_cmr_id})" if id_ else python_cmr_id


return self

def option(
self, parameter: str, key: str, value: Union[str, bool, int, float, None]
) -> Self:
Expand Down
13 changes: 11 additions & 2 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from cmr import Query

from importlib.metadata import version

class MockQuery(Query):
def _valid_state(self) -> bool:
Expand All @@ -8,7 +8,8 @@ def _valid_state(self) -> bool:

def test_query_headers_initially_empty():
query = MockQuery("/foo")
assert query.headers == {}
expected_version = version("python_cmr")
assert query.headers == {"Client-Id": f"python_cmr-v{expected_version}"}


def test_bearer_token_adds_header():
Expand Down Expand Up @@ -55,3 +56,11 @@ def test_token_replaces_existing_auth_header():
query.token("token")

assert query.headers["Authorization"] == "token"

def test_client_id_sets_header():
query = MockQuery("/foo")
query.client_id("test_client")
query.token("token")

expected_version = version("python_cmr")
assert query.headers["Client-Id"] == f"test_client python_cmr-v{expected_version}"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert query.headers["Client-Id"] == f"test_client python_cmr-v{expected_version}"
assert query.headers["Client-Id"] == f"test_client (python_cmr-v{expected_version})"