Skip to content

Commit

Permalink
Use constructors for ResponseContent
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed May 21, 2024
1 parent ffdfac3 commit 9344d51
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 1,492 deletions.
40 changes: 16 additions & 24 deletions generator/src/main/resources/crust/reqwest/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::apis::ResponseContent;
use super::{Error, configuration};
use std::io::Read;

{{#operations}}
{{#operation}}
Expand Down Expand Up @@ -374,44 +373,37 @@ pub fn {{{operationId}}}(configuration: &configuration::Configuration, {{#allPar
_ => Err(err),
})?;


let local_var_headers = super::get_header_map(&local_var_resp);

let local_var_status = local_var_resp.status();
let mut local_var_content = vec![];
local_var_resp.into_reader().read_to_end(&mut local_var_content)?;

if local_var_status < 400 {
{{#vendorExtensions.x-produceMultipleMediaTypes}}
if local_var_resp.status() < 400 {
{{#returnType}}
let local_var_entity : {{{returnType}}} = if is_json { serde_json::from_slice(&local_var_content).map_err(Error::from)? } else { String::from_utf8(local_var_content.clone())? };
{{/returnType}}
{{#vendorExtensions.x-produceMultipleMediaTypes}}
if is_json {
ResponseContent::deserialized(local_var_resp)
} else {
ResponseContent::string(local_var_resp)
}
{{/vendorExtensions.x-produceMultipleMediaTypes}}
{{^vendorExtensions.x-produceMultipleMediaTypes}}
{{#produces}}
{{#mediaIsJson}}
{{#returnType}}
let local_var_entity : {{{returnType}}} = serde_json::from_slice(&local_var_content).map_err(Error::from)?;
{{/returnType}}
ResponseContent::deserialized(local_var_resp)
{{/mediaIsJson}}
{{^mediaIsJson}}
{{#returnType}}
{{#isResponseFile}}
let local_var_entity = local_var_content.clone();
ResponseContent::bytes(local_var_resp)
{{/isResponseFile}}
{{^isResponseFile}}
let local_var_entity = String::from_utf8(local_var_content.clone())?;
ResponseContent::string(local_var_resp)
{{/isResponseFile}}
{{/returnType}}
{{/mediaIsJson}}
{{/produces}}
{{/vendorExtensions.x-produceMultipleMediaTypes}}
let local_var_result = ResponseContent { status: local_var_status, content: local_var_content, entity: {{#returnType}}local_var_entity{{/returnType}}{{^returnType}}(){{/returnType}}, headers: local_var_headers };
Ok(local_var_result)
{{/returnType}}
{{^returnType}}
ResponseContent::unit(local_var_resp)
{{/returnType}}
} else {
let local_var_entity: {{{operationIdCamelCase}}}Error = serde_json::from_slice(&local_var_content)?;
let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity, headers: local_var_headers };
Err(Error::ResponseError(local_var_error))
ResponseContent::deserialized(local_var_resp)
.and_then(|content| Err(Error::ResponseError(content)))
}
}

Expand Down
43 changes: 41 additions & 2 deletions generator/src/main/resources/crust/reqwest/api_mod.mustache
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::error;
use std::fmt;
use std::collections::HashMap;
use base64::{engine::general_purpose, Engine};

pub (crate) fn get_header_map(response : &ureq::Response) -> HashMap<String,String> {
use base64::{engine::general_purpose, Engine};
use serde::de::DeserializeOwned;

fn get_header_map(response: &ureq::Response) -> HashMap<String, String> {
let mut headers = HashMap::new();
let names = response.headers_names();
Expand All @@ -26,6 +27,44 @@ pub struct ResponseContent<T> {
pub headers: HashMap<String, String>,
}

impl<T> ResponseContent<T> {
fn new<F, E>(response: ureq::Response, f: F) -> Result<Self, Error<E>>
where
F: FnOnce(&[u8]) -> Result<T, Error<E>>,
{
let status = response.status();
let headers = get_header_map(&response);
let mut content = Vec::new();
response.into_reader().read_to_end(&mut content)?;
let entity = f(&content)?;
Ok(Self { status, content, entity, headers })
}
}

impl ResponseContent<()> {
fn unit<E>(response: ureq::Response) -> Result<Self, Error<E>> {
Self::new(response, |_| Ok(()))
}
}

impl ResponseContent<Vec<u8>> {
fn bytes<E>(response: ureq::Response) -> Result<Self, Error<E>> {
Self::new(response, |content| Ok(content.into()))
}
}

impl ResponseContent<String> {
fn string<E>(response: ureq::Response) -> Result<Self, Error<E>> {
Self::new(response, |content| String::from_utf8(content.into()).map_err(From::from))
}
}

impl<T: DeserializeOwned> ResponseContent<T> {
fn deserialized<E>(response: ureq::Response) -> Result<Self, Error<E>> {
Self::new(response, |content| serde_json::from_slice(content).map_err(From::from))
}
}

#[derive(Debug)]
pub enum Error<T> {
Multipart {
Expand Down
Loading

0 comments on commit 9344d51

Please sign in to comment.