Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
rust-server.client-operation.mustache Maven / Gradle / Ivy
async fn {{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}(
&self,
{{#vendorExtensions}}
{{#x-callback-params}}
callback_{{.}}: String,
{{/x-callback-params}}
{{/vendorExtensions}}
{{#allParams}}
param_{{{paramName}}}: {{^required}}Option<{{/required}}{{#isArray}}&{{/isArray}}{{{dataType}}}{{^required}}>{{/required}},
{{/allParams}}
context: &C) -> Result<{{{operationId}}}Response, ApiError>
{
let mut client_service = self.client_service.clone();
let mut uri = format!(
{{#isCallbackRequest}}
"{{vendorExtensions.x-path-format-string}}"
{{/isCallbackRequest}}
{{^isCallbackRequest}}
"{}{{^servers}}{{{basePathWithoutHost}}}{{/servers}}{{#servers.0}}{{{url}}}{{/servers.0}}{{vendorExtensions.x-path-format-string}}",
self.base_path
{{/isCallbackRequest}}
{{#pathParams}}
,{{{paramName}}}=utf8_percent_encode(¶m_{{{paramName}}}.to_string(), ID_ENCODE_SET)
{{/pathParams}}
{{#vendorExtensions}}
{{#x-callback-params}}
,{{.}}=callback_{{.}}
{{/x-callback-params}}
{{/vendorExtensions}}
);
// Query parameters
let query_string = {
let mut query_string = form_urlencoded::Serializer::new("".to_owned());
{{#queryParams}}
{{^required}}
if let Some(param_{{{paramName}}}) = param_{{{paramName}}} {
{{/required}}
query_string.append_pair("{{{baseName}}}",
{{#vendorExtensions}}
{{#x-consumes-json}}
&match serde_json::to_string(¶m_{{{paramName}}}) {
Ok(str) => str,
Err(e) => return Err(ApiError(format!("Unable to serialize {{{paramName}}} to string: {}", e))),
});
{{/x-consumes-json}}
{{^x-consumes-json}}
{{#isArray}}
¶m_{{{paramName}}}.iter().map(ToString::to_string).collect::>().join(","));
{{/isArray}}
{{^isArray}}
¶m_{{{paramName}}}{{^isString}}.to_string(){{/isString}});
{{/isArray}}
{{/x-consumes-json}}
{{/vendorExtensions}}
{{^required}}
}
{{/required}}
{{/queryParams}}
{{#authMethods}}
{{#isApiKey}}
{{#isKeyInQuery}}
if let Some(AuthData::ApiKey(ref api_key)) = (context as &dyn Has>).get().as_ref() {
query_string.append_pair("{{keyParamName}}", api_key);
}
{{/isKeyInQuery}}
{{/isApiKey}}
{{/authMethods}}
query_string.finish()
};
if !query_string.is_empty() {
uri += "?";
uri += &query_string;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Err(ApiError(format!("Unable to build URI: {}", err))),
};
let mut request = match Request::builder()
.method("{{{vendorExtensions.x-http-method}}}")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Err(ApiError(format!("Unable to create request: {}", e)))
};
{{>client-request-body-instance}}
let header = HeaderValue::from_str(Has::::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)))
});
{{#hasAuthMethods}}
#[allow(clippy::collapsible_match)]
if let Some(auth_data) = Has::>::get(context).as_ref() {
{{! Currently only authentication with Basic and Bearer are supported }}
#[allow(clippy::single_match, clippy::match_single_binding)]
match auth_data {
{{#authMethods}}
{{#isBasicBasic}}
AuthData::Basic(basic_header) => {
let auth = swagger::auth::Header(basic_header.clone());
let header = match HeaderValue::from_str(&format!("{}", auth)) {
Ok(h) => h,
Err(e) => return Err(ApiError(format!("Unable to create Authorization header: {}", e)))
};
request.headers_mut().insert(
hyper::header::AUTHORIZATION,
header);
},
{{/isBasicBasic}}
{{#isBasicBearer}}
AuthData::Bearer(bearer_header) => {
let auth = swagger::auth::Header(bearer_header.clone());
let header = match HeaderValue::from_str(&format!("{}", auth)) {
Ok(h) => h,
Err(e) => return Err(ApiError(format!("Unable to create Authorization header: {}", e)))
};
request.headers_mut().insert(
hyper::header::AUTHORIZATION,
header);
},
{{/isBasicBearer}}
{{#isOAuth}}
{{^isBasicBearer}}
AuthData::Bearer(bearer_header) => {
let auth = swagger::auth::Header(bearer_header.clone());
let header = match HeaderValue::from_str(&format!("{}", auth)) {
Ok(h) => h,
Err(e) => return Err(ApiError(format!("Unable to create Authorization header: {}", e)))
};
request.headers_mut().insert(
hyper::header::AUTHORIZATION,
header);
},
{{/isBasicBearer}}
{{/isOAuth}}
{{/authMethods}}
_ => {}
}
}
{{/hasAuthMethods}}
{{#headerParams}}
{{#-first}}
// Header parameters
{{/-first}}
{{^isMap}}
{{^required}}
#[allow(clippy::single_match)]
match param_{{{paramName}}} {
Some(param_{{{paramName}}}) => {
{{/required}}
request.headers_mut().append(
HeaderName::from_static("{{{nameInLowerCase}}}"),
#[allow(clippy::redundant_clone)]
match header::IntoHeaderValue(param_{{{paramName}}}.clone()).try_into() {
Ok(header) => header,
Err(e) => {
return Err(ApiError(format!(
"Invalid header {{{paramName}}} - {}", e)));
},
});
{{^required}}
},
None => {}
}
{{/required}}
{{/isMap}}
{{#isMap}}
let param_{{{paramName}}}: Option<{{{dataType}}}> = None;
{{/isMap}}
{{/headerParams}}
let response = client_service.call((request, context.clone()))
.map_err(|e| ApiError(format!("No response received: {}", e))).await?;
match response.status().as_u16() {
{{#responses}}
{{{code}}} => {
{{#headers}}
let response_{{{name}}} = match response.headers().get(HeaderName::from_static("{{{nameInLowerCase}}}")) {
Some(response_{{{name}}}) => {
let response_{{{name}}} = response_{{{name}}}.clone();
let response_{{{name}}} = match TryInto::>::try_into(response_{{{name}}}) {
Ok(value) => value,
Err(e) => {
return Err(ApiError(format!("Invalid response header {{baseName}} for response {{code}} - {}", e)));
},
};
{{#required}}
response_{{{name}}}.0
{{/required}}
{{^required}}
Some(response_{{{name}}}.0)
{{/required}}
},
{{#required}}
None => return Err(ApiError(String::from("Required response header {{{baseName}}} for response {{{code}}} was not found."))),
{{/required}}
{{^required}}
None => None,
{{/required}}
};
{{/headers}}
{{#dataType}}
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?;
{{>client-response-body-instance}}
Ok({{{operationId}}}Response::{{#vendorExtensions}}{{x-response-id}}{{/vendorExtensions}}
{{^headers}}
(body)
{{/headers}}
{{#headers}}
{{#-first}}
{
body,
{{/-first}}
{{{name}}}: response_{{name}},
{{#-last}}
}
{{/-last}}
{{/headers}}
)
{{/dataType}}
{{^dataType}}
Ok(
{{{operationId}}}Response::{{#vendorExtensions}}{{x-response-id}}{{/vendorExtensions}}
{{#headers}}
{{#-first}}
{
{{/-first}}
{{{name}}}: response_{{name}},
{{#-last}}
}
{{/-last}}
{{/headers}}
)
{{/dataType}}
}
{{/responses}}
code => {
let headers = response.headers().clone();
let body = response.into_body()
.take(100)
.into_raw().await;
Err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}",
code,
headers,
match body {
Ok(body) => match String::from_utf8(body) {
Ok(body) => body,
Err(e) => format!("", e),
},
Err(e) => format!("", e),
}
)))
}
}
}