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_json::Error),
30    Io(std::io::Error),
31    QcsToken(crate::common::configuration::TokenError),
32    ResponseError(ResponseContent<T>),
33    #[cfg(feature = "otel-tracing")]
34    ReqwestMiddleware(anyhow::Error),
35}
36
37impl<T> Error<T> {
38    pub fn status_code(&self) -> Option<reqwest::StatusCode> {
39        match self {
40            Self::ResponseError(err) => Some(err.status),
41            _ => None,
42        }
43    }
44}
45
46impl<T> fmt::Display for Error<T> {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        let (module, e) = match self {
49            Error::Reqwest(e) => ("reqwest", e.to_string()),
50            Error::Serde(e) => ("serde", e.to_string()),
51            Error::Io(e) => ("IO", e.to_string()),
52            Error::QcsToken(e) => ("refresh_qcs_token", e.to_string()),
53            Error::ResponseError(e) => (
54                "response",
55                format!("status code {}: {}", e.status, e.content),
56            ),
57            #[cfg(feature = "otel-tracing")]
58            Error::ReqwestMiddleware(e) => ("reqwest-middleware", e.to_string()),
59        };
60        write!(f, "error in {}: {}", module, e)
61    }
62}
63
64impl<T: fmt::Debug> error::Error for Error<T> {
65    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
66        Some(match self {
67            Error::Reqwest(e) => e,
68            Error::Serde(e) => e,
69            Error::Io(e) => e,
70            Error::QcsToken(e) => e,
71            #[cfg(feature = "otel-tracing")]
72            Error::ReqwestMiddleware(e) => e.source()?,
73            Error::ResponseError(_) => return None,
74        })
75    }
76}
77
78impl<T> From<reqwest::Error> for Error<T> {
79    fn from(e: reqwest::Error) -> Self {
80        Error::Reqwest(e)
81    }
82}
83
84#[cfg(feature = "otel-tracing")]
85impl<T> From<reqwest_middleware::Error> for Error<T> {
86    fn from(e: reqwest_middleware::Error) -> Self {
87        match e {
88            reqwest_middleware::Error::Reqwest(e) => Error::Reqwest(e),
89            reqwest_middleware::Error::Middleware(e) => Error::ReqwestMiddleware(e),
90        }
91    }
92}
93
94impl<T> From<serde_json::Error> for Error<T> {
95    fn from(e: serde_json::Error) -> Self {
96        Error::Serde(e)
97    }
98}
99
100impl<T> From<std::io::Error> for Error<T> {
101    fn from(e: std::io::Error) -> Self {
102        Error::Io(e)
103    }
104}
105
106impl<T> From<crate::common::configuration::TokenError> for Error<T> {
107    fn from(e: crate::common::configuration::TokenError) -> Self {
108        Error::QcsToken(e)
109    }
110}
111
112pub fn urlencode<T: AsRef<str>>(s: T) -> String {
113    ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
114}
115
116pub mod account_api;
117pub mod authentication_api;
118pub mod client_applications_api;
119pub mod default_api;
120pub mod endpoints_api;
121pub mod engagements_api;
122pub mod quantum_processors_api;
123pub mod reservations_api;
124
125pub mod configuration;