use async_trait::async_trait;
use futures::{
future, future::BoxFuture, future::FutureExt, future::TryFutureExt, stream, stream::StreamExt,
Stream,
};
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use hyper::{service::Service, Body, Request, Response, Uri};
use percent_encoding::{utf8_percent_encode, AsciiSet};
use std::borrow::Cow;
use std::convert::TryInto;
use std::error::Error;
use std::fmt;
use std::future::Future;
use std::io::{ErrorKind, Read};
use std::marker::PhantomData;
use std::path::Path;
use std::str;
use std::str::FromStr;
use std::string::ToString;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use swagger::{ApiError, AuthData, BodyExt, Connector, DropContextService, Has, XSpanIdString};
use url::form_urlencoded;
use crate::header;
use crate::models;
#[allow(dead_code)]
const FRAGMENT_ENCODE_SET: &AsciiSet = &percent_encoding::CONTROLS
.add(b' ')
.add(b'"')
.add(b'<')
.add(b'>')
.add(b'`');
#[allow(dead_code)]
const ID_ENCODE_SET: &AsciiSet = &FRAGMENT_ENCODE_SET.add(b'|');
use crate::{
Api, GetTaskListResponse, GetTaskLogByIdResponse, PauseTaskByIdResponse,
ResumeTaskByIdResponse, StartTaskResponse, StopTaskByIdResponse,
};
fn into_base_path(
input: impl TryInto<Uri, Error = hyper::http::uri::InvalidUri>,
correct_scheme: Option<&'static str>,
) -> Result<String, ClientInitError> {
let uri = input.try_into()?;
let scheme = uri.scheme_str().ok_or(ClientInitError::InvalidScheme)?;
if let Some(correct_scheme) = correct_scheme {
if scheme != correct_scheme {
return Err(ClientInitError::InvalidScheme);
}
}
let host = uri.host().ok_or(ClientInitError::MissingHost)?;
let port = uri
.port_u16()
.map(|x| format!(":{}", x))
.unwrap_or_default();
Ok(format!(
"{}://{}{}{}",
scheme,
host,
port,
uri.path().trim_end_matches('/')
))
}
pub struct Client<S, C>
where
S: Service<(Request<Body>, C), Response = Response<Body>> + Clone + Sync + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<crate::ServiceError> + fmt::Display,
C: Clone + Send + Sync + 'static,
{
client_service: S,
base_path: String,
marker: PhantomData<fn(C)>,
}
impl<S, C> fmt::Debug for Client<S, C>
where
S: Service<(Request<Body>, C), Response = Response<Body>> + Clone + Sync + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<crate::ServiceError> + fmt::Display,
C: Clone + Send + Sync + 'static,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Client {{ base_path: {} }}", self.base_path)
}
}
impl<S, C> Clone for Client<S, C>
where
S: Service<(Request<Body>, C), Response = Response<Body>> + Clone + Sync + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<crate::ServiceError> + fmt::Display,
C: Clone + Send + Sync + 'static,
{
fn clone(&self) -> Self {
Self {
client_service: self.client_service.clone(),
base_path: self.base_path.clone(),
marker: PhantomData,
}
}
}
impl<Connector, C> Client<DropContextService<hyper::client::Client<Connector, Body>, C>, C>
where
Connector: hyper::client::connect::Connect + Clone + Send + Sync + 'static,
C: Clone + Send + Sync + 'static,
{
pub fn try_new_with_connector(
base_path: &str,
protocol: Option<&'static str>,
connector: Connector,
) -> Result<Self, ClientInitError> {
let client_service = hyper::client::Client::builder().build(connector);
let client_service = DropContextService::new(client_service);
Ok(Self {
client_service,
base_path: into_base_path(base_path, protocol)?,
marker: PhantomData,
})
}
}
#[derive(Debug, Clone)]
pub enum HyperClient {
Http(hyper::client::Client<hyper::client::HttpConnector, Body>),
Https(hyper::client::Client<HttpsConnector, Body>),
}
impl Service<Request<Body>> for HyperClient {
type Response = Response<Body>;
type Error = hyper::Error;
type Future = hyper::client::ResponseFuture;
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
match self {
HyperClient::Http(client) => client.poll_ready(cx),
HyperClient::Https(client) => client.poll_ready(cx),
}
}
fn call(&mut self, req: Request<Body>) -> Self::Future {
match self {
HyperClient::Http(client) => client.call(req),
HyperClient::Https(client) => client.call(req),
}
}
}
impl<C> Client<DropContextService<HyperClient, C>, C>
where
C: Clone + Send + Sync + 'static,
{
pub fn try_new(base_path: &str) -> Result<Self, ClientInitError> {
let uri = Uri::from_str(base_path)?;
let scheme = uri.scheme_str().ok_or(ClientInitError::InvalidScheme)?;
let scheme = scheme.to_ascii_lowercase();
let connector = Connector::builder();
let client_service = match scheme.as_str() {
"http" => HyperClient::Http(hyper::client::Client::builder().build(connector.build())),
"https" => {
let connector = connector
.https()
.build()
.map_err(ClientInitError::SslError)?;
HyperClient::Https(hyper::client::Client::builder().build(connector))
}
_ => {
return Err(ClientInitError::InvalidScheme);
}
};
let client_service = DropContextService::new(client_service);
Ok(Self {
client_service,
base_path: into_base_path(base_path, None)?,
marker: PhantomData,
})
}
}
impl<C> Client<DropContextService<hyper::client::Client<hyper::client::HttpConnector, Body>, C>, C>
where
C: Clone + Send + Sync + 'static,
{
pub fn try_new_http(base_path: &str) -> Result<Self, ClientInitError> {
let http_connector = Connector::builder().build();
Self::try_new_with_connector(base_path, Some("http"), http_connector)
}
}
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "ios"))]
type HttpsConnector = hyper_tls::HttpsConnector<hyper::client::HttpConnector>;
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
type HttpsConnector = hyper_openssl::HttpsConnector<hyper::client::HttpConnector>;
impl<C> Client<DropContextService<hyper::client::Client<HttpsConnector, Body>, C>, C>
where
C: Clone + Send + Sync + 'static,
{
pub fn try_new_https(base_path: &str) -> Result<Self, ClientInitError> {
let https_connector = Connector::builder()
.https()
.build()
.map_err(ClientInitError::SslError)?;
Self::try_new_with_connector(base_path, Some("https"), https_connector)
}
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
pub fn try_new_https_pinned<CA>(
base_path: &str,
ca_certificate: CA,
) -> Result<Self, ClientInitError>
where
CA: AsRef<Path>,
{
let https_connector = Connector::builder()
.https()
.pin_server_certificate(ca_certificate)
.build()
.map_err(ClientInitError::SslError)?;
Self::try_new_with_connector(base_path, Some("https"), https_connector)
}
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
pub fn try_new_https_mutual<CA, K, D>(
base_path: &str,
ca_certificate: CA,
client_key: K,
client_certificate: D,
) -> Result<Self, ClientInitError>
where
CA: AsRef<Path>,
K: AsRef<Path>,
D: AsRef<Path>,
{
let https_connector = Connector::builder()
.https()
.pin_server_certificate(ca_certificate)
.client_authentication(client_key, client_certificate)
.build()
.map_err(ClientInitError::SslError)?;
Self::try_new_with_connector(base_path, Some("https"), https_connector)
}
}
impl<S, C> Client<S, C>
where
S: Service<(Request<Body>, C), Response = Response<Body>> + Clone + Sync + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<crate::ServiceError> + fmt::Display,
C: Clone + Send + Sync + 'static,
{
pub fn try_new_with_client_service(
client_service: S,
base_path: &str,
) -> Result<Self, ClientInitError> {
Ok(Self {
client_service,
base_path: into_base_path(base_path, None)?,
marker: PhantomData,
})
}
}
#[derive(Debug)]
pub enum ClientInitError {
InvalidScheme,
InvalidUri(hyper::http::uri::InvalidUri),
MissingHost,
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "ios"))]
SslError(native_tls::Error),
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
SslError(openssl::error::ErrorStack),
}
impl From<hyper::http::uri::InvalidUri> for ClientInitError {
fn from(err: hyper::http::uri::InvalidUri) -> ClientInitError {
ClientInitError::InvalidUri(err)
}
}
impl fmt::Display for ClientInitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s: &dyn fmt::Debug = self;
s.fmt(f)
}
}
impl Error for ClientInitError {
fn description(&self) -> &str {
"Failed to produce a hyper client."
}
}
#[async_trait]
impl<S, C> Api<C> for Client<S, C>
where
S: Service<(Request<Body>, C), Response = Response<Body>> + Clone + Sync + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<crate::ServiceError> + fmt::Display,
C: Has<XSpanIdString> + Clone + Send + Sync + 'static,
{
fn poll_ready(&self, cx: &mut Context) -> Poll<Result<(), crate::ServiceError>> {
match self.client_service.clone().poll_ready(cx) {
Poll::Ready(Err(e)) => Poll::Ready(Err(e.into())),
Poll::Ready(Ok(o)) => Poll::Ready(Ok(o)),
Poll::Pending => Poll::Pending,
}
}
async fn get_task_list(&self, context: &C) -> Result<GetTaskListResponse, ApiError> {
let mut client_service = self.client_service.clone();
let mut uri = format!("{}/task", self.base_path);
let query_string = {
let mut query_string = form_urlencoded::Serializer::new("".to_owned());
query_string.finish()
};
if !query_string.is_empty() {
uri += "?";
uri += &query_string;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Err(ApiError(format!("Unable to build URI: {}", err))),
};
let mut request = match Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty())
{
Ok(req) => req,
Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))),
};
let header = HeaderValue::from_str(Has::<XSpanIdString>::get(context).0.as_str());
request.headers_mut().insert(
HeaderName::from_static("x-span-id"),
match header {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create X-Span ID header value: {}",
e
)))
}
},
);
let response = client_service
.call((request, context.clone()))
.map_err(|e| ApiError(format!("No response received: {}", e)))
.await?;
match response.status().as_u16() {
200 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::TaskListResponse>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(GetTaskListResponse::ListOfRunningTasks(body))
}
code => {
let headers = response.headers().clone();
let body = response.into_body().take(100).into_raw().await;
Err(ApiError(format!(
"Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(body) => match String::from_utf8(body) {
Ok(body) => body,
Err(e) => format!("<Body was not UTF8: {:?}>", e),
},
Err(e) => format!("<Failed to read body: {}>", e),
}
)))
}
}
}
async fn get_task_log_by_id(
&self,
param_get_task_log_request: models::GetTaskLogRequest,
context: &C,
) -> Result<GetTaskLogByIdResponse, ApiError> {
let mut client_service = self.client_service.clone();
let mut uri = format!("{}/log", self.base_path);
let query_string = {
let mut query_string = form_urlencoded::Serializer::new("".to_owned());
query_string.finish()
};
if !query_string.is_empty() {
uri += "?";
uri += &query_string;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Err(ApiError(format!("Unable to build URI: {}", err))),
};
let mut request = match Request::builder()
.method("POST")
.uri(uri)
.body(Body::empty())
{
Ok(req) => req,
Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))),
};
let body = serde_json::to_string(¶m_get_task_log_request)
.expect("impossible to fail to serialize");
*request.body_mut() = Body::from(body);
let header = "application/json";
request.headers_mut().insert(
CONTENT_TYPE,
match HeaderValue::from_str(header) {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create header: {} - {}",
header, e
)))
}
},
);
let header = HeaderValue::from_str(Has::<XSpanIdString>::get(context).0.as_str());
request.headers_mut().insert(
HeaderName::from_static("x-span-id"),
match header {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create X-Span ID header value: {}",
e
)))
}
},
);
let response = client_service
.call((request, context.clone()))
.map_err(|e| ApiError(format!("No response received: {}", e)))
.await?;
match response.status().as_u16() {
404 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::GeneralError>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(GetTaskLogByIdResponse::InvalidHandle(body))
}
200 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<Vec<models::GetTaskLogResponseInner>>(body)
.map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(GetTaskLogByIdResponse::TheLogFetched(body))
}
code => {
let headers = response.headers().clone();
let body = response.into_body().take(100).into_raw().await;
Err(ApiError(format!(
"Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(body) => match String::from_utf8(body) {
Ok(body) => body,
Err(e) => format!("<Body was not UTF8: {:?}>", e),
},
Err(e) => format!("<Failed to read body: {}>", e),
}
)))
}
}
}
async fn pause_task_by_id(
&self,
param_simple_id_request: models::SimpleIdRequest,
context: &C,
) -> Result<PauseTaskByIdResponse, ApiError> {
let mut client_service = self.client_service.clone();
let mut uri = format!("{}/pause", self.base_path);
let query_string = {
let mut query_string = form_urlencoded::Serializer::new("".to_owned());
query_string.finish()
};
if !query_string.is_empty() {
uri += "?";
uri += &query_string;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Err(ApiError(format!("Unable to build URI: {}", err))),
};
let mut request = match Request::builder()
.method("POST")
.uri(uri)
.body(Body::empty())
{
Ok(req) => req,
Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))),
};
let body = serde_json::to_string(¶m_simple_id_request)
.expect("impossible to fail to serialize");
*request.body_mut() = Body::from(body);
let header = "application/json";
request.headers_mut().insert(
CONTENT_TYPE,
match HeaderValue::from_str(header) {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create header: {} - {}",
header, e
)))
}
},
);
let header = HeaderValue::from_str(Has::<XSpanIdString>::get(context).0.as_str());
request.headers_mut().insert(
HeaderName::from_static("x-span-id"),
match header {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create X-Span ID header value: {}",
e
)))
}
},
);
let response = client_service
.call((request, context.clone()))
.map_err(|e| ApiError(format!("No response received: {}", e)))
.await?;
match response.status().as_u16() {
400 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::GeneralError>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(PauseTaskByIdResponse::FailedToPause(body))
}
404 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::GeneralError>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(PauseTaskByIdResponse::InvalidHandle(body))
}
200 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::TaskStatus>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(PauseTaskByIdResponse::StatusOfPausingTheTask(body))
}
code => {
let headers = response.headers().clone();
let body = response.into_body().take(100).into_raw().await;
Err(ApiError(format!(
"Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(body) => match String::from_utf8(body) {
Ok(body) => body,
Err(e) => format!("<Body was not UTF8: {:?}>", e),
},
Err(e) => format!("<Failed to read body: {}>", e),
}
)))
}
}
}
async fn resume_task_by_id(
&self,
param_simple_id_request: models::SimpleIdRequest,
context: &C,
) -> Result<ResumeTaskByIdResponse, ApiError> {
let mut client_service = self.client_service.clone();
let mut uri = format!("{}/resume", self.base_path);
let query_string = {
let mut query_string = form_urlencoded::Serializer::new("".to_owned());
query_string.finish()
};
if !query_string.is_empty() {
uri += "?";
uri += &query_string;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Err(ApiError(format!("Unable to build URI: {}", err))),
};
let mut request = match Request::builder()
.method("POST")
.uri(uri)
.body(Body::empty())
{
Ok(req) => req,
Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))),
};
let body = serde_json::to_string(¶m_simple_id_request)
.expect("impossible to fail to serialize");
*request.body_mut() = Body::from(body);
let header = "application/json";
request.headers_mut().insert(
CONTENT_TYPE,
match HeaderValue::from_str(header) {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create header: {} - {}",
header, e
)))
}
},
);
let header = HeaderValue::from_str(Has::<XSpanIdString>::get(context).0.as_str());
request.headers_mut().insert(
HeaderName::from_static("x-span-id"),
match header {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create X-Span ID header value: {}",
e
)))
}
},
);
let response = client_service
.call((request, context.clone()))
.map_err(|e| ApiError(format!("No response received: {}", e)))
.await?;
match response.status().as_u16() {
400 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::GeneralError>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(ResumeTaskByIdResponse::FailedToResume(body))
}
404 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::GeneralError>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(ResumeTaskByIdResponse::InvalidHandle(body))
}
200 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::TaskStatus>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(ResumeTaskByIdResponse::StatusOfTheTask(body))
}
code => {
let headers = response.headers().clone();
let body = response.into_body().take(100).into_raw().await;
Err(ApiError(format!(
"Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(body) => match String::from_utf8(body) {
Ok(body) => body,
Err(e) => format!("<Body was not UTF8: {:?}>", e),
},
Err(e) => format!("<Failed to read body: {}>", e),
}
)))
}
}
}
async fn start_task(
&self,
param_start_task_request: models::StartTaskRequest,
context: &C,
) -> Result<StartTaskResponse, ApiError> {
let mut client_service = self.client_service.clone();
let mut uri = format!("{}/task", self.base_path);
let query_string = {
let mut query_string = form_urlencoded::Serializer::new("".to_owned());
query_string.finish()
};
if !query_string.is_empty() {
uri += "?";
uri += &query_string;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Err(ApiError(format!("Unable to build URI: {}", err))),
};
let mut request = match Request::builder()
.method("POST")
.uri(uri)
.body(Body::empty())
{
Ok(req) => req,
Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))),
};
let body = serde_json::to_string(¶m_start_task_request)
.expect("impossible to fail to serialize");
*request.body_mut() = Body::from(body);
let header = "application/json";
request.headers_mut().insert(
CONTENT_TYPE,
match HeaderValue::from_str(header) {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create header: {} - {}",
header, e
)))
}
},
);
let header = HeaderValue::from_str(Has::<XSpanIdString>::get(context).0.as_str());
request.headers_mut().insert(
HeaderName::from_static("x-span-id"),
match header {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create X-Span ID header value: {}",
e
)))
}
},
);
let response = client_service
.call((request, context.clone()))
.map_err(|e| ApiError(format!("No response received: {}", e)))
.await?;
match response.status().as_u16() {
400 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::GeneralError>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(StartTaskResponse::InvalidArguments(body))
}
200 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body =
serde_json::from_str::<models::StartTask200Response>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(StartTaskResponse::ListOfRunningTasks(body))
}
code => {
let headers = response.headers().clone();
let body = response.into_body().take(100).into_raw().await;
Err(ApiError(format!(
"Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(body) => match String::from_utf8(body) {
Ok(body) => body,
Err(e) => format!("<Body was not UTF8: {:?}>", e),
},
Err(e) => format!("<Failed to read body: {}>", e),
}
)))
}
}
}
async fn stop_task_by_id(
&self,
param_simple_id_request: models::SimpleIdRequest,
context: &C,
) -> Result<StopTaskByIdResponse, ApiError> {
let mut client_service = self.client_service.clone();
let mut uri = format!("{}/stop", self.base_path);
let query_string = {
let mut query_string = form_urlencoded::Serializer::new("".to_owned());
query_string.finish()
};
if !query_string.is_empty() {
uri += "?";
uri += &query_string;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Err(ApiError(format!("Unable to build URI: {}", err))),
};
let mut request = match Request::builder()
.method("POST")
.uri(uri)
.body(Body::empty())
{
Ok(req) => req,
Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))),
};
let body = serde_json::to_string(¶m_simple_id_request)
.expect("impossible to fail to serialize");
*request.body_mut() = Body::from(body);
let header = "application/json";
request.headers_mut().insert(
CONTENT_TYPE,
match HeaderValue::from_str(header) {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create header: {} - {}",
header, e
)))
}
},
);
let header = HeaderValue::from_str(Has::<XSpanIdString>::get(context).0.as_str());
request.headers_mut().insert(
HeaderName::from_static("x-span-id"),
match header {
Ok(h) => h,
Err(e) => {
return Err(ApiError(format!(
"Unable to create X-Span ID header value: {}",
e
)))
}
},
);
let response = client_service
.call((request, context.clone()))
.map_err(|e| ApiError(format!("No response received: {}", e)))
.await?;
match response.status().as_u16() {
200 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<serde_json::Value>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(StopTaskByIdResponse::StatusOfStoppingTheTask(body))
}
404 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::GeneralError>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(StopTaskByIdResponse::InvalidHandle(body))
}
500 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body = serde_json::from_str::<models::GeneralError>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(StopTaskByIdResponse::FailedToTerminate(body))
}
code => {
let headers = response.headers().clone();
let body = response.into_body().take(100).into_raw().await;
Err(ApiError(format!(
"Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(body) => match String::from_utf8(body) {
Ok(body) => body,
Err(e) => format!("<Body was not UTF8: {:?}>", e),
},
Err(e) => format!("<Failed to read body: {}>", e),
}
)))
}
}
}
}