Skip to content

Commit

Permalink
update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Aug 11, 2022
1 parent e58b8b1 commit 5662d61
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 390 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn into_base_path(input: impl TryInto<Uri, Error=hyper::http::uri::InvalidUri>,
}
}

let host = uri.host().ok_or_else(|| ClientInitError::MissingHost)?;
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('/')))
}
Expand Down Expand Up @@ -199,7 +199,7 @@ impl<C> Client<DropContextService<HyperClient, C>, C> where
"https" => {
let connector = connector.https()
.build()
.map_err(|e| ClientInitError::SslError(e))?;
.map_err(ClientInitError::SslError)?;
HyperClient::Https(hyper::client::Client::builder().build(connector))
},
_ => {
Expand Down Expand Up @@ -251,7 +251,7 @@ impl<C> Client<DropContextService<hyper::client::Client<HttpsConnector, Body>, C
let https_connector = Connector::builder()
.https()
.build()
.map_err(|e| ClientInitError::SslError(e))?;
.map_err(ClientInitError::SslError)?;
Self::try_new_with_connector(base_path, Some("https"), https_connector)
}

Expand All @@ -272,7 +272,7 @@ impl<C> Client<DropContextService<hyper::client::Client<HttpsConnector, Body>, C
.https()
.pin_server_certificate(ca_certificate)
.build()
.map_err(|e| ClientInitError::SslError(e))?;
.map_err(ClientInitError::SslError)?;
Self::try_new_with_connector(base_path, Some("https"), https_connector)
}

Expand Down Expand Up @@ -300,7 +300,7 @@ impl<C> Client<DropContextService<hyper::client::Client<HttpsConnector, Body>, C
.pin_server_certificate(ca_certificate)
.client_authentication(client_key, client_certificate)
.build()
.map_err(|e| ClientInitError::SslError(e))?;
.map_err(ClientInitError::SslError)?;
Self::try_new_with_connector(base_path, Some("https"), https_connector)
}
}
Expand Down Expand Up @@ -494,18 +494,17 @@ impl<S, C> Api<C> for Client<S, C> where
Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e)))
});

let header = HeaderValue::from_str(Has::<XSpanIdString>::get(context).0.clone().to_string().as_str());
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 mut response = client_service.call((request, context.clone()))
let response = client_service.call((request, context.clone()))
.map_err(|e| ApiError(format!("No response received: {}", e))).await?;

match response.status().as_u16() {
201 => {
let body = response.into_body();
Ok(
MultipartRelatedRequestPostResponse::OK
)
Expand Down Expand Up @@ -647,18 +646,17 @@ impl<S, C> Api<C> for Client<S, C> where
Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", multipart_header, e)))
});

let header = HeaderValue::from_str(Has::<XSpanIdString>::get(context).0.clone().to_string().as_str());
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 mut response = client_service.call((request, context.clone()))
let response = client_service.call((request, context.clone()))
.map_err(|e| ApiError(format!("No response received: {}", e))).await?;

match response.status().as_u16() {
201 => {
let body = response.into_body();
Ok(
MultipartRequestPostResponse::OK
)
Expand Down Expand Up @@ -774,18 +772,17 @@ impl<S, C> Api<C> for Client<S, C> where
Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e)))
});

let header = HeaderValue::from_str(Has::<XSpanIdString>::get(context).0.clone().to_string().as_str());
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 mut response = client_service.call((request, context.clone()))
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();
Ok(
MultipleIdenticalMimeTypesPostResponse::OK
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(missing_docs, trivial_casts, unused_variables, unused_mut, unused_imports, unused_extern_crates, non_camel_case_types)]
#![allow(unused_imports)]

use async_trait::async_trait;
use futures::Stream;
Expand All @@ -9,8 +10,8 @@ use serde::{Serialize, Deserialize};

type ServiceError = Box<dyn Error + Send + Sync + 'static>;

pub const BASE_PATH: &'static str = "";
pub const API_VERSION: &'static str = "1.0.7";
pub const BASE_PATH: &str = "";
pub const API_VERSION: &str = "1.0.7";

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub enum MultipartRelatedRequestPostResponse {
Expand Down Expand Up @@ -95,7 +96,7 @@ pub trait ApiNoContext<C: Send + Sync> {
pub trait ContextWrapperExt<C: Send + Sync> where Self: Sized
{
/// Binds this API to a context.
fn with_context(self: Self, context: C) -> ContextWrapper<Self, C>;
fn with_context(self, context: C) -> ContextWrapper<Self, C>;
}

impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ContextWrapperExt<C> for T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn into_base_path(input: impl TryInto<Uri, Error=hyper::http::uri::InvalidUri>,
}
}

let host = uri.host().ok_or_else(|| ClientInitError::MissingHost)?;
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('/')))
}
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<C> Client<DropContextService<HyperClient, C>, C> where
"https" => {
let connector = connector.https()
.build()
.map_err(|e| ClientInitError::SslError(e))?;
.map_err(ClientInitError::SslError)?;
HyperClient::Https(hyper::client::Client::builder().build(connector))
},
_ => {
Expand Down Expand Up @@ -244,7 +244,7 @@ impl<C> Client<DropContextService<hyper::client::Client<HttpsConnector, Body>, C
let https_connector = Connector::builder()
.https()
.build()
.map_err(|e| ClientInitError::SslError(e))?;
.map_err(ClientInitError::SslError)?;
Self::try_new_with_connector(base_path, Some("https"), https_connector)
}

Expand All @@ -265,7 +265,7 @@ impl<C> Client<DropContextService<hyper::client::Client<HttpsConnector, Body>, C
.https()
.pin_server_certificate(ca_certificate)
.build()
.map_err(|e| ClientInitError::SslError(e))?;
.map_err(ClientInitError::SslError)?;
Self::try_new_with_connector(base_path, Some("https"), https_connector)
}

Expand Down Expand Up @@ -293,7 +293,7 @@ impl<C> Client<DropContextService<hyper::client::Client<HttpsConnector, Body>, C
.pin_server_certificate(ca_certificate)
.client_authentication(client_key, client_certificate)
.build()
.map_err(|e| ClientInitError::SslError(e))?;
.map_err(ClientInitError::SslError)?;
Self::try_new_with_connector(base_path, Some("https"), https_connector)
}
}
Expand Down Expand Up @@ -425,18 +425,17 @@ impl<S, C> Api<C> for Client<S, C> where
Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e)))
});

let header = HeaderValue::from_str(Has::<XSpanIdString>::get(context).0.clone().to_string().as_str());
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 mut response = client_service.call((request, context.clone()))
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();
Ok(
OpGetResponse::OK
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(missing_docs, trivial_casts, unused_variables, unused_mut, unused_imports, unused_extern_crates, non_camel_case_types)]
#![allow(unused_imports)]

use async_trait::async_trait;
use futures::Stream;
Expand All @@ -9,8 +10,8 @@ use serde::{Serialize, Deserialize};

type ServiceError = Box<dyn Error + Send + Sync + 'static>;

pub const BASE_PATH: &'static str = "";
pub const API_VERSION: &'static str = "0.0.1";
pub const BASE_PATH: &str = "";
pub const API_VERSION: &str = "0.0.1";

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub enum OpGetResponse {
Expand Down Expand Up @@ -51,7 +52,7 @@ pub trait ApiNoContext<C: Send + Sync> {
pub trait ContextWrapperExt<C: Send + Sync> where Self: Sized
{
/// Binds this API to a context.
fn with_context(self: Self, context: C) -> ContextWrapper<Self, C>;
fn with_context(self, context: C) -> ContextWrapper<Self, C>;
}

impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ContextWrapperExt<C> for T {
Expand Down
Loading

0 comments on commit 5662d61

Please sign in to comment.