qcs_api_client_openapi/apis/
engagements_api.rs

1// Copyright 2022 Rigetti Computing
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*
16 * Rigetti QCS API
17 *
18 * # Introduction  This is the documentation for the Rigetti QCS HTTP API.  You can find out more about Rigetti at [https://rigetti.com](https://rigetti.com), and also interact with QCS via the web at [https://qcs.rigetti.com](https://qcs.rigetti.com).  This API is documented in **OpenAPI format** and so is compatible with the dozens of language-specific client generators available [here](https://github.com/OpenAPITools/openapi-generator) and elsewhere on the web.  # Principles  This API follows REST design principles where appropriate, and otherwise an HTTP RPC paradigm. We adhere to the Google [API Improvement Proposals](https://google.aip.dev/general) where reasonable to provide a consistent, intuitive developer experience. HTTP response codes match their specifications, and error messages fit a common format.  # Authentication  All access to the QCS API requires OAuth2 authentication provided by Okta. You can request access [here](https://www.rigetti.com/get-quantum). Once you have a user account, you can download your access token from QCS [here](https://qcs.rigetti.com/auth/token).   That access token is valid for 24 hours after issuance. The value of `access_token` within the JSON file is the token used for authentication (don't use the entire JSON file).  Authenticate requests using the `Authorization` header and a `Bearer` prefix:  ``` curl --header \"Authorization: Bearer eyJraW...Iow\" ```  # Quantum Processor Access  Access to the quantum processors themselves is not yet provided directly by this HTTP API, but is instead performed over ZeroMQ/[rpcq](https://github.com/rigetti/rpcq). Until that changes, we suggest using [pyquil](https://github.com/rigetti/pyquil) to build and execute quantum programs via the Legacy API.  # Legacy API  Our legacy HTTP API remains accessible at https://forest-server.qcs.rigetti.com, and it shares a source of truth with this API's services. You can use either service with the same user account and means of authentication. We strongly recommend using the API documented here, as the legacy API is on the path to deprecation.
19 *
20 * The version of the OpenAPI document: 2020-07-31
21 * Contact: support@rigetti.com
22 * Generated by: https://openapi-generator.tech
23 */
24
25use super::{configuration, Error};
26use crate::apis::ResponseContent;
27use ::qcs_api_client_common::backoff::{
28    duration_from_io_error, duration_from_reqwest_error, duration_from_response, ExponentialBackoff,
29};
30#[cfg(feature = "tracing")]
31use qcs_api_client_common::configuration::tokens::TokenRefresher;
32use reqwest::StatusCode;
33use serde::{Deserialize, Serialize};
34
35/// struct for typed errors of method [`create_engagement`]
36#[derive(Debug, Clone, Serialize, Deserialize)]
37#[serde(untagged)]
38pub enum CreateEngagementError {
39    Status400(crate::models::Error),
40    Status404(crate::models::Error),
41    Status422(crate::models::Error),
42    Status503(),
43    UnknownValue(serde_json::Value),
44}
45
46async fn create_engagement_inner(
47    configuration: &configuration::Configuration,
48    backoff: &mut ExponentialBackoff,
49    create_engagement_request: crate::models::CreateEngagementRequest,
50    x_qcs_account_id: Option<&str>,
51    x_qcs_account_type: Option<crate::models::AccountType>,
52) -> Result<crate::models::EngagementWithCredentials, Error<CreateEngagementError>> {
53    let local_var_configuration = configuration;
54
55    let local_var_client = &local_var_configuration.client;
56
57    let local_var_uri_str = format!(
58        "{}/v1/engagements",
59        local_var_configuration.qcs_config.api_url()
60    );
61    let mut local_var_req_builder =
62        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
63
64    #[cfg(feature = "tracing")]
65    {
66        // Ignore parsing errors if the URL is invalid for some reason.
67        // If it is invalid, it will turn up as an error later when actually making the request.
68        let local_var_do_tracing = local_var_uri_str
69            .parse::<::url::Url>()
70            .ok()
71            .is_none_or(|url| {
72                configuration
73                    .qcs_config
74                    .should_trace(&::urlpattern::UrlPatternMatchInput::Url(url))
75            });
76
77        if local_var_do_tracing {
78            ::tracing::debug!(
79                url=%local_var_uri_str,
80                method="POST",
81                "making create_engagement request",
82            );
83        }
84    }
85
86    if let Some(local_var_param_value) = x_qcs_account_id {
87        local_var_req_builder =
88            local_var_req_builder.header("x-qcs-account-id", local_var_param_value.to_string());
89    }
90    if let Some(local_var_param_value) = x_qcs_account_type {
91        local_var_req_builder =
92            local_var_req_builder.header("x-qcs-account-type", local_var_param_value.to_string());
93    }
94
95    // Use the QCS Bearer token if a client OAuthSession is present,
96    // but do not require one when the security schema says it is optional.
97    {
98        use qcs_api_client_common::configuration::TokenError;
99
100        #[allow(
101            clippy::nonminimal_bool,
102            clippy::eq_op,
103            reason = "Logic must be done at runtime since it cannot be handled by the mustache template engine."
104        )]
105        let is_jwt_bearer_optional: bool = false || "JWTBearer" == "JWTBearerOptional";
106
107        let token = local_var_configuration
108            .qcs_config
109            .get_bearer_access_token()
110            .await;
111
112        if is_jwt_bearer_optional && matches!(token, Err(TokenError::NoCredentials)) {
113            // the client is configured without any OAuthSession, but this call does not require one.
114            #[cfg(feature = "tracing")]
115            tracing::debug!(
116                "No client credentials found, but this call does not require authentication."
117            );
118        } else {
119            local_var_req_builder = local_var_req_builder.bearer_auth(token?.secret());
120        }
121    }
122
123    local_var_req_builder = local_var_req_builder.json(&create_engagement_request);
124
125    let local_var_req = local_var_req_builder.build()?;
126    let local_var_resp = local_var_client.execute(local_var_req).await?;
127
128    let local_var_status = local_var_resp.status();
129
130    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
131        let local_var_content = local_var_resp.text().await?;
132        serde_json::from_str(&local_var_content).map_err(Error::from)
133    } else {
134        let local_var_retry_delay =
135            duration_from_response(local_var_resp.status(), local_var_resp.headers(), backoff);
136        let local_var_content = local_var_resp.text().await?;
137        let local_var_entity: Option<CreateEngagementError> =
138            serde_json::from_str(&local_var_content).ok();
139        let local_var_error = ResponseContent {
140            status: local_var_status,
141            content: local_var_content,
142            entity: local_var_entity,
143            retry_delay: local_var_retry_delay,
144        };
145        Err(Error::ResponseError(local_var_error))
146    }
147}
148
149/// Create a new engagement using the specified parameters.  At least one of the following parameters must be supplied: - **endpointId**: The ID of the endpoint on which to engage. - **quantumProcessorId**: The ID of the quantum processor on which to engage, allowing the     service to select a default endpoint. Ignored if **endpointId** is set.
150pub async fn create_engagement(
151    configuration: &configuration::Configuration,
152    create_engagement_request: crate::models::CreateEngagementRequest,
153    x_qcs_account_id: Option<&str>,
154    x_qcs_account_type: Option<crate::models::AccountType>,
155) -> Result<crate::models::EngagementWithCredentials, Error<CreateEngagementError>> {
156    let mut backoff = configuration.backoff.clone();
157    let mut refreshed_credentials = false;
158    let method = reqwest::Method::POST;
159    loop {
160        let result = create_engagement_inner(
161            configuration,
162            &mut backoff,
163            create_engagement_request.clone(),
164            x_qcs_account_id.clone(),
165            x_qcs_account_type.clone(),
166        )
167        .await;
168
169        match result {
170            Ok(result) => return Ok(result),
171            Err(Error::ResponseError(response)) => {
172                if !refreshed_credentials
173                    && matches!(
174                        response.status,
175                        StatusCode::FORBIDDEN | StatusCode::UNAUTHORIZED
176                    )
177                {
178                    configuration.qcs_config.refresh().await?;
179                    refreshed_credentials = true;
180                    continue;
181                } else if let Some(duration) = response.retry_delay {
182                    tokio::time::sleep(duration).await;
183                    continue;
184                }
185
186                return Err(Error::ResponseError(response));
187            }
188            Err(Error::Reqwest(error)) => {
189                if let Some(duration) = duration_from_reqwest_error(&method, &error, &mut backoff) {
190                    tokio::time::sleep(duration).await;
191                    continue;
192                }
193
194                return Err(Error::Reqwest(error));
195            }
196            Err(Error::Io(error)) => {
197                if let Some(duration) = duration_from_io_error(&method, &error, &mut backoff) {
198                    tokio::time::sleep(duration).await;
199                    continue;
200                }
201
202                return Err(Error::Io(error));
203            }
204            Err(error) => return Err(error),
205        }
206    }
207}