use futures;
use futures::{Future, Stream, future, stream};
use hyper;
use hyper::client::HttpConnector;
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use hyper::{Body, Uri, Response};
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
use hyper_openssl::HttpsConnector;
use serde_json;
use std::borrow::Cow;
use std::convert::TryInto;
use std::io::{Read, Error, ErrorKind};
use std::error;
use std::fmt;
use std::path::Path;
use std::sync::Arc;
use std::str;
use std::str::FromStr;
use std::string::ToString;
use swagger;
use swagger::{ApiError, Connector, client::Service, XSpanIdString, Has, AuthData};
use url::form_urlencoded;
use url::percent_encoding::{utf8_percent_encode, PATH_SEGMENT_ENCODE_SET, QUERY_ENCODE_SET};
use crate::models;
use crate::header;
url::define_encode_set! {
pub ID_ENCODE_SET = [PATH_SEGMENT_ENCODE_SET] | {'|'}
}
use crate::{Api,
I2cBusApiResponse,
I2cBusListResponse,
I2cBusReadByteResponse,
I2cBusReadBytesResponse,
I2cBusReadRegResponse,
I2cBusWriteByteResponse,
I2cBusWriteByteRegResponse,
I2cBusWriteBytesResponse,
I2cBusWriteBytesRegResponse
};
fn into_base_path(input: &str, correct_scheme: Option<&'static str>) -> Result<String, ClientInitError> {
let uri = Uri::from_str(input)?;
let scheme = uri.scheme_part().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_else(|| ClientInitError::MissingHost)?;
let port = uri.port_part().map(|x| format!(":{}", x)).unwrap_or_default();
Ok(format!("{}://{}{}{}", scheme, host, port, uri.path().trim_end_matches('/')))
}
pub struct Client<F>
{
client_service: Arc<Box<dyn Service<ReqBody=Body, Future=F> + Send + Sync>>,
base_path: String,
}
impl<F> fmt::Debug for Client<F>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Client {{ base_path: {} }}", self.base_path)
}
}
impl<F> Clone for Client<F>
{
fn clone(&self) -> Self {
Client {
client_service: self.client_service.clone(),
base_path: self.base_path.clone(),
}
}
}
impl Client<hyper::client::ResponseFuture>
{
pub fn try_new_with_connector<C>(
base_path: &str,
protocol: Option<&'static str>,
connector: C,
) -> Result<Self, ClientInitError> where
C: hyper::client::connect::Connect + 'static,
C::Transport: 'static,
C::Future: 'static,
{
let client_service = Box::new(hyper::client::Client::builder().build(connector));
Ok(Client {
client_service: Arc::new(client_service),
base_path: into_base_path(base_path, protocol)?,
})
}
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)
}
pub fn try_new_https(base_path: &str) -> Result<Self, ClientInitError>
{
let https_connector = Connector::builder()
.https()
.build()
.map_err(|e| ClientInitError::SslError(e))?;
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(|e| ClientInitError::SslError(e))?;
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(|e| ClientInitError::SslError(e))?;
Self::try_new_with_connector(base_path, Some("https"), https_connector)
}
}
impl<F> Client<F>
{
pub fn try_new_with_client_service(
client_service: Arc<Box<dyn Service<ReqBody=Body, Future=F> + Send + Sync>>,
base_path: &str,
) -> Result<Self, ClientInitError> {
Ok(Client {
client_service: client_service,
base_path: into_base_path(base_path, None)?,
})
}
}
#[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::Error for ClientInitError {
fn description(&self) -> &str {
"Failed to produce a hyper client."
}
}
impl<C, F> Api<C> for Client<F> where
C: Has<XSpanIdString> ,
F: Future<Item=Response<Body>, Error=hyper::Error> + Send + 'static
{
fn i2c_bus_api(
&self,
context: &C) -> Box<dyn Future<Item=I2cBusApiResponse, Error=ApiError> + Send>
{
let mut uri = format!(
"{}/i2c/api",
self.base_path
);
let mut query_string = url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(future::err(ApiError(format!("Unable to build URI: {}", err)))),
};
let mut request = match hyper::Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create request: {}", e))))
};
let header = HeaderValue::from_str((context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str());
request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header {
Ok(h) => h,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create X-Span ID header value: {}", e))))
});
Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
200 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
Ok(body.to_string())
)
)
.map(move |body| {
I2cBusApiResponse::OK
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
404 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
Ok(body.to_string())
)
)
.map(move |body| {
I2cBusApiResponse::FileNotFound
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
code => {
let headers = response.headers().clone();
Box::new(response.into_body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<dyn Future<Item=_, Error=_> + Send>
}
}
}))
}
fn i2c_bus_list(
&self,
context: &C) -> Box<dyn Future<Item=I2cBusListResponse, Error=ApiError> + Send>
{
let mut uri = format!(
"{}/i2c/buslist",
self.base_path
);
let mut query_string = url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(future::err(ApiError(format!("Unable to build URI: {}", err)))),
};
let mut request = match hyper::Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create request: {}", e))))
};
let header = HeaderValue::from_str((context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str());
request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header {
Ok(h) => h,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create X-Span ID header value: {}", e))))
});
Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
200 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<Vec<models::I2cBusList>>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusListResponse::OK
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
code => {
let headers = response.headers().clone();
Box::new(response.into_body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<dyn Future<Item=_, Error=_> + Send>
}
}
}))
}
fn i2c_bus_read_byte(
&self,
param_bus_id: i32,
param_addr: i32,
context: &C) -> Box<dyn Future<Item=I2cBusReadByteResponse, Error=ApiError> + Send>
{
let mut uri = format!(
"{}/i2c/{bus_id}/read/byte/{addr}",
self.base_path
,bus_id=utf8_percent_encode(¶m_bus_id.to_string(), ID_ENCODE_SET)
,addr=utf8_percent_encode(¶m_addr.to_string(), ID_ENCODE_SET)
);
let mut query_string = url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(future::err(ApiError(format!("Unable to build URI: {}", err)))),
};
let mut request = match hyper::Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create request: {}", e))))
};
let header = HeaderValue::from_str((context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str());
request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header {
Ok(h) => h,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create X-Span ID header value: {}", e))))
});
Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
200 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusRead>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusReadByteResponse::OK
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
400 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusArg>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusReadByteResponse::BadRequest
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
502 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusError>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusReadByteResponse::TransactionFailed
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
code => {
let headers = response.headers().clone();
Box::new(response.into_body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<dyn Future<Item=_, Error=_> + Send>
}
}
}))
}
fn i2c_bus_read_bytes(
&self,
param_bus_id: i32,
param_addr: i32,
param_num_bytes: i32,
context: &C) -> Box<dyn Future<Item=I2cBusReadBytesResponse, Error=ApiError> + Send>
{
let mut uri = format!(
"{}/i2c/{bus_id}/read/bytes/{addr}/{num_bytes}",
self.base_path
,bus_id=utf8_percent_encode(¶m_bus_id.to_string(), ID_ENCODE_SET)
,addr=utf8_percent_encode(¶m_addr.to_string(), ID_ENCODE_SET)
,num_bytes=utf8_percent_encode(¶m_num_bytes.to_string(), ID_ENCODE_SET)
);
let mut query_string = url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(future::err(ApiError(format!("Unable to build URI: {}", err)))),
};
let mut request = match hyper::Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create request: {}", e))))
};
let header = HeaderValue::from_str((context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str());
request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header {
Ok(h) => h,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create X-Span ID header value: {}", e))))
});
Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
200 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusRead>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusReadBytesResponse::OK
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
400 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusArg>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusReadBytesResponse::BadRequest
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
502 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusError>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusReadBytesResponse::TransactionFailed
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
code => {
let headers = response.headers().clone();
Box::new(response.into_body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<dyn Future<Item=_, Error=_> + Send>
}
}
}))
}
fn i2c_bus_read_reg(
&self,
param_bus_id: i32,
param_addr: i32,
param_reg: i32,
param_num_bytes: i32,
context: &C) -> Box<dyn Future<Item=I2cBusReadRegResponse, Error=ApiError> + Send>
{
let mut uri = format!(
"{}/i2c/{bus_id}/read/reg/{addr}/{reg}/{num_bytes}",
self.base_path
,bus_id=utf8_percent_encode(¶m_bus_id.to_string(), ID_ENCODE_SET)
,addr=utf8_percent_encode(¶m_addr.to_string(), ID_ENCODE_SET)
,reg=utf8_percent_encode(¶m_reg.to_string(), ID_ENCODE_SET)
,num_bytes=utf8_percent_encode(¶m_num_bytes.to_string(), ID_ENCODE_SET)
);
let mut query_string = url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(future::err(ApiError(format!("Unable to build URI: {}", err)))),
};
let mut request = match hyper::Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create request: {}", e))))
};
let header = HeaderValue::from_str((context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str());
request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header {
Ok(h) => h,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create X-Span ID header value: {}", e))))
});
Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
200 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusRead>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusReadRegResponse::OK
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
400 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusArg>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusReadRegResponse::BadRequest
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
502 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusError>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusReadRegResponse::TransactionFailed
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
code => {
let headers = response.headers().clone();
Box::new(response.into_body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<dyn Future<Item=_, Error=_> + Send>
}
}
}))
}
fn i2c_bus_write_byte(
&self,
param_bus_id: i32,
param_addr: i32,
param_value: i32,
context: &C) -> Box<dyn Future<Item=I2cBusWriteByteResponse, Error=ApiError> + Send>
{
let mut uri = format!(
"{}/i2c/{bus_id}/write/byte/{addr}/{value}",
self.base_path
,bus_id=utf8_percent_encode(¶m_bus_id.to_string(), ID_ENCODE_SET)
,addr=utf8_percent_encode(¶m_addr.to_string(), ID_ENCODE_SET)
,value=utf8_percent_encode(¶m_value.to_string(), ID_ENCODE_SET)
);
let mut query_string = url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(future::err(ApiError(format!("Unable to build URI: {}", err)))),
};
let mut request = match hyper::Request::builder()
.method("POST")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create request: {}", e))))
};
let header = HeaderValue::from_str((context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str());
request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header {
Ok(h) => h,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create X-Span ID header value: {}", e))))
});
Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
200 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusOk>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteByteResponse::OK
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
400 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusArg>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteByteResponse::BadRequest
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
502 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusError>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteByteResponse::TransactionFailed
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
code => {
let headers = response.headers().clone();
Box::new(response.into_body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<dyn Future<Item=_, Error=_> + Send>
}
}
}))
}
fn i2c_bus_write_byte_reg(
&self,
param_bus_id: i32,
param_addr: i32,
param_reg: i32,
param_value: i32,
context: &C) -> Box<dyn Future<Item=I2cBusWriteByteRegResponse, Error=ApiError> + Send>
{
let mut uri = format!(
"{}/i2c/{bus_id}/write/byte/reg/{addr}/{reg}/{value}",
self.base_path
,bus_id=utf8_percent_encode(¶m_bus_id.to_string(), ID_ENCODE_SET)
,addr=utf8_percent_encode(¶m_addr.to_string(), ID_ENCODE_SET)
,reg=utf8_percent_encode(¶m_reg.to_string(), ID_ENCODE_SET)
,value=utf8_percent_encode(¶m_value.to_string(), ID_ENCODE_SET)
);
let mut query_string = url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(future::err(ApiError(format!("Unable to build URI: {}", err)))),
};
let mut request = match hyper::Request::builder()
.method("POST")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create request: {}", e))))
};
let header = HeaderValue::from_str((context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str());
request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header {
Ok(h) => h,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create X-Span ID header value: {}", e))))
});
Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
200 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusOk>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteByteRegResponse::OK
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
400 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusArg>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteByteRegResponse::BadRequest
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
502 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusError>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteByteRegResponse::TransactionFailed
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
code => {
let headers = response.headers().clone();
Box::new(response.into_body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<dyn Future<Item=_, Error=_> + Send>
}
}
}))
}
fn i2c_bus_write_bytes(
&self,
param_bus_id: i32,
param_addr: i32,
param_values: models::Values,
context: &C) -> Box<dyn Future<Item=I2cBusWriteBytesResponse, Error=ApiError> + Send>
{
let mut uri = format!(
"{}/i2c/{bus_id}/write/bytes/{addr}",
self.base_path
,bus_id=utf8_percent_encode(¶m_bus_id.to_string(), ID_ENCODE_SET)
,addr=utf8_percent_encode(¶m_addr.to_string(), ID_ENCODE_SET)
);
let mut query_string = url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(future::err(ApiError(format!("Unable to build URI: {}", err)))),
};
let mut request = match hyper::Request::builder()
.method("POST")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create request: {}", e))))
};
let body = serde_json::to_string(¶m_values).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 Box::new(future::err(ApiError(format!("Unable to create header: {} - {}", header, e))))
});
let header = HeaderValue::from_str((context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str());
request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header {
Ok(h) => h,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create X-Span ID header value: {}", e))))
});
Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
200 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusOk>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteBytesResponse::OK
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
400 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusArg>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteBytesResponse::BadRequest
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
502 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusError>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteBytesResponse::TransactionFailed
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
code => {
let headers = response.headers().clone();
Box::new(response.into_body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<dyn Future<Item=_, Error=_> + Send>
}
}
}))
}
fn i2c_bus_write_bytes_reg(
&self,
param_bus_id: i32,
param_addr: i32,
param_reg: i32,
param_values: models::Values,
context: &C) -> Box<dyn Future<Item=I2cBusWriteBytesRegResponse, Error=ApiError> + Send>
{
let mut uri = format!(
"{}/i2c/{bus_id}/write/bytes/reg/{addr}/{reg}",
self.base_path
,bus_id=utf8_percent_encode(¶m_bus_id.to_string(), ID_ENCODE_SET)
,addr=utf8_percent_encode(¶m_addr.to_string(), ID_ENCODE_SET)
,reg=utf8_percent_encode(¶m_reg.to_string(), ID_ENCODE_SET)
);
let mut query_string = url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(future::err(ApiError(format!("Unable to build URI: {}", err)))),
};
let mut request = match hyper::Request::builder()
.method("POST")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create request: {}", e))))
};
let body = serde_json::to_string(¶m_values).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 Box::new(future::err(ApiError(format!("Unable to create header: {} - {}", header, e))))
});
let header = HeaderValue::from_str((context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str());
request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header {
Ok(h) => h,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create X-Span ID header value: {}", e))))
});
Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
200 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusOk>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteBytesRegResponse::OK
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
400 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusArg>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteBytesRegResponse::BadRequest
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
502 => {
let body = response.into_body();
Box::new(
body
.concat2()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.and_then(|body|
str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
.and_then(|body|
serde_json::from_str::<models::I2cBusError>(body)
.map_err(|e| e.into())
)
)
.map(move |body| {
I2cBusWriteBytesRegResponse::TransactionFailed
(body)
})
) as Box<dyn Future<Item=_, Error=_> + Send>
},
code => {
let headers = response.headers().clone();
Box::new(response.into_body()
.take(100)
.concat2()
.then(move |body|
future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(ref body) => match str::from_utf8(body) {
Ok(body) => Cow::from(body),
Err(e) => Cow::from(format!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<dyn Future<Item=_, Error=_> + Send>
}
}
}))
}
}