Skip to content

Commit

Permalink
✨ add user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Augustinio committed Aug 5, 2024
1 parent 8bf4b1e commit 8f47b14
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand Down
8 changes: 7 additions & 1 deletion pasqal_cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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="[email protected]",
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}"
)

0 comments on commit 8f47b14

Please sign in to comment.