#![allow(unused_extern_crates)] extern crate chrono; extern crate hyper_tls; extern crate mime; extern crate native_tls; extern crate openssl; extern crate tokio_core; extern crate url; use self::tokio_core::reactor::Handle; use self::url::percent_encoding::{utf8_percent_encode, PATH_SEGMENT_ENCODE_SET, QUERY_ENCODE_SET}; use futures; use futures::{Future, Stream}; use futures::{future, stream}; use hyper; use hyper::Uri; use hyper::header::{ContentType, Headers}; use std::borrow::Cow; use std::error; use std::fmt; use std::io::{Error, ErrorKind, Read}; use std::path::Path; use std::str; use std::str::FromStr; use std::sync::Arc; use mimetypes; use serde_json; #[allow(unused_imports)] use std::collections::{BTreeMap, HashMap}; #[allow(unused_imports)] use swagger; use swagger::{ApiError, Context, XSpanId}; use models; use {Api, ContainerIdGetResponse, ContainerLookupGetResponse, ContainerPostResponse, CreatorIdGetResponse, CreatorLookupGetResponse, CreatorPostResponse, EditgroupIdAcceptPostResponse, EditgroupIdGetResponse, EditgroupPostResponse, EditorUsernameChangelogGetResponse, EditorUsernameGetResponse, FileIdGetResponse, FileLookupGetResponse, FilePostResponse, ReleaseIdGetResponse, ReleaseLookupGetResponse, ReleasePostResponse, WorkIdGetResponse, WorkPostResponse}; /// Convert input into a base path, e.g. "http://example:123". Also checks the scheme as it goes. fn into_base_path( input: &str, correct_scheme: Option<&'static str>, ) -> Result { // First convert to Uri, since a base path is a subset of Uri. let uri = Uri::from_str(input)?; let scheme = uri.scheme().ok_or(ClientInitError::InvalidScheme)?; // Check the scheme if necessary 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().map(|x| format!(":{}", x)).unwrap_or_default(); Ok(format!("{}://{}{}", scheme, host, port)) } /// A client that implements the API by making HTTP calls out to a server. #[derive(Clone)] pub struct Client { hyper_client: Arc< Fn( &Handle ) -> Box< hyper::client::Service< Request = hyper::Request, Response = hyper::Response, Error = hyper::Error, Future = hyper::client::FutureResponse, >, > + Sync + Send, >, handle: Arc, base_path: String, } impl fmt::Debug for Client { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Client {{ base_path: {} }}", self.base_path) } } impl Client { /// Create an HTTP client. /// /// # Arguments /// * `handle` - tokio reactor handle to use for execution /// * `base_path` - base path of the client API, i.e. "www.my-api-implementation.com" pub fn try_new_http(handle: Handle, base_path: &str) -> Result { let http_connector = swagger::http_connector(); Self::try_new_with_connector::( handle, base_path, Some("http"), http_connector, ) } /// Create a client with a TLS connection to the server. /// /// # Arguments /// * `handle` - tokio reactor handle to use for execution /// * `base_path` - base path of the client API, i.e. "www.my-api-implementation.com" /// * `ca_certificate` - Path to CA certificate used to authenticate the server pub fn try_new_https( handle: Handle, base_path: &str, ca_certificate: CA, ) -> Result where CA: AsRef, { let https_connector = swagger::https_connector(ca_certificate); Self::try_new_with_connector::>( handle, base_path, Some("https"), https_connector, ) } /// Create a client with a mutually authenticated TLS connection to the server. /// /// # Arguments /// * `handle` - tokio reactor handle to use for execution /// * `base_path` - base path of the client API, i.e. "www.my-api-implementation.com" /// * `ca_certificate` - Path to CA certificate used to authenticate the server /// * `client_key` - Path to the client private key /// * `client_certificate` - Path to the client's public certificate associated with the private key pub fn try_new_https_mutual( handle: Handle, base_path: &str, ca_certificate: CA, client_key: K, client_certificate: C, ) -> Result where CA: AsRef, K: AsRef, C: AsRef, { let https_connector = swagger::https_mutual_connector(ca_certificate, client_key, client_certificate); Self::try_new_with_connector::>( handle, base_path, Some("https"), https_connector, ) } /// Create a client with a custom implementation of hyper::client::Connect. /// /// Intended for use with custom implementations of connect for e.g. protocol logging /// or similar functionality which requires wrapping the transport layer. When wrapping a TCP connection, /// this function should be used in conjunction with /// `swagger::{http_connector, https_connector, https_mutual_connector}`. /// /// For ordinary tcp connections, prefer the use of `try_new_http`, `try_new_https` /// and `try_new_https_mutual`, to avoid introducing a dependency on the underlying transport layer. /// /// # Arguments /// /// * `handle` - tokio reactor handle to use for execution /// * `base_path` - base path of the client API, i.e. "www.my-api-implementation.com" /// * `protocol` - Which protocol to use when constructing the request url, e.g. `Some("http")` /// * `connector_fn` - Function which returns an implementation of `hyper::client::Connect` pub fn try_new_with_connector( handle: Handle, base_path: &str, protocol: Option<&'static str>, connector_fn: Box C + Send + Sync>, ) -> Result where C: hyper::client::Connect + hyper::client::Service, { let hyper_client = { move |handle: &Handle| -> Box< hyper::client::Service< Request = hyper::Request, Response = hyper::Response, Error = hyper::Error, Future = hyper::client::FutureResponse, >, > { let connector = connector_fn(handle); Box::new( hyper::Client::configure() .connector(connector) .build(handle), ) } }; Ok(Client { hyper_client: Arc::new(hyper_client), handle: Arc::new(handle), base_path: into_base_path(base_path, protocol)?, }) } /// Constructor for creating a `Client` by passing in a pre-made `hyper` client. /// /// One should avoid relying on this function if possible, since it adds a dependency on the underlying transport /// implementation, which it would be better to abstract away. Therefore, using this function may lead to a loss of /// code generality, which may make it harder to move the application to a serverless environment, for example. /// /// The reason for this function's existence is to support legacy test code, which did mocking at the hyper layer. /// This is not a recommended way to write new tests. If other reasons are found for using this function, they /// should be mentioned here. pub fn try_new_with_hyper_client( hyper_client: Arc< Fn( &Handle ) -> Box< hyper::client::Service< Request = hyper::Request, Response = hyper::Response, Error = hyper::Error, Future = hyper::client::FutureResponse, >, > + Sync + Send, >, handle: Handle, base_path: &str, ) -> Result { Ok(Client { hyper_client: hyper_client, handle: Arc::new(handle), base_path: into_base_path(base_path, None)?, }) } } impl Api for Client { fn container_id_get( &self, param_id: String, context: &Context, ) -> Box> { let uri = format!( "{}/v0/container/{id}", self.base_path, id = utf8_percent_encode(¶m_id.to_string(), PATH_SEGMENT_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { ContainerIdGetResponse::FetchASingleContainerById(body) }), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ContainerIdGetResponse::BadRequest(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { ContainerIdGetResponse::GenericErrorResponse(body) }), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn container_lookup_get( &self, param_issn: String, context: &Context, ) -> Box> { // Query parameters let query_issn = format!("issn={issn}&", issn = param_issn.to_string()); let uri = format!( "{}/v0/container/lookup?{issn}", self.base_path, issn = utf8_percent_encode(&query_issn, QUERY_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { ContainerLookupGetResponse::FindASingleContainerByExternalIdentifer(body) }), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ContainerLookupGetResponse::BadRequest(body)), ) as Box> } 404 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ContainerLookupGetResponse::NoSuchContainer(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { ContainerLookupGetResponse::GenericErrorResponse(body) }), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn container_post( &self, param_body: Option, context: &Context, ) -> Box> { let uri = format!("{}/v0/container", self.base_path); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Post, uri); let body = param_body .map(|ref body| serde_json::to_string(body).expect("impossible to fail to serialize")); if let Some(body) = body { request.set_body(body.into_bytes()); } request .headers_mut() .set(ContentType(mimetypes::requests::CONTAINER_POST.clone())); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 201 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ContainerPostResponse::Created(body)), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ContainerPostResponse::BadRequest(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ContainerPostResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn creator_id_get( &self, param_id: String, context: &Context, ) -> Box> { let uri = format!( "{}/v0/creator/{id}", self.base_path, id = utf8_percent_encode(¶m_id.to_string(), PATH_SEGMENT_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { CreatorIdGetResponse::FetchASingleCreatorById(body) }), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| CreatorIdGetResponse::BadRequest(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| CreatorIdGetResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn creator_lookup_get( &self, param_orcid: String, context: &Context, ) -> Box> { // Query parameters let query_orcid = format!("orcid={orcid}&", orcid = param_orcid.to_string()); let uri = format!( "{}/v0/creator/lookup?{orcid}", self.base_path, orcid = utf8_percent_encode(&query_orcid, QUERY_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { CreatorLookupGetResponse::FindASingleCreatorByExternalIdentifer( body, ) }), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| CreatorLookupGetResponse::BadRequest(body)), ) as Box> } 404 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| CreatorLookupGetResponse::NoSuchCreator(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { CreatorLookupGetResponse::GenericErrorResponse(body) }), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn creator_post( &self, param_body: Option, context: &Context, ) -> Box> { let uri = format!("{}/v0/creator", self.base_path); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Post, uri); let body = param_body .map(|ref body| serde_json::to_string(body).expect("impossible to fail to serialize")); if let Some(body) = body { request.set_body(body.into_bytes()); } request .headers_mut() .set(ContentType(mimetypes::requests::CREATOR_POST.clone())); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 201 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| CreatorPostResponse::Created(body)), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| CreatorPostResponse::BadRequest(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| CreatorPostResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn editgroup_id_accept_post( &self, param_id: i32, context: &Context, ) -> Box> { let uri = format!( "{}/v0/editgroup/{id}/accept", self.base_path, id = utf8_percent_encode(¶m_id.to_string(), PATH_SEGMENT_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Post, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditgroupIdAcceptPostResponse::MergedEditgroupSuccessfully_( body, ) }), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditgroupIdAcceptPostResponse::EditgroupIsInAnUnmergableState( body, ) }), ) as Box> } 404 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditgroupIdAcceptPostResponse::NoSuchEditgroup(body) }), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditgroupIdAcceptPostResponse::GenericErrorResponse(body) }), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn editgroup_id_get( &self, param_id: i32, context: &Context, ) -> Box> { let uri = format!( "{}/v0/editgroup/{id}", self.base_path, id = utf8_percent_encode(¶m_id.to_string(), PATH_SEGMENT_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditgroupIdGetResponse::FetchEditgroupByIdentifier(body) }), ) as Box> } 404 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| EditgroupIdGetResponse::NoSuchEditgroup(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditgroupIdGetResponse::GenericErrorResponse(body) }), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn editgroup_post( &self, context: &Context, ) -> Box> { let uri = format!("{}/v0/editgroup", self.base_path); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Post, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 201 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| EditgroupPostResponse::SuccessfullyCreated(body)), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditgroupPostResponse::InvalidRequestParameters(body) }), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| EditgroupPostResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn editor_username_changelog_get( &self, param_username: String, context: &Context, ) -> Box> { let uri = format!( "{}/v0/editor/{username}/changelog", self.base_path, username = utf8_percent_encode(¶m_username.to_string(), PATH_SEGMENT_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditorUsernameChangelogGetResponse::FindChanges_(body) }), ) as Box> } 404 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditorUsernameChangelogGetResponse::UsernameNotFound(body) }), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditorUsernameChangelogGetResponse::GenericErrorResponse(body) }), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn editor_username_get( &self, param_username: String, context: &Context, ) -> Box> { let uri = format!( "{}/v0/editor/{username}", self.base_path, username = utf8_percent_encode(¶m_username.to_string(), PATH_SEGMENT_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditorUsernameGetResponse::FetchGenericInformationAboutAnEditor( body, ) }), ) as Box> } 404 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| EditorUsernameGetResponse::UsernameNotFound(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { EditorUsernameGetResponse::GenericErrorResponse(body) }), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn file_id_get( &self, param_id: String, context: &Context, ) -> Box> { let uri = format!( "{}/v0/file/{id}", self.base_path, id = utf8_percent_encode(¶m_id.to_string(), PATH_SEGMENT_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| FileIdGetResponse::FetchASingleFileById(body)), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| FileIdGetResponse::BadRequest(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| FileIdGetResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn file_lookup_get( &self, param_sha1: String, context: &Context, ) -> Box> { // Query parameters let query_sha1 = format!("sha1={sha1}&", sha1 = param_sha1.to_string()); let uri = format!( "{}/v0/file/lookup?{sha1}", self.base_path, sha1 = utf8_percent_encode(&query_sha1, QUERY_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { FileLookupGetResponse::FindASingleFileByExternalIdentifer(body) }), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| FileLookupGetResponse::BadRequest(body)), ) as Box> } 404 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| FileLookupGetResponse::NoSuchFile(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| FileLookupGetResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn file_post( &self, param_body: Option, context: &Context, ) -> Box> { let uri = format!("{}/v0/file", self.base_path); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Post, uri); let body = param_body .map(|ref body| serde_json::to_string(body).expect("impossible to fail to serialize")); if let Some(body) = body { request.set_body(body.into_bytes()); } request .headers_mut() .set(ContentType(mimetypes::requests::FILE_POST.clone())); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 201 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| FilePostResponse::Created(body)), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| FilePostResponse::BadRequest(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| FilePostResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn release_id_get( &self, param_id: String, context: &Context, ) -> Box> { let uri = format!( "{}/v0/release/{id}", self.base_path, id = utf8_percent_encode(¶m_id.to_string(), PATH_SEGMENT_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { ReleaseIdGetResponse::FetchASingleReleaseById(body) }), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ReleaseIdGetResponse::BadRequest(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ReleaseIdGetResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn release_lookup_get( &self, param_doi: String, context: &Context, ) -> Box> { // Query parameters let query_doi = format!("doi={doi}&", doi = param_doi.to_string()); let uri = format!( "{}/v0/release/lookup?{doi}", self.base_path, doi = utf8_percent_encode(&query_doi, QUERY_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { ReleaseLookupGetResponse::FindASingleReleaseByExternalIdentifer( body, ) }), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ReleaseLookupGetResponse::BadRequest(body)), ) as Box> } 404 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ReleaseLookupGetResponse::NoSuchRelease(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| { ReleaseLookupGetResponse::GenericErrorResponse(body) }), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn release_post( &self, param_body: Option, context: &Context, ) -> Box> { let uri = format!("{}/v0/release", self.base_path); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Post, uri); let body = param_body .map(|ref body| serde_json::to_string(body).expect("impossible to fail to serialize")); if let Some(body) = body { request.set_body(body.into_bytes()); } request .headers_mut() .set(ContentType(mimetypes::requests::RELEASE_POST.clone())); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 201 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ReleasePostResponse::Created(body)), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ReleasePostResponse::BadRequest(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| ReleasePostResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn work_id_get( &self, param_id: String, context: &Context, ) -> Box> { let uri = format!( "{}/v0/work/{id}", self.base_path, id = utf8_percent_encode(¶m_id.to_string(), PATH_SEGMENT_ENCODE_SET) ); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Get, uri); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 200 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| WorkIdGetResponse::FetchASingleWorkById(body)), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| WorkIdGetResponse::BadRequest(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| WorkIdGetResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } fn work_post( &self, param_body: Option, context: &Context, ) -> Box> { let uri = format!("{}/v0/work", self.base_path); let uri = match Uri::from_str(&uri) { Ok(uri) => uri, Err(err) => { return Box::new(futures::done(Err(ApiError(format!( "Unable to build URI: {}", err ))))) } }; let mut request = hyper::Request::new(hyper::Method::Post, uri); let body = param_body .map(|ref body| serde_json::to_string(body).expect("impossible to fail to serialize")); if let Some(body) = body { request.set_body(body.into_bytes()); } request .headers_mut() .set(ContentType(mimetypes::requests::WORK_POST.clone())); context .x_span_id .as_ref() .map(|header| request.headers_mut().set(XSpanId(header.clone()))); let hyper_client = (self.hyper_client)(&*self.handle); Box::new( hyper_client .call(request) .map_err(|e| ApiError(format!("No response received: {}", e))) .and_then(|mut response| match response.status().as_u16() { 201 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| WorkPostResponse::Created(body)), ) as Box> } 400 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| WorkPostResponse::BadRequest(body)), ) as Box> } 0 => { let body = response.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::(body) .map_err(|e| e.into()) }) }) .map(move |body| WorkPostResponse::GenericErrorResponse(body)), ) as Box> } code => { let headers = response.headers().clone(); Box::new(response.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!("", e)) } }, Err(e) => Cow::from(format!("", e)), } ))) })) as Box> } }), ) } } #[derive(Debug)] pub enum ClientInitError { InvalidScheme, InvalidUri(hyper::error::UriError), MissingHost, SslError(openssl::error::ErrorStack), } impl From for ClientInitError { fn from(err: hyper::error::UriError) -> ClientInitError { ClientInitError::InvalidUri(err) } } impl From for ClientInitError { fn from(err: openssl::error::ErrorStack) -> ClientInitError { ClientInitError::SslError(err) } } impl fmt::Display for ClientInitError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (self as &fmt::Debug).fmt(f) } } impl error::Error for ClientInitError { fn description(&self) -> &str { "Failed to produce a hyper client." } }