//! {{description}} //! {{>partial_header}} use reqwest::StatusCode; use crate::apis::ResponseContent; use super::{Error, configuration}; {{#operations}} {{#operation}} /// Enum for successes of method [`{{operationId}}`]. #[derive(Debug, Clone)] pub enum {{{operationIdCamelCase}}}Success { {{#responses}} {{#is2xx}} /// Response for status code {{code}}. Status{{code}}{{#isEnum}}{{#enumName}}({{.}}){{/enumName}}{{/isEnum}}{{^isEnum}}{{#dataType}}({{.}}){{/dataType}}{{/isEnum}}, {{/is2xx}} {{#is3xx}} /// Response for status code {{code}}. Status{{code}}{{#isEnum}}{{#enumName}}({{.}}){{/enumName}}{{/isEnum}}{{^isEnum}}{{#dataType}}({{.}}){{/dataType}}{{/isEnum}}, {{/is3xx}} {{/responses}} } impl {{{operationIdCamelCase}}}Success { fn from_body(status: StatusCode, body: &str) -> Option> { // Attempt to deserialize the response for the given status code. match status.as_u16() { {{#responses}} {{#is2xx}} {{#isEnum}} {{#enumName}} {{code}} => { Some(match serde_json::from_str(body) { Ok(data) => Ok(Self::Status{{code}}(data)), Err(err) => Err(err), }) } {{/enumName}} {{^enumName}} {{code}} => { Some(Ok(Self::Status{{code}})) } {{/enumName}} {{/isEnum}} {{^isEnum}} {{#dataType}} {{code}} => { Some(match serde_json::from_str(body) { Ok(data) => Ok(Self::Status{{code}}(data)), Err(err) => Err(err), }) } {{/dataType}} {{^dataType}} {{code}} => { Some(Ok(Self::Status{{code}})) } {{/dataType}} {{/isEnum}} {{/is2xx}} {{#is3xx}} {{#isEnum}} {{#enumName}} {{code}} => { Some(match serde_json::from_str(body) { Ok(data) => Ok(Self::Status{{code}}(data)), Err(err) => Err(err), }) } {{/enumName}} {{^enumName}} {{code}} => { Some(Ok(Self::Status{{code}})) } {{/enumName}} {{/isEnum}} {{^isEnum}} {{#dataType}} {{code}} => { Some(match serde_json::from_str(body) { Ok(data) => Ok(Self::Status{{code}}(data)), Err(err) => Err(err), }) } {{/dataType}} {{^dataType}} {{code}} => { Some(Ok(Self::Status{{code}})) } {{/dataType}} {{/isEnum}} {{/is3xx}} {{/responses}} _ => None } } } {{/operation}} {{/operations}} {{#operations}} {{#operation}} /// Enum for known errors of method [`{{operationId}}`]. #[derive(Debug, Clone)] pub enum {{{operationIdCamelCase}}}Error { {{#responses}} {{#is4xx}} /// Error for status code {{code}}. Status{{code}}{{#isEnum}}{{#enumName}}({{.}}){{/enumName}}{{/isEnum}}{{^isEnum}}{{#dataType}}({{.}}){{/dataType}}{{/isEnum}}, {{/is4xx}} {{#is5xx}} /// Error for status code {{code}}. Status{{code}}{{#isEnum}}{{#enumName}}({{.}}){{/enumName}}{{/isEnum}}{{^isEnum}}{{#dataType}}({{.}}){{/dataType}}{{/isEnum}}, {{/is5xx}} {{#isDefault}} /// Default error response. DefaultResponse {{#isEnum}}{{#enumName}}({{.}}){{/enumName}}{{/isEnum}}{{^isEnum}}{{#dataType}}({{.}}){{/dataType}}{{/isEnum}}, {{/isDefault}} {{/responses}} } impl {{{operationIdCamelCase}}}Error { #[allow(unused_variables, clippy::match_single_binding)] fn from_body(status: StatusCode, body: &str) -> Option> { // Attempt to deserialize the response for the given status code. match status.as_u16() { {{#responses}} {{#is4xx}} {{code}} => { Some(match serde_json::from_str(body) { Ok(data) => Ok(Self::Status{{code}}(data)), Err(err) => Err(err), }) } {{/is4xx}} {{#is5xx}} {{code}} => { Some(match serde_json::from_str(body) { Ok(data) => Ok(Self::Status{{code}}(data)), Err(err) => Err(err), }) } {{/is5xx}} {{#isDefault}} _ if { serde_json::from_str::<{{#isEnum}}{{{enumName}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}>(body).is_ok() } => { Some(match serde_json::from_str(body) { Ok(data) => Ok(Self::DefaultResponse(data)), Err(err) => Err(err), }) } {{/isDefault}} {{/responses}} _ => None } } } {{/operation}} {{/operations}} {{#operations}} {{#operation}} {{#description}} /// {{{.}}} {{/description}} {{#notes}} /// {{{.}}} {{/notes}} pub async fn {{{operationId}}}( configuration: &configuration::Configuration, {{#allParams}} {{{paramName}}}: {{^required}}Option<{{/required}}{{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{#isString}}{{#isArray}}Vec<{{/isArray}}{{^isUuid}}&str{{/isUuid}}{{#isArray}}>{{/isArray}}{{/isString}}{{#isUuid}}{{#isArray}}Vec<{{/isArray}}&str{{#isArray}}>{{/isArray}}{{/isUuid}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}{{#isBodyParam}}crate::models::{{/isBodyParam}}{{/isContainer}}{{/isPrimitiveType}}{{#isEnumOrRef}}crate::models::{{/isEnumOrRef}}{{#composedSchemas}}crate::models::{{/composedSchemas}}{{{dataType}}}{{/isUuid}}{{/isString}}{{^required}}>{{/required}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{^-last}}, {{/-last}} {{/allParams}} ) -> Result, Error<{{{operationIdCamelCase}}}Error>> { let client = &configuration.client; // Create the request from a path. // Make sure to url encode any user given text. let uri_str = format!( "{}{{{path}}}", configuration.base_path {{#pathParams}}, {{{baseName}}}={{#isString}}crate::apis::urlencode({{/isString}} {{{paramName}}} {{#isString}}){{/isString}} {{/pathParams}} ); let mut req_builder = client.request(reqwest::Method::{{{httpMethod}}}, &uri_str); {{#queryParams}} // === Add queries to request === {{#required}} {{#isArray}} req_builder = match "{{collectionFormat}}" { "multi" => req_builder.query(&{{{paramName}}}.into_iter().map(|p| ("{{{baseName}}}".to_owned(), p.to_string())).collect::>()), _ => req_builder.query(&[("{{{baseName}}}", &{{{paramName}}}.into_iter().map(|p| p.to_string()).collect::>().join(",").to_string())]), }; {{/isArray}} {{^isArray}} {{^isNullable}} req_builder = req_builder.query(&[("{{{baseName}}}", &{{{paramName}}}.to_string())]); {{/isNullable}} {{#isNullable}} {{#isDeepObject}} if let Some(s) = &{{{paramName}}} { let params = crate::apis::parse_deep_object("{{{baseName}}}", s); req_builder = req_builder.query(¶ms); }; {{/isDeepObject}} {{^isDeepObject}} if let Some(s) = &{{{paramName}}} { req_builder = req_builder.query(&[("{{{baseName}}}", &s.to_string())]); }; {{/isDeepObject}} {{/isNullable}} {{/isArray}} {{/required}} {{^required}} if let Some(s) = &{{{paramName}}} { {{#isArray}} req_builder = match "{{collectionFormat}}" { "multi" => req_builder.query(&s.into_iter().map(|p| ("{{{baseName}}}".to_owned(), p.to_string())).collect::>()), _ => req_builder.query(&[("{{{baseName}}}", &s.into_iter().map(|p| p.to_string()).collect::>().join(",").to_string())]), }; {{/isArray}} {{^isArray}} {{#isDeepObject}} let params = crate::apis::parse_deep_object("{{{baseName}}}", s); req_builder = req_builder.query(¶ms); {{/isDeepObject}} {{^isDeepObject}} req_builder = req_builder.query(&[("{{{baseName}}}", &s.to_string())]); {{/isDeepObject}} {{/isArray}} } {{/required}} {{/queryParams}} {{#hasAuthMethods}} {{#authMethods}} {{#isApiKey}} {{#isKeyInQuery}} // Add query auth if needed. if let Some(apikey) = &configuration.api_key { let key = apikey.key.clone(); let value = match &apikey.prefix { Some(prefix) => format!("{} {}", prefix, key), None => key, }; req_builder = req_builder.query(&[("{{{keyParamName}}}", value)]); } {{/isKeyInQuery}} {{/isApiKey}} {{/authMethods}} {{/hasAuthMethods}} // === Add headers to request === // Set the user agent string if given. if let Some(user_agent) = &configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } {{#hasHeaderParams}} {{#headerParams}} {{#required}} {{^isNullable}} req_builder = req_builder.header("{{{baseName}}}", {{{paramName}}}{{#isArray}}.join(","){{/isArray}}.to_string()); {{/isNullable}} {{#isNullable}} match &{{{paramName}}} { Some(param_value) => { req_builder = req_builder.header("{{{baseName}}}", param_value{{#isArray}}.join(","){{/isArray}}.to_string()); }, None => { req_builder = req_builder.header("{{{baseName}}}", ""); }, } {{/isNullable}} {{/required}} {{^required}} if let Some(param_value) = &{{{paramName}}} { req_builder = req_builder.header("{{{baseName}}}", param_value{{#isArray}}.join(","){{/isArray}}.to_string()); } {{/required}} {{/headerParams}} {{/hasHeaderParams}} {{#hasAuthMethods}} // === Add auth to request === {{#authMethods}} {{#isApiKey}} {{#isKeyInHeader}} if let Some(apikey) = &configuration.api_key { let key = apikey.key.clone(); let value = match &apikey.prefix { Some(prefix) => format!("{} {}", prefix, key), None => key, }; req_builder = req_builder.header("{{{keyParamName}}}", value); }; {{/isKeyInHeader}} {{/isApiKey}} {{#isBasic}} {{#isBasicBasic}} if let Some(auth_conf) = &configuration.basic_auth { req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned()); }; {{/isBasicBasic}} {{#isBasicBearer}} if let Some(token) = &configuration.bearer_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; {{/isBasicBearer}} {{/isBasic}} {{#isOAuth}} if let Some(token) = &configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; {{/isOAuth}} {{/authMethods}} {{/hasAuthMethods}} {{#isMultipart}} {{#hasFormParams}} // === Add multipart to request === let mut form = reqwest::multipart::Form::new(); {{#formParams}} {{#isFile}} compile_error!("TODO: support async file upload"); {{/isFile}} {{^isFile}} {{#required}} {{^isNullable}} form = form.text("{{{baseName}}}", {{{paramName}}}{{#isArray}}.into_iter().map(|p| p.to_string()).collect::>().join(","){{/isArray}}.to_string()); {{/isNullable}} {{#isNullable}} match &{{{paramName}}} { Some(param_value) => { form = form.text("{{{baseName}}}", param_value{{#isArray}}.into_iter().map(|p| p.to_string()).collect::>().join(","){{/isArray}}.to_string()); }, None => { form = form.text("{{{baseName}}}", ""); }, } {{/isNullable}} {{/required}} {{^required}} if let Some(param_value) = &{{{paramName}}} { form = form.text("{{{baseName}}}", param_value{{#isArray}}.into_iter().map(|p| p.to_string()).collect::>().join(","){{/isArray}}.to_string()); } {{/required}} {{/isFile}} {{/formParams}} req_builder = req_builder.multipart(form); {{/hasFormParams}} {{/isMultipart}} {{^isMultipart}} {{#hasFormParams}} // === Add form to request === let mut form_params = std::collections::HashMap::new(); {{#formParams}} {{#isFile}} {{#required}} {{^isNullable}} form_params.insert("{{{baseName}}}", compile_error!("File form param not supported with x-www-form-urlencoded content")); {{/isNullable}} {{#isNullable}} match &{{{paramName}}} { Some(param_value) => { form_params.insert("{{{baseName}}}", compile_error!("File form param not supported with x-www-form-urlencoded content")); }, None => { compile_error!("Required nullable file form param not supported with x-www-form-urlencoded content"); }, } {{/isNullable}} {{/required}} {{^required}} if let Some(param_value) = &{{{paramName}}} { form_params.insert("{{{baseName}}}", compile_error!("File form param not supported with x-www-form-urlencoded content")); } {{/required}} {{/isFile}} {{^isFile}} {{#required}} {{^isNullable}} form_params.insert("{{{baseName}}}", {{{paramName}}}{{#isArray}}.into_iter().map(|p| p.to_string()).collect::>().join(","){{/isArray}}.to_string()); {{/isNullable}} {{#isNullable}} match &{{{paramName}}} { Some(param_value) => { form_params.insert("{{{baseName}}}", param_value{{#isArray}}.into_iter().map(|p| p.to_string()).collect::>().join(","){{/isArray}}.to_string()); }, None => { form_params.insert("{{{baseName}}}", ""); }, } {{/isNullable}} {{/required}} {{^required}} if let Some(param_value) = &{{{paramName}}} { form_params.insert("{{{baseName}}}", param_value{{#isArray}}.into_iter().map(|p| p.to_string()).collect::>().join(","){{/isArray}}.to_string()); } {{/required}} {{/isFile}} {{/formParams}} req_builder = req_builder.form(&form_params); {{/hasFormParams}} {{/isMultipart}} {{#hasBodyParam}} // === Add body to request === {{#bodyParams}} req_builder = req_builder.json(&{{{paramName}}}); {{/bodyParams}} {{/hasBodyParam}} // === Request is built. // Execute the request. let req = req_builder.build()?; let resp = client.execute(req).await?; // Get the response. let status = resp.status(); let response_body = resp.text().await?; if !status.is_client_error() && !status.is_server_error() { // Try to parse the OK response. match {{{operationIdCamelCase}}}Success::from_body(status, &response_body) { Some(Ok(content)) => Ok(ResponseContent { status, response_body, content }), Some(Err(err)) => Err(err.into()), None => Err(Error::UnknownResponse { status, is_error: false, response_body }) } } else { // Try to parse the Err response. match {{{operationIdCamelCase}}}Error::from_body(status, &response_body) { Some(Ok(content)) => Err(Error::ResponseError(ResponseContent { status, response_body, content })), Some(Err(err)) => Err(err.into()), None => Err(Error::UnknownResponse { status, is_error: true, response_body }) } } } {{/operation}} {{/operations}}