Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented convenience method for client id #97

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

Sharvin-M
Copy link

@Sharvin-M Sharvin-M commented Jan 14, 2025

Wrote the method for a user to specify the 'Client-Id' header in the Query class. By default, the method should return the Client-Id of the request as follows: "Client-Id: python_cmr-v0.13.0". If a client-id parameter is specified by the user, it should return: "Client-Id": f"{client_id} python_cmr-v0.13."

This is my first time contributing to open source, so I will appreciate any and all feedback! @chuckwondo @frankinspace

@Sharvin-M Sharvin-M marked this pull request as draft January 14, 2025 21:06
@Sharvin-M Sharvin-M marked this pull request as ready for review January 14, 2025 21:06
Copy link
Collaborator

@frankinspace frankinspace left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution! I added a couple suggestions.

cmr/queries.py Outdated
"""

if not client_id:
self.headers.update({"Client-Id: python_cmr-v0.13.0"})
Copy link
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: python_cmr-v0.13.0"})
self.headers.update({"Client-Id": "python_cmr-v0.13.0"})

cmr/queries.py Outdated
@@ -365,6 +365,26 @@ def bearer_token(self, bearer_token: str) -> Self:

return self

def client_id(self, client_id: str) -> Self:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should be able to use https://docs.python.org/3/library/importlib.metadata.html#importlib.metadata.version to dynamically replace the version of python_cmr in use instead of hard-coding it in the string.

@frankinspace frankinspace linked an issue Jan 14, 2025 that may be closed by this pull request
@frankinspace
Copy link
Collaborator

I'd also suggest adding a quick test function in https://github.com/nasa/python_cmr/blob/35b8bdafa655c9a95edc41659ccb867df9e86c18/tests/test_queries.py to test that the custom string is appended as expected to the header.

I'd also suggest calling the function from the __init__ function: https://github.com/nasa/python_cmr/blob/develop/cmr/queries.py#L61
So that the default value is added as a header when the object is first initialized. Any subsequent calls to client_id would override the initial value.

Copy link
Collaborator

@chuckwondo chuckwondo left a comment

Choose a reason for hiding this comment

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

@Sharvin-M, thanks for your first contribution!

cmr/queries.py Outdated
@@ -365,6 +365,26 @@ def bearer_token(self, bearer_token: str) -> Self:

return self

def client_id(self, client_id: str) -> Self:
Copy link
Collaborator

Choose a reason for hiding this comment

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

To avoid confusion, I suggest that you do not name the function parameter the same as the function itself. I suggest changing the function parameter name to id_ instead of client_id. Note the trailing underscore on id_ because there is a global function named id, so the trailing underscore is a convention used to avoid name collisions with global variables.

cmr/queries.py Outdated
@@ -365,6 +365,26 @@ def bearer_token(self, bearer_token: str) -> Self:

return self

def client_id(self, client_id: str) -> Self:
"""
Set the value of this query's 'Client ID' header according to User's input.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Set the value of this query's 'Client ID' header according to User's input.
Set the value of this query's `Client-Id` header.

cmr/queries.py Outdated
Comment on lines 374 to 375
Otherwise, set the specified paramter option to the value along with
the suffix (python_cmr-vX.Y.Z) and a space character between the specified paramter and the suffix.
Copy link
Collaborator

Choose a reason for hiding this comment

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

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

@Sharvin-M
Copy link
Author

I attempted to add the requested changes, let me know your thoughts! @frankinspace @chuckwondo

cmr/queries.py Outdated Show resolved Hide resolved
cmr/queries.py Outdated Show resolved Hide resolved
cmr/queries.py Outdated
Comment on lines 379 to 384
if not id_:
return self

self.headers.update(
{"Client-Id": f"{id_} python_cmr-v{version('python_cmr')}"}
)
Copy link
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

cmr/queries.py Outdated
@@ -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
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()

query.token("token")

expected_version = version("python_cmr")
assert query.headers["Client-Id"] == f"test_client python_cmr-v{expected_version}"
Copy link
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})"

@frankinspace
Copy link
Collaborator

For some reason the VCR tests in multiple queries are failing. I would have expected failure due to unexpected new header but they seem to be failing in a way that is causing twice the number of requests to be sent

@chuckwondo
Copy link
Collaborator

For some reason the VCR tests in multiple queries are failing. I would have expected failure due to unexpected new header but they seem to be failing in a way that is causing twice the number of requests to be sent

I can dig into it, if you like.

@Sharvin-M
Copy link
Author

Sharvin-M commented Jan 17, 2025

I looked into it a bit, Please let me know if any of this is useful info @frankinspace @chuckwondo

1. There was a type error because no arg is provided to the client_id method in the init function. To temporarily bypass this, I passed in the default argument manually (on line 72). I don't entirely understand how optional arguments work in Python.

# line 72
self.client_id(f"python_cmr-v{version('python_cmr')}") 

2. I was then able to run the tests, which had one failing related to the get_all method:

Screenshot 2025-01-17 at 9 38 16 AM

3. There were many deprecation warnings related to not using the results method in all of the tests being run.

cmr/queries.py Outdated Show resolved Hide resolved
Co-authored-by: Chuck Daniels <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide convenience method for adding a client-id header
3 participants