Skip to content

Commit

Permalink
feat: change template to use ureq
Browse files Browse the repository at this point in the history
  • Loading branch information
nponsard committed Aug 29, 2023
1 parent 7714abc commit 004fd22
Show file tree
Hide file tree
Showing 13 changed files with 854 additions and 1,229 deletions.
1 change: 0 additions & 1 deletion .openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Cargo.toml
docs/BackupPassphraseConfig.md
docs/DecryptData.md
docs/DecryptMode.md
Expand Down
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ exclude = [
[dependencies]
serde = "^1.0"
serde_derive = "^1.0"
base64 = "0.21"
serde_json = "^1.0"
url = "^2.2"
uuid = { version = "^1.0", features = ["serde"] }

[dependencies.reqwest]
version = "^0.11"
features = ["json", "multipart"]
ureq = { version = "2", features = ["json"] }
14 changes: 2 additions & 12 deletions generator/src/main/resources/crust/Cargo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,8 @@ serde_derive = "^1.0"
{{#serdeWith}}
serde_with = "^2.0"
{{/serdeWith}}
base64 = "0.21"
serde_json = "^1.0"
url = "^2.2"
uuid = { version = "^1.0", features = ["serde"] }

{{^supportAsync}}
reqwest = {version = "^0.11", features = ["blocking", "json"]}
{{/supportAsync}}
{{#supportAsync}}
{{#supportMiddleware}}
reqwest-middleware = "0.2.0"
{{/supportMiddleware}}
[dependencies.reqwest]
version = "^0.11"
features = ["json", "multipart"]
{{/supportAsync}}
ureq = { version = "2", features = ["json"] }
2 changes: 1 addition & 1 deletion generator/src/main/resources/crust/lib.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate url;
extern crate reqwest;
extern crate ureq;

pub mod apis;
pub mod models;
2 changes: 1 addition & 1 deletion generator/src/main/resources/crust/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl {{{classname}}} {
pub fn new({{#requiredVars}}{{{name}}}: {{#isNullable}}Option<{{/isNullable}}{{#isEnum}}{{#isArray}}Vec<{{/isArray}}{{{enumName}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}>{{/isNullable}}{{^-last}}, {{/-last}}{{/requiredVars}}) -> {{{classname}}} {
{{{classname}}} {
{{#vars}}
{{{name}}}{{^required}}{{#isArray}}: None{{/isArray}}{{#isMap}}: None{{/isMap}}{{^isContainer}}: None{{/isContainer}}{{/required}}{{#required}}{{#isModel}}: {{^isNullable}}Box::new({{{name}}}){{/isNullable}}{{#isNullable}}if let Some(x) = {{{name}}} {Some(Box::new(x))} else {None}{{/isNullable}}{{/isModel}}{{/required}},
{{{name}}}{{^required}}{{#isArray}}: None{{/isArray}}{{#isMap}}: None{{/isMap}}{{^isContainer}}: None{{/isContainer}}{{/required}}{{#required}}{{#isModel}}: {{^isNullable}}Box::new({{{name}}}){{/isNullable}}{{#isNullable}}{{{name}}}.map(Box::new){{/isNullable}}{{/isModel}}{{/required}},
{{/vars}}
}
}
Expand Down
246 changes: 60 additions & 186 deletions generator/src/main/resources/crust/reqwest/api.mustache

Large diffs are not rendered by default.

55 changes: 30 additions & 25 deletions generator/src/main/resources/crust/reqwest/api_mod.mustache
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
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> {
let mut headers = HashMap::new();
let names = response.headers_names();
for name in names {
if let Some(value) = response.header(&name){
headers.insert(name, value.to_string());
}
}

headers
}


#[derive(Debug, Clone)]
pub struct ResponseContent<T> {
pub status: reqwest::StatusCode,
pub status: u16,
pub content: String,
pub entity: T,
pub headers: reqwest::header::HeaderMap
pub headers: HashMap<String, String>,
}

#[derive(Debug)]
pub enum Error<T> {
Reqwest(reqwest::Error),
{{#supportMiddleware}}
ReqwestMiddleware(reqwest_middleware::Error),
{{/supportMiddleware}}
Ureq(ureq::Error),
Serde(serde_json::Error),
Io(std::io::Error),
ResponseError(ResponseContent<T>),
Expand All @@ -23,10 +37,7 @@ pub enum Error<T> {
impl <T> fmt::Display for Error<T> {
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::Ureq(e) => ("ureq", e.to_string()),
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
Expand All @@ -38,31 +49,20 @@ impl <T> fmt::Display for Error<T> {
impl <T: fmt::Debug> error::Error for Error<T> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(match self {
Error::Reqwest(e) => e,
{{#supportMiddleware}}
Error::ReqwestMiddleware(e) => e,
{{/supportMiddleware}}
Error::Ureq(e) => e,
Error::Serde(e) => e,
Error::Io(e) => e,
Error::ResponseError(_) => return None,
})
}
}

impl <T> From<reqwest::Error> for Error<T> {
fn from(e: reqwest::Error) -> Self {
Error::Reqwest(e)
}
}

{{#supportMiddleware}}
impl<T> From<reqwest_middleware::Error> for Error<T> {
fn from(e: reqwest_middleware::Error) -> Self {
Error::ReqwestMiddleware(e)
impl <T> From<ureq::Error> for Error<T> {
fn from(e: ureq::Error) -> Self {
Error::Ureq(e)
}
}

{{/supportMiddleware}}
impl <T> From<serde_json::Error> for Error<T> {
fn from(e: serde_json::Error) -> Self {
Error::Serde(e)
Expand Down Expand Up @@ -108,6 +108,11 @@ pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String
unimplemented!("Only objects are supported with style=deepObject")
}

pub(crate) fn basic_auth(auth: &configuration::BasicAuth) -> String {
let string = format!("{}:{}", auth.0, auth.1.as_ref().unwrap_or(&"".to_string()));
format!("Basic {}", general_purpose::STANDARD.encode(string)).to_string()
}

{{#apiInfo}}
{{#apis}}
pub mod {{{classFilename}}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub struct Configuration {
pub base_path: String,
pub user_agent: Option<String>,
pub client: {{#supportMiddleware}}reqwest_middleware::ClientWithMiddleware{{/supportMiddleware}}{{^supportMiddleware}}reqwest{{^supportAsync}}::blocking{{/supportAsync}}::Client{{/supportMiddleware}},
pub client: ureq::Agent,
pub basic_auth: Option<BasicAuth>,
pub oauth_access_token: Option<String>,
pub bearer_access_token: Option<String>,
Expand All @@ -30,7 +30,7 @@ impl Default for Configuration {
Configuration {
base_path: "{{{basePath}}}".to_owned(),
user_agent: {{#httpUserAgent}}Some("{{{.}}}".to_owned()){{/httpUserAgent}}{{^httpUserAgent}}Some("OpenAPI-Generator/{{{version}}}/rust".to_owned()){{/httpUserAgent}},
client: {{#supportMiddleware}}reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build(){{/supportMiddleware}}{{^supportMiddleware}}reqwest{{^supportAsync}}::blocking{{/supportAsync}}::Client::new(){{/supportMiddleware}},
client: ureq::agent(),
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
Expand Down
4 changes: 2 additions & 2 deletions src/apis/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
pub struct Configuration {
pub base_path: String,
pub user_agent: Option<String>,
pub client: reqwest::Client,
pub client: ureq::Agent,
pub basic_auth: Option<BasicAuth>,
pub oauth_access_token: Option<String>,
pub bearer_access_token: Option<String>,
Expand All @@ -38,7 +38,7 @@ impl Default for Configuration {
Configuration {
base_path: "https://nethsmdemo.nitrokey.com/api/v1".to_owned(),
user_agent: Some("OpenAPI-Generator/v1/rust".to_owned()),
client: reqwest::Client::new(),
client: ureq::agent(),
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
Expand Down
Loading

0 comments on commit 004fd22

Please sign in to comment.