xcc.Connection

class Connection(refresh_token: Optional[str] = None, access_token: Optional[str] = None, host: str = 'platform.strawberryfields.ai', port: int = 443, tls: bool = True, headers: Optional[Dict[str, str]] = None)[source]

Bases: object

Represents a connection to the Xanadu Cloud.

Parameters
  • refresh_token (str, optional) – JWT refresh token, such as a Xanadu Cloud API key, that is used to fetch access tokens from the Xanadu Cloud

  • access_token (str, optional) – JWT access token that is used to authenticate requests to the Xanadu Cloud; missing, invalid, or expired access tokens are replaced using the refresh token

  • host (str) – hostname of the Xanadu Cloud server

  • port (int) – port of the Xanadu Cloud server

  • tls (bool) – whether to use HTTPS for the connection

  • headers (Dict[str, str], optional) – HTTP request headers to override

Raises

ValueError – if both the refresh token and access token are None

Example:

The following example shows how to use the Connection class to access the Xanadu Cloud. First, a connection is instantiated with a Xanadu Cloud API key:

>>> import xcc
>>> connection = xcc.Connection(refresh_token="Xanadu Cloud API key goes here")

This connection can be tested using ping(). If there is an issue with the connection, a requests.models.HTTPError will be raised.

>>> connection.ping()
<Response [200]>

The Xanadu Cloud can now be directly accessed to, for example, retrieve information about the X8_01 device:

>>> response = connection.request(method="GET", path="/devices/X8_01")
>>> response
<Response [200]>
>>> import json
>>> print(json.dumps(response.json(), indent=4))
{
    "expected_uptime": {
        "monday": [
            "15:00:00+00:00",
            "22:59:59+00:00"
        ],
        "tuesday": [
            "15:00:00+00:00",
            "22:59:59+00:00"
        ],
        "thursday": [
            "15:00:00+00:00",
            "22:59:59+00:00"
        ],
        "wednesday": [
            "15:00:00+00:00",
            "22:59:59+00:00"
        ]
    },
    "created_at": "2021-01-27T15:15:25.801308Z",
    "target": "X8_01",
    "status": "online"
}

access_token

Returns the access token used to authenticate requests to the Xanadu Cloud.

api_version

Returns the "Accept-Version" header included in requests to the Xanadu Cloud.

headers

Returns the headers included in requests to the Xanadu Cloud.

host

Returns the host of the URL used to send requests to the Xanadu Cloud.

port

Returns the port of the URL used to send requests to the Xanadu Cloud

refresh_token

Returns the refresh token used to fetch access tokens.

scheme

Returns the scheme of the URL used to send requests to the Xanadu Cloud.

tls

Returns whether HTTPS is used for the connection to the Xanadu Cloud.

user_agent

Returns the "User-Agent" header included in requests to the Xanadu Cloud.

access_token

Returns the access token used to authenticate requests to the Xanadu Cloud.

api_version

Returns the “Accept-Version” header included in requests to the Xanadu Cloud.

headers

Returns the headers included in requests to the Xanadu Cloud.

host

Returns the host of the URL used to send requests to the Xanadu Cloud.

port

Returns the port of the URL used to send requests to the Xanadu Cloud

refresh_token

Returns the refresh token used to fetch access tokens.

scheme

Returns the scheme of the URL used to send requests to the Xanadu Cloud.

tls

Returns whether HTTPS is used for the connection to the Xanadu Cloud.

user_agent

Returns the “User-Agent” header included in requests to the Xanadu Cloud.

ping()

Pings the Xanadu Cloud.

request(method, path, **kwargs)

Sends an HTTP request to the Xanadu Cloud.

update_access_token()

Updates the access token of a connection using its refresh token.

url([path])

Returns the URL to a Xanadu Cloud endpoint.

ping() requests.models.Response[source]

Pings the Xanadu Cloud.

Returns

HTTP response of the ping HTTP request

Return type

requests.Response

Raises

requests.exceptions.RequestException – if there was an issue sending the ping request to the Xanadu Cloud or the status code of the HTTP response indicates that an error occurred (i.e., 4XX or 5XX)

request(method: str, path: str, **kwargs) requests.models.Response[source]

Sends an HTTP request to the Xanadu Cloud.

Parameters
  • method (str) – HTTP request method

  • path (str) – HTTP request path

  • **kwargs – optional arguments to pass to requests.request()

Returns

HTTP response to the HTTP request

Return type

requests.Response

Raises

requests.exceptions.RequestException – if there was an issue sending the HTTP request or the status code of the HTTP response indicates that an error occurred (i.e., 4XX or 5XX)

Note

A second HTTP request will be made to the Xanadu Cloud if the HTTP response to the first request has a 401 status code. The second request will be identical to the first one except that a fresh access token will be used.

update_access_token() None[source]

Updates the access token of a connection using its refresh token.

Raises

requests.exceptions.RequestException – if there was an issue sending the HTTP request for the access token or the status code of the HTTP response indicates that an error occurred (i.e., 4XX or 5XX)

url(path: str = '') str[source]

Returns the URL to a Xanadu Cloud endpoint.

Parameters

path (str) – path component of the URL

Returns

URL containing a scheme, host, port, the provided path

Return type

str