diff --git a/CHANGELOG.md b/CHANGELOG.md index 0abffa23..eeef5602 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. +## [0.11.3] - 2024-08-05 + +### Added + +- Introduced the `User-Agent` header. + ## [0.11.2] - 2024-07-31 ### Added @@ -33,7 +39,7 @@ All notable changes to this project will be documented in this file. ### Changed - Expose add_jobs and close_batch functions in the SDK interface -- Refactor non-private methods that were prefixed with _ +- Refactor non-private methods that were prefixed with \_ - Add test coverage for new functions - Drop priority from Batch attributes @@ -84,8 +90,8 @@ All notable changes to this project will be documented in this file. ### Added - Added feature to create an "open" batch. - - To create an open batch, set the `complete` argument to `True` in the `create_batch` method of the SDK. - - To add jobs to an open batch, use the `add_jobs` method. + - To create an open batch, set the `complete` argument to `True` in the `create_batch` method of the SDK. + - To add jobs to an open batch, use the `add_jobs` method. - Updated documentation to add examples to create open batches. - The `wait` argument now waits for all the jobs to be terminated instead of waiting for the batch to be terminated. diff --git a/pasqal_cloud/client.py b/pasqal_cloud/client.py index bd484ac6..b4d6445b 100644 --- a/pasqal_cloud/client.py +++ b/pasqal_cloud/client.py @@ -20,6 +20,7 @@ import requests from requests.auth import AuthBase +from pasqal_cloud._version import __version__ as sdk_version from pasqal_cloud.authentication import ( Auth0TokenProvider, HTTPBearerAuthenticator, @@ -35,6 +36,7 @@ from pasqal_cloud.utils.jsend import JobResult, JSendPayload from pasqal_cloud.utils.retry import retry_http_error + TIMEOUT = 30 # client http requests timeout after 30s @@ -69,6 +71,7 @@ def __init__( self.authenticator = HTTPBearerAuthenticator(token_provider) self.project_id = project_id + self.user_agent = f"PasqalCloudSDK/{sdk_version}" @staticmethod def _make_endpoints(endpoints: Optional[Endpoints]) -> Endpoints: @@ -123,7 +126,10 @@ def _authenticated_request( url, json=payload, timeout=TIMEOUT, - headers={"content-type": "application/json"}, + headers={ + "content-type": "application/json", + "User-Agent": self.user_agent, + }, auth=self.authenticator, params=params, ) diff --git a/tests/test_client.py b/tests/test_client.py index 7a595c99..c88d2691 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -14,6 +14,7 @@ PASQAL_ENDPOINTS, SDK, ) +from pasqal_cloud._version import __version__ as sdk_version from pasqal_cloud.authentication import TokenProvider from tests.test_doubles.authentication import ( FakeAuth0AuthenticationFailure, @@ -442,3 +443,41 @@ def test_request_pagination_without_pagination_success( ) assert len(response) == 1 assert response == [{"id": 1}] + +class TestHeaders: + @pytest.fixture(autouse=True) + @patch( + "pasqal_cloud.client.Auth0TokenProvider", + FakeAuth0AuthenticationSuccess, + ) + def _init_sdk(self): + self.sdk = SDK( + username="me@test.com", + password="password", + project_id=str(uuid4()), + ) + + def test_user_agent_in_request_headers( + self, mock_request: Generator[Any, Any, None] + ): + """ + Test that the `_authenticated_request` method of + the client injects the user-agent header properly + in the headers. + """ + mock_request.reset_mock() + mock_request.register_uri( + "GET", + "http://core-test.com", + status_code=200, + json={ + "ok": True + } + ) + + _ = self.sdk._client._authenticated_request( + "GET", "http://core-test.com" + ) + assert mock_request.last_request.headers["User-Agent"] == ( + f"PasqalCloudSDK/{sdk_version}" + )