rust.reqwest-trait.api_mod.mustache Maven / Gradle / Ivy
use std::error;
use std::fmt;
{{#withAWSV4Signature}}
use aws_sigv4;
{{/withAWSV4Signature}}
#[derive(Debug, Clone)]
pub struct ResponseContent {
pub status: reqwest::StatusCode,
pub content: String,
pub entity: Option,
}
#[derive(Debug)]
pub enum Error {
Reqwest(reqwest::Error),
{{#supportMiddleware}}
ReqwestMiddleware(reqwest_middleware::Error),
{{/supportMiddleware}}
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseContent),
{{#withAWSV4Signature}}
AWSV4SignatureError(aws_sigv4::http_request::Error),
{{/withAWSV4Signature}}
{{#supportTokenSource}}
TokenSource(Box),
{{/supportTokenSource}}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (module, e) = match self {
Error::Reqwest(e) => ("reqwest", e.to_string()),
{{#supportMiddleware}}
Error::ReqwestMiddleware(e) => ("reqwest-middleware", e.to_string()),
{{/supportMiddleware}}
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
{{#withAWSV4Signature}}
Error::AWSV4SignatureError(e) => ("aws v4 signature", e.to_string()),
{{/withAWSV4Signature}}
{{#supportTokenSource}}
Error::TokenSource(e) => ("token source failure", e.to_string()),
{{/supportTokenSource}}
};
write!(f, "error in {}: {}", module, e)
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(match self {
Error::Reqwest(e) => e,
{{#supportMiddleware}}
Error::ReqwestMiddleware(e) => e,
{{/supportMiddleware}}
Error::Serde(e) => e,
Error::Io(e) => e,
Error::ResponseError(_) => return None,
{{#withAWSV4Signature}}
Error::AWSV4SignatureError(_) => return None,
{{/withAWSV4Signature}}
{{#supportTokenSource}}
Error::TokenSource(e) => &**e,
{{/supportTokenSource}}
})
}
}
impl From for Error {
fn from(e: reqwest::Error) -> Self {
Error::Reqwest(e)
}
}
{{#supportMiddleware}}
impl From for Error {
fn from(e: reqwest_middleware::Error) -> Self {
Error::ReqwestMiddleware(e)
}
}
{{/supportMiddleware}}
impl From for Error {
fn from(e: serde_json::Error) -> Self {
Error::Serde(e)
}
}
impl From for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
pub fn urlencode>(s: T) -> String {
::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
}
pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String, String)> {
if let serde_json::Value::Object(object) = value {
let mut params = vec![];
for (key, value) in object {
match value {
serde_json::Value::Object(_) => params.append(&mut parse_deep_object(
&format!("{}[{}]", prefix, key),
value,
)),
serde_json::Value::Array(array) => {
for (i, value) in array.iter().enumerate() {
params.append(&mut parse_deep_object(
&format!("{}[{}][{}]", prefix, key, i),
value,
));
}
},
serde_json::Value::String(s) => params.push((format!("{}[{}]", prefix, key), s.clone())),
_ => params.push((format!("{}[{}]", prefix, key), value.to_string())),
}
}
return params;
}
unimplemented!("Only objects are supported with style=deepObject")
}
{{#apiInfo}}
{{#apis}}
pub mod {{{classFilename}}};
{{/apis}}
{{/apiInfo}}
pub mod configuration;
{{#topLevelApiClient}}
use std::sync::Arc;
pub trait Api {
{{#apiInfo}}
{{#apis}}
fn {{{classFilename}}}(&self) -> &dyn {{{classFilename}}}::{{classname}};
{{/apis}}
{{/apiInfo}}
}
pub struct ApiClient {
{{#apiInfo}}
{{#apis}}
{{{classFilename}}}: Box,
{{/apis}}
{{/apiInfo}}
}
impl ApiClient {
pub fn new(configuration: Arc) -> Self {
Self {
{{#apiInfo}}
{{#apis}}
{{{classFilename}}}: Box::new({{{classFilename}}}::{{classname}}Client::new(configuration.clone())),
{{/apis}}
{{/apiInfo}}
}
}
}
impl Api for ApiClient {
{{#apiInfo}}
{{#apis}}
fn {{{classFilename}}}(&self) -> &dyn {{{classFilename}}}::{{classname}} {
self.{{{classFilename}}}.as_ref()
}
{{/apis}}
{{/apiInfo}}
}
{{#mockall}}
#[cfg(feature = "mockall")]
pub struct MockApiClient {
{{#apiInfo}}
{{#apis}}
pub {{{classFilename}}}_mock: {{{classFilename}}}::Mock{{classname}},
{{/apis}}
{{/apiInfo}}
}
#[cfg(feature = "mockall")]
impl MockApiClient {
pub fn new() -> Self {
Self {
{{#apiInfo}}
{{#apis}}
{{{classFilename}}}_mock: {{{classFilename}}}::Mock{{classname}}::new(),
{{/apis}}
{{/apiInfo}}
}
}
}
#[cfg(feature = "mockall")]
impl Api for MockApiClient {
{{#apiInfo}}
{{#apis}}
fn {{{classFilename}}}(&self) -> &dyn {{{classFilename}}}::{{classname}} {
&self.{{{classFilename}}}_mock
}
{{/apis}}
{{/apiInfo}}
}
{{/mockall}}
{{/topLevelApiClient}}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy