Skip to content

Commit

Permalink
Rewrite XQC API version to 0.2.0 and add User-Agent (#540)
Browse files Browse the repository at this point in the history
**Context:**
The current version of StrawberryFields sends an incorrect version header to the Xanadu Quantum Cloud API.

**Description of the Change:**
- Update the `Accept-Version` header of requests sent from the `strawberryfields.api.connection` module to `0.2.0` instead of `1.0.0`
- Add a custom User-Agent string which sends a header in the standard format `StrawberryFields/0.17.0` where `0.17.0` is the current version of StrawberryFields

**Benefits:**
- Allows compability with the XQC platform in future iterations where API versioning will be more strict
- The `User-Agent` string gives important contextual information to the XQC API which can be used to provide custom behaviour in the future (for example, rewriting behaviour in the case of problematic requests, or providing deprecation notices in the event of lifecycle changes in the API)

**Possible Drawbacks:**
N/A

**Related GitHub Issues:**
N/A

----

* Rewrite XQC API version to 0.2.0
* more
* fmt
* typo
* test fix
* update changelog
* more
  • Loading branch information
Jeremy Swinarton committed Feb 16, 2021
1 parent 079191a commit 31cc0d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

<h3>Bug fixes</h3>

* `Connection` objects now send requests to the platform API at version `0.2.0`
instead of the incorrect version number `1.0.0`.
[(#540)](https://github.com/XanaduAI/strawberryfields/pull/540)

<h3>Documentation</h3>

<h3>Contributors</h3>

This release contains contributions from (in alphabetical order):

Jeremy Swinarton.

# Release 0.17.0 (current release)

<h3>New features since last release</h3>
Expand Down
13 changes: 11 additions & 2 deletions strawberryfields/api/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import numpy as np
import requests

from strawberryfields._version import __version__
from strawberryfields.configuration import load_config
from strawberryfields.io import to_blackbird
from strawberryfields.logger import create_logger
Expand Down Expand Up @@ -97,14 +98,22 @@ def __init__(

self._base_url = "http{}://{}:{}".format("s" if self.use_ssl else "", self.host, self.port)

self._headers = {"Accept-Version": self.api_version}
self._headers = {
"Accept-Version": self.api_version,
"User-Agent": self.user_agent,
}

self.log = create_logger(__name__)

@property
def api_version(self) -> str:
"""str: The platform API version to request."""
return "1.0.0"
return "0.2.0"

@property
def user_agent(self) -> str:
"""str: The User-Agent header sent alongside requests to the platform."""
return f"StrawberryFields/{__version__}"

@property
def token(self) -> str:
Expand Down
5 changes: 4 additions & 1 deletion tests/api/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ def test_refresh_access_token(self, mocker, monkeypatch):

conn = Connection(token=test_token, host=test_host)
conn._refresh_access_token()
expected_headers = {'Accept-Version': conn.api_version}
expected_headers = {
'Accept-Version': conn.api_version,
'User-Agent': conn.user_agent,
}
expected_url = f"https://{test_host}:443{path}"
spy.assert_called_once_with(expected_url, headers=expected_headers, data=data)

Expand Down

0 comments on commit 31cc0d5

Please sign in to comment.