Skip to main content

qcs_api_client_openapi/apis/
mod.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
15use std::error;
16use std::fmt;
17
18#[derive(Debug, Clone)]
19pub struct ResponseContent<T> {
20    pub status: reqwest::StatusCode,
21    pub content: String,
22    pub entity: Option<T>,
23    pub retry_delay: Option<std::time::Duration>,
24}
25
26#[derive(Debug)]
27pub enum Error<T> {
28    Reqwest(reqwest::Error),
29    Serde(serde_path_to_error::Error<serde_json::Error>),
30    Io(std::io::Error),
31    QcsToken(crate::common::configuration::TokenError),
32    ResponseError(ResponseContent<T>),
33    InvalidContentType {
34        content_type: String,
35        return_type: &'static str,
36    },
37    #[cfg(feature = "tracing-opentelemetry")]
38    ReqwestMiddleware(anyhow::Error),
39}
40
41impl<T> Error<T> {
42    pub fn status_code(&self) -> Option<reqwest::StatusCode> {
43        match self {
44            Self::ResponseError(err) => Some(err.status),
45            _ => None,
46        }
47    }
48}
49
50impl<T> fmt::Display for Error<T> {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        let (module, e) = match self {
53            Error::Reqwest(e) => ("reqwest", e.to_string()),
54            Error::Serde(e) => ("serde", e.to_string()),
55            Error::Io(e) => ("IO", e.to_string()),
56            Error::QcsToken(e) => ("refresh_qcs_token", e.to_string()),
57            Error::ResponseError(e) => (
58                "response",
59                format!("status code {}: {}", e.status, e.content),
60            ),
61            Error::InvalidContentType { content_type, return_type } => (
62                "api",
63                format!("received {content_type} content type response that cannot be converted to `{return_type}`"),
64            ),
65            #[cfg(feature = "tracing-opentelemetry")]
66            Error::ReqwestMiddleware(e) => ("reqwest-middleware", e.to_string()),
67        };
68        write!(f, "error in {}: {}", module, e)
69    }
70}
71
72impl<T: fmt::Debug> error::Error for Error<T> {
73    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
74        Some(match self {
75            Error::Reqwest(e) => e,
76            Error::Serde(e) => e,
77            Error::Io(e) => e,
78            Error::QcsToken(e) => e,
79            #[cfg(feature = "tracing-opentelemetry")]
80            Error::ReqwestMiddleware(e) => e.source()?,
81            Error::InvalidContentType { .. } => return None,
82            Error::ResponseError(_) => return None,
83        })
84    }
85}
86
87impl<T> From<reqwest::Error> for Error<T> {
88    fn from(e: reqwest::Error) -> Self {
89        Error::Reqwest(e)
90    }
91}
92
93#[cfg(feature = "tracing-opentelemetry")]
94impl<T> From<reqwest_middleware::Error> for Error<T> {
95    fn from(e: reqwest_middleware::Error) -> Self {
96        match e {
97            reqwest_middleware::Error::Reqwest(e) => Error::Reqwest(e),
98            reqwest_middleware::Error::Middleware(e) => Error::ReqwestMiddleware(e),
99        }
100    }
101}
102
103impl<T> From<serde_path_to_error::Error<serde_json::Error>> for Error<T> {
104    fn from(e: serde_path_to_error::Error<serde_json::Error>) -> Self {
105        Error::Serde(e)
106    }
107}
108
109impl<T> From<std::io::Error> for Error<T> {
110    fn from(e: std::io::Error) -> Self {
111        Error::Io(e)
112    }
113}
114
115impl<T> From<crate::common::configuration::TokenError> for Error<T> {
116    fn from(e: crate::common::configuration::TokenError) -> Self {
117        Error::QcsToken(e)
118    }
119}
120
121pub fn urlencode<T: AsRef<str>>(s: T) -> String {
122    ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
123}
124
125pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String, String)> {
126    if let serde_json::Value::Object(object) = value {
127        let mut params = vec![];
128
129        for (key, value) in object {
130            match value {
131                serde_json::Value::Object(_) => params.append(&mut parse_deep_object(
132                    &format!("{}[{}]", prefix, key),
133                    value,
134                )),
135                serde_json::Value::Array(array) => {
136                    for (i, value) in array.iter().enumerate() {
137                        params.append(&mut parse_deep_object(
138                            &format!("{}[{}][{}]", prefix, key, i),
139                            value,
140                        ));
141                    }
142                }
143                serde_json::Value::String(s) => {
144                    params.push((format!("{}[{}]", prefix, key), s.clone()))
145                }
146                _ => params.push((format!("{}[{}]", prefix, key), value.to_string())),
147            }
148        }
149
150        return params;
151    }
152
153    unimplemented!("Only objects are supported with style=deepObject, got: {value:#}")
154}
155
156/// Internal use only
157/// A content type supported by this client.
158#[allow(dead_code)]
159enum ContentType {
160    Json,
161    Text,
162    Unsupported(String),
163}
164
165impl From<&str> for ContentType {
166    fn from(content_type: &str) -> Self {
167        if content_type.starts_with("application") && content_type.contains("json") {
168            Self::Json
169        } else if content_type.starts_with("text/plain") {
170            Self::Text
171        } else {
172            Self::Unsupported(content_type.to_string())
173        }
174    }
175}
176
177pub mod account_api;
178pub mod authentication_api;
179pub mod client_applications_api;
180pub mod default_api;
181pub mod endpoints_api;
182pub mod engagements_api;
183pub mod quantum_processors_api;
184pub mod reservations_api;
185
186pub mod configuration;