Skip to content

Commit

Permalink
Bump ureq to 3.0.0-rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Oct 29, 2024
1 parent 84d6c5b commit 791e740
Show file tree
Hide file tree
Showing 11 changed files with 565 additions and 654 deletions.
2 changes: 1 addition & 1 deletion .openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.8.0-SNAPSHOT
7.10.0-SNAPSHOT
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ serde = { default-features = false, version = "^1.0" }
serde_derive = "^1.0"
serde_json = { default-features = false, version = "^1.0" }
url = "^2.2"
ureq = { version = "2", features = ["json", "tls"], default-features = false }
ureq = { version = "3.0.0-rc2", features = ["json", "rustls"], default-features = false }
base64 = { version = "0.21", default-features = false, features = ["alloc"] }
http = "1"

[dev-dependencies]
chrono = "0.4.38"
env_logger = "0.11.3"
rustainers = "0.12.0"
rustls = { version = "0.22.4" }
tokio = { version = "1.37.0", features = ["rt", "macros"] }
ureq = "=2.9.7"
ureq = "=3.0.0-rc2"
2 changes: 1 addition & 1 deletion generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ java -DdebugOperations -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar
```

Will, for example, output the debug info for operations.
You can use this info in the `api.mustache` file.
You can use this info in the `api.mustache` file.
5 changes: 3 additions & 2 deletions generator/src/main/resources/crust/Cargo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ serde = { default-features = false, version = "^1.0" }
serde_derive = "^1.0"
serde_json = { default-features = false, version = "^1.0" }
url = "^2.2"
ureq = { version = "2", features = ["json", "tls"], default-features = false }
ureq = { version = "=3.0.0-rc2", features = ["json", "rustls"], default-features = false }
base64 = { version = "0.21", default-features = false, features = ["alloc"] }
mime = "0.3"
multipart = { version = "0.18", default-features = false, features = ["client"] }
http = "1"

[dev-dependencies]
chrono = "0.4.38"
env_logger = "0.11.3"
rustainers = "0.12.0"
rustls = { version = "0.22.4" }
tokio = { version = "1.37.0", features = ["rt", "macros"] }
ureq = "=2.9.7"
ureq = "=3.0.0-rc2"
46 changes: 21 additions & 25 deletions generator/src/main/resources/crust/reqwest/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub fn {{{operationId}}}(configuration: &configuration::Configuration, {{#allPar
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!("{}{{{path}}}", local_var_configuration.base_path{{#pathParams}}, {{{baseName}}}={{#isString}}crate::apis::urlencode({{/isString}}{{{paramName}}}{{^required}}.unwrap(){{/required}}{{#required}}{{#isNullable}}.unwrap(){{/isNullable}}{{/required}}{{#isArray}}.join(",").as_ref(){{/isArray}}{{#isString}}){{/isString}}{{/pathParams}});
let mut local_var_req_builder = local_var_client.request("{{{httpMethod}}}", local_var_uri_str.as_str());
let mut local_var_req_builder = create_request!(local_var_client, {{httpMethod}}, local_var_uri_str.as_str());

{{#queryParams}}
{{#required}}
Expand Down Expand Up @@ -240,24 +240,24 @@ pub fn {{{operationId}}}(configuration: &configuration::Configuration, {{#allPar
{{#hasAuthMethods}}
{{/hasAuthMethods}}
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder.set("user-agent", local_var_user_agent);
local_var_req_builder = local_var_req_builder.header("user-agent", local_var_user_agent);
}
{{#hasHeaderParams}}
{{#headerParams}}
{{#required}}
{{^isNullable}}
local_var_req_builder = local_var_req_builder.set("{{{baseName}}}", {{{paramName}}}{{#isArray}}.join(","){{/isArray}});
local_var_req_builder = local_var_req_builder.header("{{{baseName}}}", {{{paramName}}}{{#isArray}}.join(","){{/isArray}});
{{/isNullable}}
{{#isNullable}}
match {{{paramName}}} {
Some(local_var_param_value) => { local_var_req_builder = local_var_req_builder.set("{{{baseName}}}", local_var_param_value{{#isArray}}.join(","){{/isArray}}); },
None => { local_var_req_builder = local_var_req_builder.set("{{{baseName}}}", ""); },
Some(local_var_param_value) => { local_var_req_builder = local_var_req_builder.header("{{{baseName}}}", local_var_param_value{{#isArray}}.join(","){{/isArray}}); },
None => { local_var_req_builder = local_var_req_builder.header("{{{baseName}}}", ""); },
}
{{/isNullable}}
{{/required}}
{{^required}}
if let Some(local_var_param_value) = {{{paramName}}} {
local_var_req_builder = local_var_req_builder.set("{{{baseName}}}", local_var_param_value{{#isArray}}.join(","){{/isArray}}.to_string().as_str());
local_var_req_builder = local_var_req_builder.header("{{{baseName}}}", local_var_param_value{{#isArray}}.join(","){{/isArray}}.to_string().as_str());
}
{{/required}}
{{/headerParams}}
Expand All @@ -272,7 +272,7 @@ pub fn {{{operationId}}}(configuration: &configuration::Configuration, {{#allPar
Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
None => local_var_key,
};
local_var_req_builder = local_var_req_builder.set("{{{keyParamName}}}", &local_var_value);
local_var_req_builder = local_var_req_builder.header("{{{keyParamName}}}", &local_var_value);
};
{{/isKeyInHeader}}
{{/isApiKey}}
Expand All @@ -281,18 +281,18 @@ pub fn {{{operationId}}}(configuration: &configuration::Configuration, {{#allPar
if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
let value = super::basic_auth(local_var_auth_conf);
local_var_req_builder = local_var_req_builder.set("authorization", &value);
local_var_req_builder = local_var_req_builder.header("authorization", &value);
};
{{/isBasicBasic}}
{{#isBasicBearer}}
if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
local_var_req_builder = local_var_req_builder.set("authorization",format!("Bearer {}",local_var_token));
local_var_req_builder = local_var_req_builder.header("authorization",format!("Bearer {}",local_var_token));
};
{{/isBasicBearer}}
{{/isBasic}}
{{#isOAuth}}
if let Some(ref local_var_token) = local_var_configuration.oauth_access_token {
local_var_req_builder = local_var_req_builder.set("authorization",format!("Bearer {}",local_var_token));
local_var_req_builder = local_var_req_builder.header("authorization",format!("Bearer {}",local_var_token));
};
{{/isOAuth}}
{{/authMethods}}
Expand All @@ -307,7 +307,7 @@ pub fn {{{operationId}}}(configuration: &configuration::Configuration, {{#allPar
let accept_str = "{{{mediaType}}}";
{{/produces}}
{{/vendorExtensions.x-produceMultipleMediaTypes}}
local_var_req_builder = local_var_req_builder.set("accept", accept_str);
local_var_req_builder = local_var_req_builder.header("accept", accept_str);
{{/hasProduces}}

{{#isMultipart}}
Expand Down Expand Up @@ -341,40 +341,39 @@ pub fn {{{operationId}}}(configuration: &configuration::Configuration, {{#allPar
{{/formParams}}
{{/hasFormParams}}

let local_var_multipart = local_var_multipart.prepare()?;
local_var_req_builder = local_var_req_builder.set(
let mut local_var_multipart = local_var_multipart.prepare()?;
local_var_req_builder = local_var_req_builder.header(
"content-type",
&format!("multipart/form-data; boundary={}", local_var_multipart.boundary()),
);
let local_var_result = local_var_req_builder.send(local_var_multipart);
let local_var_result = local_var_req_builder.send(ureq::SendBody::from_reader(&mut local_var_multipart));
{{/isMultipart}}
{{^isMultipart}}
{{#vendorExtensions.x-consumeMultipleMediaTypes}}
{{#hasBodyParam}}{{#bodyParams}}
let body_json = body.is_json();
local_var_req_builder = local_var_req_builder.set("content-type", body.content_type());
local_var_req_builder = local_var_req_builder.header("content-type", body.content_type());
{{/bodyParams}}{{/hasBodyParam}}
let local_var_result = if body_json {
local_var_req_builder.send_json(body)
} else {
local_var_req_builder.send_string(body.get_string().as_str())
local_var_req_builder.send(body.get_string().as_str())
};
{{/vendorExtensions.x-consumeMultipleMediaTypes}}
{{^vendorExtensions.x-consumeMultipleMediaTypes}}
{{#consumes}}
local_var_req_builder = local_var_req_builder.set("content-type", "{{{mediaType}}}");
local_var_req_builder = local_var_req_builder.header("content-type", "{{{mediaType}}}");
{{#mediaIsJson}}
let local_var_result = local_var_req_builder.send_json(
{{/mediaIsJson}}
{{^mediaIsJson}}
{{#hasBodyParam}}
{{#bodyParams}}
{{#isFile}}
let {{{paramName}}} = std::io::Cursor::new({{{paramName}}});
let local_var_result = local_var_req_builder.send(
{{/isFile}}
{{^isFile}}
let local_var_result = local_var_req_builder.send_string(
let local_var_result = local_var_req_builder.send(
{{/isFile}}
{{/bodyParams}}
{{/hasBodyParam}}
Expand All @@ -390,16 +389,13 @@ pub fn {{{operationId}}}(configuration: &configuration::Configuration, {{#allPar
{{#hasBodyParam}});{{/hasBodyParam}}
{{/vendorExtensions.x-consumeMultipleMediaTypes}}
{{^hasBodyParam}}
let local_var_result = local_var_req_builder.call();
let local_var_result = local_var_req_builder.send_empty();
{{/hasBodyParam}}
{{/isMultipart}}

let local_var_resp = local_var_result.or_else(|err| match err {
ureq::Error::Status(_status, resp) => Ok(resp),
_ => Err(err),
})?;
let local_var_resp = local_var_result?;

let local_var_status = local_var_resp.status();
let local_var_status = local_var_resp.status().as_u16();
if local_var_status < 400 {
{{#returnType}}
{{#vendorExtensions.x-produceMultipleMediaTypes}}
Expand Down
36 changes: 23 additions & 13 deletions generator/src/main/resources/crust/reqwest/api_mod.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@ use std::collections::HashMap;
use base64::{engine::general_purpose, Engine};
use serde::de::DeserializeOwned;

fn get_header_map(response: &ureq::Response) -> HashMap<String, String> {
macro_rules! create_request {
($agent:expr, GET, $uri:expr) => { $agent.get($uri).force_send_body() }
($agent:expr, POST, $uri:expr) => { $agent.post($uri) }
($agent:expr, PUT, $uri:expr) => { $agent.put($uri) }
($agent:expr, DELETE, $uri:expr) => { $agent.delete($uri).force_send_body() }
}

fn get_header_map<B>(response: &http::Response<B>) -> 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());
let names = response.headers();
for (name, value) in names {
if let Ok(value) = value.to_str() {
headers.insert(name.as_str().into(), value.into());
}
}

headers
}


#[derive(Debug, Clone)]
pub struct ResponseContent<T> {
pub status: u16,
Expand All @@ -28,39 +34,43 @@ pub struct ResponseContent<T> {
}

impl<T> ResponseContent<T> {
fn new<F, E>(response: ureq::Response, f: F) -> Result<Self, Error<E>>
fn new<F, E>(response: http::Response<ureq::Body>, f: F) -> Result<Self, Error<E>>
where
F: FnOnce(&[u8]) -> Result<T, Error<E>>,
{
let status = response.status();
use std::io::Read;
let status = response.status().as_u16();
let headers = get_header_map(&response);
let mut content = Vec::new();
response.into_reader().read_to_end(&mut content)?;
response
.into_body()
.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>> {
fn unit<E>(response: http::Response<ureq::Body>) -> Result<Self, Error<E>> {
Self::new(response, |_| Ok(()))
}
}

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

impl ResponseContent<String> {
fn string<E>(response: ureq::Response) -> Result<Self, Error<E>> {
fn string<E>(response: http::Response<ureq::Body>) -> 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>> {
fn deserialized<E>(response: http::Response<ureq::Body>) -> Result<Self, Error<E>> {
Self::new(response, |content| serde_json::from_slice(content).map_err(From::from))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ 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: ureq::agent(),
client: ureq::config::Config::builder()
.http_status_as_error(false)
.build()
.new_agent(),
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
Expand Down
5 changes: 4 additions & 1 deletion src/apis/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ 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: ureq::agent(),
client: ureq::config::Config::builder()
.http_status_as_error(false)
.build()
.new_agent(),
basic_auth: None,
oauth_access_token: None,
bearer_access_token: None,
Expand Down
Loading

0 comments on commit 791e740

Please sign in to comment.