From e3df57d70050a8952c35a7a0b11977aea74b7441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Courtine?= Date: Fri, 11 Jan 2019 16:20:26 +0100 Subject: [PATCH 1/6] Resolves #525 for Rust client generator with reqwest library. --- .../src/main/resources/rust/reqwest/api.mustache | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache index 091c40fe6253..dc02a0329a45 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache @@ -1,6 +1,9 @@ {{>partial_header}} use std::rc::Rc; use std::borrow::Borrow; +{{#hasFormParams}} +use std::collections::HashMap; +{{/hasFormParams}} use reqwest; @@ -90,6 +93,19 @@ impl {{{classname}}} for {{{classname}}}Client { {{/authMethods}} {{/hasAuthMethods}} + {{#hasFormParams}} + let mut form_params = HashMap::new(); + {{#formParams}} + {{#isFile}} + form_params.insert("{{{baseName}}}".to_string(), unimplemented!()); + {{/isFile}} + {{^isFile}} + form_params.insert("{{{baseName}}}".to_string(), {{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}.to_string()); + {{/isFile}} + {{/formParams}} + req_builder = req_builder.form(&form_params); + {{/hasFormParams}} + {{#hasBodyParam}} {{#bodyParams}} req_builder = req_builder.json(&{{{paramName}}}); From cf9e0dee4bda9b41b7ee613e39534d823967ea28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Courtine?= Date: Fri, 11 Jan 2019 16:39:53 +0100 Subject: [PATCH 2/6] Use Reqwest "query" method to generate query URL. --- .../main/resources/rust/reqwest/api.mustache | 31 ++++++++----------- .../resources/rust/reqwest/api_mod.mustache | 1 + 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache index dc02a0329a45..96c86af3c932 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache @@ -1,9 +1,8 @@ {{>partial_header}} use std::rc::Rc; use std::borrow::Borrow; -{{#hasFormParams}} +#[allow(unused_imports)] use std::collections::HashMap; -{{/hasFormParams}} use reqwest; @@ -37,26 +36,22 @@ impl {{{classname}}} for {{{classname}}}Client { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}{{{path}}}", configuration.base_path{{#pathParams}}, {{{baseName}}}={{{paramName}}}{{#isListContainer}}.join(",").as_ref(){{/isListContainer}}{{/pathParams}}); + let mut req_builder = client.{{{httpMethod}}}(uri_str.as_str()); + {{#queryParams}} - query.append_pair("{{{baseName}}}", &{{{paramName}}}{{#isListContainer}}.into_iter().map(|p| p.to_string()).collect::>().join(","){{/isListContainer}}.to_string()); + req_builder = req_builder.query(&[("{{{baseName}}}", &{{{paramName}}}{{#isListContainer}}.into_iter().map(|p| p.to_string()).collect::>().join(","){{/isListContainer}}.to_string())]); {{/queryParams}} {{#hasAuthMethods}}{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}} - if let Some(ref apikey) = configuration.api_key { - let key = apikey.key.clone(); - let val = match apikey.prefix { - Some(ref prefix) => format!("{} {}", prefix, key), - None => key, - }; - query.append_pair("{{{keyParamName}}}".to_owned(), val); - } + if let Some(ref apikey) = configuration.api_key { + let key = apikey.key.clone(); + let val = match apikey.prefix { + Some(ref prefix) => format!("{} {}", prefix, key), + None => key, + }; + req_builder = req_builder.query(&[("{{{keyParamName}}}", val)]); + } {{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}{{/hasAuthMethods}} - query.finish() - }; - let uri_str = format!("{}{{{path}}}?{}", configuration.base_path, query_string{{#pathParams}}, {{{baseName}}}={{{paramName}}}{{#isListContainer}}.join(",").as_ref(){{/isListContainer}}{{/pathParams}}); - - let mut req_builder = client.{{{httpMethod}}}(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache index 483e2c388f8a..452ae1f9a3e5 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache @@ -19,6 +19,7 @@ impl From for Error { } } +#[allow(unused_imports)] use super::models::*; {{#apiInfo}} From f3bbf4fe1ff662db7897bbb9d5acfb20168bb725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Courtine?= Date: Fri, 11 Jan 2019 17:14:39 +0100 Subject: [PATCH 3/6] urlencode URL string parameters. --- .../src/main/resources/rust/reqwest/api.mustache | 6 ++---- .../src/main/resources/rust/reqwest/api_mod.mustache | 5 ++++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache index 96c86af3c932..b2456d3c5b75 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache @@ -1,12 +1,11 @@ {{>partial_header}} use std::rc::Rc; use std::borrow::Borrow; -#[allow(unused_imports)] use std::collections::HashMap; use reqwest; -use super::{Error, configuration}; +use super::{Error, configuration, urlencode}; pub struct {{{classname}}}Client { configuration: Rc, @@ -28,7 +27,6 @@ pub trait {{{classname}}} { {{/operations}} } - impl {{{classname}}} for {{{classname}}}Client { {{#operations}} {{#operation}} @@ -36,7 +34,7 @@ impl {{{classname}}} for {{{classname}}}Client { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let uri_str = format!("{}{{{path}}}", configuration.base_path{{#pathParams}}, {{{baseName}}}={{{paramName}}}{{#isListContainer}}.join(",").as_ref(){{/isListContainer}}{{/pathParams}}); + let uri_str = format!("{}{{{path}}}", configuration.base_path{{#pathParams}}, {{{baseName}}}={{#isString}}urlencode({{/isString}}{{{paramName}}}{{#isListContainer}}.join(",").as_ref(){{/isListContainer}}{{#isString}}){{/isString}}{{/pathParams}}); let mut req_builder = client.{{{httpMethod}}}(uri_str.as_str()); {{#queryParams}} diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache index 452ae1f9a3e5..132521458f91 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache @@ -19,7 +19,10 @@ impl From for Error { } } -#[allow(unused_imports)] +pub fn urlencode>(s: T) -> String { + ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect() +} + use super::models::*; {{#apiInfo}} From 08fb48b5c209f5e3a2fb512ae1f7518ad20984b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Courtine?= Date: Fri, 11 Jan 2019 17:15:44 +0100 Subject: [PATCH 4/6] Generate rust-reqwest client, and verify it compiles and work as intended. --- .../rust-reqwest/.openapi-generator/VERSION | 2 +- .../petstore/rust-reqwest/docs/PetApi.md | 8 +- .../petstore/rust-reqwest/docs/StoreApi.md | 4 +- .../petstore/rust-reqwest/docs/UserApi.md | 16 +-- .../petstore/rust-reqwest/src/apis/mod.rs | 4 + .../petstore/rust-reqwest/src/apis/pet_api.rs | 100 +++++++---------- .../rust-reqwest/src/apis/store_api.rs | 46 +++----- .../rust-reqwest/src/apis/user_api.rs | 104 +++++++----------- 8 files changed, 118 insertions(+), 166 deletions(-) diff --git a/samples/client/petstore/rust-reqwest/.openapi-generator/VERSION b/samples/client/petstore/rust-reqwest/.openapi-generator/VERSION index d077ffb477a4..afa636560641 100644 --- a/samples/client/petstore/rust-reqwest/.openapi-generator/VERSION +++ b/samples/client/petstore/rust-reqwest/.openapi-generator/VERSION @@ -1 +1 @@ -3.3.4-SNAPSHOT \ No newline at end of file +4.0.0-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/rust-reqwest/docs/PetApi.md b/samples/client/petstore/rust-reqwest/docs/PetApi.md index ea3b5825627f..96f84a7e6048 100644 --- a/samples/client/petstore/rust-reqwest/docs/PetApi.md +++ b/samples/client/petstore/rust-reqwest/docs/PetApi.md @@ -15,7 +15,7 @@ Method | HTTP request | Description # **add_pet** -> add_pet(ctx, pet) +> add_pet(ctx, body) Add a new pet to the store ### Required Parameters @@ -23,7 +23,7 @@ Add a new pet to the store Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- **ctx** | **context.Context** | context containing the authentication | nil if no authentication - **pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | + **body** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | ### Return type @@ -160,7 +160,7 @@ Name | Type | Description | Notes [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) # **update_pet** -> update_pet(ctx, pet) +> update_pet(ctx, body) Update an existing pet ### Required Parameters @@ -168,7 +168,7 @@ Update an existing pet Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- **ctx** | **context.Context** | context containing the authentication | nil if no authentication - **pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | + **body** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | ### Return type diff --git a/samples/client/petstore/rust-reqwest/docs/StoreApi.md b/samples/client/petstore/rust-reqwest/docs/StoreApi.md index a4555a255652..7451dacfd6d0 100644 --- a/samples/client/petstore/rust-reqwest/docs/StoreApi.md +++ b/samples/client/petstore/rust-reqwest/docs/StoreApi.md @@ -89,14 +89,14 @@ No authorization required [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) # **place_order** -> ::models::Order place_order(order) +> ::models::Order place_order(body) Place an order for a pet ### Required Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **order** | [**Order**](Order.md)| order placed for purchasing the pet | + **body** | [**Order**](Order.md)| order placed for purchasing the pet | ### Return type diff --git a/samples/client/petstore/rust-reqwest/docs/UserApi.md b/samples/client/petstore/rust-reqwest/docs/UserApi.md index 05d2aeccaa3b..c6353d423e39 100644 --- a/samples/client/petstore/rust-reqwest/docs/UserApi.md +++ b/samples/client/petstore/rust-reqwest/docs/UserApi.md @@ -15,7 +15,7 @@ Method | HTTP request | Description # **create_user** -> create_user(user) +> create_user(body) Create user This can only be done by the logged in user. @@ -24,7 +24,7 @@ This can only be done by the logged in user. Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **user** | [**User**](User.md)| Created user object | + **body** | [**User**](User.md)| Created user object | ### Return type @@ -42,14 +42,14 @@ No authorization required [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) # **create_users_with_array_input** -> create_users_with_array_input(user) +> create_users_with_array_input(body) Creates list of users with given input array ### Required Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **user** | [**Vec<::models::User>**](array.md)| List of user object | + **body** | [**Vec<::models::User>**](array.md)| List of user object | ### Return type @@ -67,14 +67,14 @@ No authorization required [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) # **create_users_with_list_input** -> create_users_with_list_input(user) +> create_users_with_list_input(body) Creates list of users with given input array ### Required Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **user** | [**Vec<::models::User>**](array.md)| List of user object | + **body** | [**Vec<::models::User>**](array.md)| List of user object | ### Return type @@ -192,7 +192,7 @@ No authorization required [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) # **update_user** -> update_user(username, user) +> update_user(username, body) Updated user This can only be done by the logged in user. @@ -202,7 +202,7 @@ This can only be done by the logged in user. Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- **username** | **String**| name that need to be deleted | - **user** | [**User**](User.md)| Updated user object | + **body** | [**User**](User.md)| Updated user object | ### Return type diff --git a/samples/client/petstore/rust-reqwest/src/apis/mod.rs b/samples/client/petstore/rust-reqwest/src/apis/mod.rs index 7494e25ce9e7..bd14b2994da6 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/mod.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/mod.rs @@ -19,6 +19,10 @@ impl From for Error { } } +pub fn urlencode>(s: T) -> String { + ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect() +} + use super::models::*; mod pet_api; diff --git a/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs b/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs index 7771c6568d60..310d587be7fd 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs @@ -10,10 +10,11 @@ use std::rc::Rc; use std::borrow::Borrow; +use std::collections::HashMap; use reqwest; -use super::{Error, configuration}; +use super::{Error, configuration, urlencode}; pub struct PetApiClient { configuration: Rc, @@ -28,30 +29,25 @@ impl PetApiClient { } pub trait PetApi { - fn add_pet(&self, pet: ::models::Pet) -> Result<(), Error>; + fn add_pet(&self, body: ::models::Pet) -> Result<(), Error>; fn delete_pet(&self, pet_id: i64, api_key: &str) -> Result<(), Error>; fn find_pets_by_status(&self, status: Vec) -> Result, Error>; fn find_pets_by_tags(&self, tags: Vec) -> Result, Error>; fn get_pet_by_id(&self, pet_id: i64) -> Result<::models::Pet, Error>; - fn update_pet(&self, pet: ::models::Pet) -> Result<(), Error>; + fn update_pet(&self, body: ::models::Pet) -> Result<(), Error>; fn update_pet_with_form(&self, pet_id: i64, name: &str, status: &str) -> Result<(), Error>; fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: ::models::File) -> Result<::models::ApiResponse, Error>; } - impl PetApi for PetApiClient { - fn add_pet(&self, pet: ::models::Pet) -> Result<(), Error> { + fn add_pet(&self, body: ::models::Pet) -> Result<(), Error> { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/pet", configuration.base_path); + let mut req_builder = client.post(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/pet?{}", configuration.base_path, query_string); - let mut req_builder = client.post(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -63,7 +59,8 @@ impl PetApi for PetApiClient { req_builder = req_builder.bearer_auth(token.to_owned()); }; - req_builder = req_builder.json(&pet); + + req_builder = req_builder.json(&body); // send request let req = req_builder.build()?; @@ -76,14 +73,10 @@ impl PetApi for PetApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=pet_id); + let mut req_builder = client.delete(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/pet/{petId}?{}", configuration.base_path, query_string, petId=pet_id); - let mut req_builder = client.delete(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -97,6 +90,7 @@ impl PetApi for PetApiClient { }; + // send request let req = req_builder.build()?; @@ -108,15 +102,11 @@ impl PetApi for PetApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - query.append_pair("status", &status.into_iter().map(|p| p.to_string()).collect::>().join(",").to_string()); + let uri_str = format!("{}/pet/findByStatus", configuration.base_path); + let mut req_builder = client.get(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/pet/findByStatus?{}", configuration.base_path, query_string); + req_builder = req_builder.query(&[("status", &status.into_iter().map(|p| p.to_string()).collect::>().join(",").to_string())]); - let mut req_builder = client.get(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -129,6 +119,7 @@ impl PetApi for PetApiClient { }; + // send request let req = req_builder.build()?; @@ -139,15 +130,11 @@ impl PetApi for PetApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - query.append_pair("tags", &tags.into_iter().map(|p| p.to_string()).collect::>().join(",").to_string()); + let uri_str = format!("{}/pet/findByTags", configuration.base_path); + let mut req_builder = client.get(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/pet/findByTags?{}", configuration.base_path, query_string); + req_builder = req_builder.query(&[("tags", &tags.into_iter().map(|p| p.to_string()).collect::>().join(",").to_string())]); - let mut req_builder = client.get(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -160,6 +147,7 @@ impl PetApi for PetApiClient { }; + // send request let req = req_builder.build()?; @@ -170,14 +158,10 @@ impl PetApi for PetApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=pet_id); + let mut req_builder = client.get(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/pet/{petId}?{}", configuration.base_path, query_string, petId=pet_id); - let mut req_builder = client.get(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -196,24 +180,21 @@ impl PetApi for PetApiClient { + // send request let req = req_builder.build()?; Ok(client.execute(req)?.error_for_status()?.json()?) } - fn update_pet(&self, pet: ::models::Pet) -> Result<(), Error> { + fn update_pet(&self, body: ::models::Pet) -> Result<(), Error> { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/pet", configuration.base_path); + let mut req_builder = client.put(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/pet?{}", configuration.base_path, query_string); - let mut req_builder = client.put(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -225,7 +206,8 @@ impl PetApi for PetApiClient { req_builder = req_builder.bearer_auth(token.to_owned()); }; - req_builder = req_builder.json(&pet); + + req_builder = req_builder.json(&body); // send request let req = req_builder.build()?; @@ -238,14 +220,10 @@ impl PetApi for PetApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=pet_id); + let mut req_builder = client.post(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/pet/{petId}?{}", configuration.base_path, query_string, petId=pet_id); - let mut req_builder = client.post(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -257,6 +235,11 @@ impl PetApi for PetApiClient { req_builder = req_builder.bearer_auth(token.to_owned()); }; + let mut form_params = HashMap::new(); + form_params.insert("name".to_string(), name.to_string()); + form_params.insert("status".to_string(), status.to_string()); + req_builder = req_builder.form(&form_params); + // send request let req = req_builder.build()?; @@ -269,14 +252,10 @@ impl PetApi for PetApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/pet/{petId}/uploadImage", configuration.base_path, petId=pet_id); + let mut req_builder = client.post(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/pet/{petId}/uploadImage?{}", configuration.base_path, query_string, petId=pet_id); - let mut req_builder = client.post(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -288,6 +267,11 @@ impl PetApi for PetApiClient { req_builder = req_builder.bearer_auth(token.to_owned()); }; + let mut form_params = HashMap::new(); + form_params.insert("additionalMetadata".to_string(), additional_metadata.to_string()); + form_params.insert("file".to_string(), unimplemented!()); + req_builder = req_builder.form(&form_params); + // send request let req = req_builder.build()?; diff --git a/samples/client/petstore/rust-reqwest/src/apis/store_api.rs b/samples/client/petstore/rust-reqwest/src/apis/store_api.rs index 90a719df9293..0fa7edcfc1d2 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/store_api.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/store_api.rs @@ -10,10 +10,11 @@ use std::rc::Rc; use std::borrow::Borrow; +use std::collections::HashMap; use reqwest; -use super::{Error, configuration}; +use super::{Error, configuration, urlencode}; pub struct StoreApiClient { configuration: Rc, @@ -31,23 +32,18 @@ pub trait StoreApi { fn delete_order(&self, order_id: &str) -> Result<(), Error>; fn get_inventory(&self, ) -> Result<::std::collections::HashMap, Error>; fn get_order_by_id(&self, order_id: i64) -> Result<::models::Order, Error>; - fn place_order(&self, order: ::models::Order) -> Result<::models::Order, Error>; + fn place_order(&self, body: ::models::Order) -> Result<::models::Order, Error>; } - impl StoreApi for StoreApiClient { fn delete_order(&self, order_id: &str) -> Result<(), Error> { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/store/order/{orderId}", configuration.base_path, orderId=urlencode(order_id)); + let mut req_builder = client.delete(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/store/order/{orderId}?{}", configuration.base_path, query_string, orderId=order_id); - let mut req_builder = client.delete(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -56,6 +52,7 @@ impl StoreApi for StoreApiClient { + // send request let req = req_builder.build()?; @@ -67,14 +64,10 @@ impl StoreApi for StoreApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/store/inventory", configuration.base_path); + let mut req_builder = client.get(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/store/inventory?{}", configuration.base_path, query_string); - let mut req_builder = client.get(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -93,6 +86,7 @@ impl StoreApi for StoreApiClient { + // send request let req = req_builder.build()?; @@ -103,14 +97,10 @@ impl StoreApi for StoreApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/store/order/{orderId}", configuration.base_path, orderId=order_id); + let mut req_builder = client.get(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/store/order/{orderId}?{}", configuration.base_path, query_string, orderId=order_id); - let mut req_builder = client.get(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -119,24 +109,21 @@ impl StoreApi for StoreApiClient { + // send request let req = req_builder.build()?; Ok(client.execute(req)?.error_for_status()?.json()?) } - fn place_order(&self, order: ::models::Order) -> Result<::models::Order, Error> { + fn place_order(&self, body: ::models::Order) -> Result<::models::Order, Error> { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/store/order", configuration.base_path); + let mut req_builder = client.post(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/store/order?{}", configuration.base_path, query_string); - let mut req_builder = client.post(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -144,7 +131,8 @@ impl StoreApi for StoreApiClient { - req_builder = req_builder.json(&order); + + req_builder = req_builder.json(&body); // send request let req = req_builder.build()?; diff --git a/samples/client/petstore/rust-reqwest/src/apis/user_api.rs b/samples/client/petstore/rust-reqwest/src/apis/user_api.rs index 8d14e2dc5cee..7131969bbf11 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/user_api.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/user_api.rs @@ -10,10 +10,11 @@ use std::rc::Rc; use std::borrow::Borrow; +use std::collections::HashMap; use reqwest; -use super::{Error, configuration}; +use super::{Error, configuration, urlencode}; pub struct UserApiClient { configuration: Rc, @@ -28,30 +29,25 @@ impl UserApiClient { } pub trait UserApi { - fn create_user(&self, user: ::models::User) -> Result<(), Error>; - fn create_users_with_array_input(&self, user: Vec<::models::User>) -> Result<(), Error>; - fn create_users_with_list_input(&self, user: Vec<::models::User>) -> Result<(), Error>; + fn create_user(&self, body: ::models::User) -> Result<(), Error>; + fn create_users_with_array_input(&self, body: Vec<::models::User>) -> Result<(), Error>; + fn create_users_with_list_input(&self, body: Vec<::models::User>) -> Result<(), Error>; fn delete_user(&self, username: &str) -> Result<(), Error>; fn get_user_by_name(&self, username: &str) -> Result<::models::User, Error>; fn login_user(&self, username: &str, password: &str) -> Result; fn logout_user(&self, ) -> Result<(), Error>; - fn update_user(&self, username: &str, user: ::models::User) -> Result<(), Error>; + fn update_user(&self, username: &str, body: ::models::User) -> Result<(), Error>; } - impl UserApi for UserApiClient { - fn create_user(&self, user: ::models::User) -> Result<(), Error> { + fn create_user(&self, body: ::models::User) -> Result<(), Error> { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/user", configuration.base_path); + let mut req_builder = client.post(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/user?{}", configuration.base_path, query_string); - let mut req_builder = client.post(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -59,7 +55,8 @@ impl UserApi for UserApiClient { - req_builder = req_builder.json(&user); + + req_builder = req_builder.json(&body); // send request let req = req_builder.build()?; @@ -68,18 +65,14 @@ impl UserApi for UserApiClient { Ok(()) } - fn create_users_with_array_input(&self, user: Vec<::models::User>) -> Result<(), Error> { + fn create_users_with_array_input(&self, body: Vec<::models::User>) -> Result<(), Error> { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/user/createWithArray", configuration.base_path); + let mut req_builder = client.post(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/user/createWithArray?{}", configuration.base_path, query_string); - let mut req_builder = client.post(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -87,7 +80,8 @@ impl UserApi for UserApiClient { - req_builder = req_builder.json(&user); + + req_builder = req_builder.json(&body); // send request let req = req_builder.build()?; @@ -96,18 +90,14 @@ impl UserApi for UserApiClient { Ok(()) } - fn create_users_with_list_input(&self, user: Vec<::models::User>) -> Result<(), Error> { + fn create_users_with_list_input(&self, body: Vec<::models::User>) -> Result<(), Error> { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/user/createWithList", configuration.base_path); + let mut req_builder = client.post(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/user/createWithList?{}", configuration.base_path, query_string); - let mut req_builder = client.post(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -115,7 +105,8 @@ impl UserApi for UserApiClient { - req_builder = req_builder.json(&user); + + req_builder = req_builder.json(&body); // send request let req = req_builder.build()?; @@ -128,14 +119,10 @@ impl UserApi for UserApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/user/{username}", configuration.base_path, username=urlencode(username)); + let mut req_builder = client.delete(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/user/{username}?{}", configuration.base_path, query_string, username=username); - let mut req_builder = client.delete(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -144,6 +131,7 @@ impl UserApi for UserApiClient { + // send request let req = req_builder.build()?; @@ -155,14 +143,10 @@ impl UserApi for UserApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/user/{username}", configuration.base_path, username=urlencode(username)); + let mut req_builder = client.get(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/user/{username}?{}", configuration.base_path, query_string, username=username); - let mut req_builder = client.get(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -171,6 +155,7 @@ impl UserApi for UserApiClient { + // send request let req = req_builder.build()?; @@ -181,16 +166,12 @@ impl UserApi for UserApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - query.append_pair("username", &username.to_string()); - query.append_pair("password", &password.to_string()); + let uri_str = format!("{}/user/login", configuration.base_path); + let mut req_builder = client.get(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/user/login?{}", configuration.base_path, query_string); + req_builder = req_builder.query(&[("username", &username.to_string())]); + req_builder = req_builder.query(&[("password", &password.to_string())]); - let mut req_builder = client.get(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -199,6 +180,7 @@ impl UserApi for UserApiClient { + // send request let req = req_builder.build()?; @@ -209,14 +191,10 @@ impl UserApi for UserApiClient { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/user/logout", configuration.base_path); + let mut req_builder = client.get(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/user/logout?{}", configuration.base_path, query_string); - let mut req_builder = client.get(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -225,6 +203,7 @@ impl UserApi for UserApiClient { + // send request let req = req_builder.build()?; @@ -232,18 +211,14 @@ impl UserApi for UserApiClient { Ok(()) } - fn update_user(&self, username: &str, user: ::models::User) -> Result<(), Error> { + fn update_user(&self, username: &str, body: ::models::User) -> Result<(), Error> { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; - let query_string = { - let mut query = ::url::form_urlencoded::Serializer::new(String::new()); + let uri_str = format!("{}/user/{username}", configuration.base_path, username=urlencode(username)); + let mut req_builder = client.put(uri_str.as_str()); - query.finish() - }; - let uri_str = format!("{}/user/{username}?{}", configuration.base_path, query_string, username=username); - let mut req_builder = client.put(uri_str.as_str()); if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); @@ -251,7 +226,8 @@ impl UserApi for UserApiClient { - req_builder = req_builder.json(&user); + + req_builder = req_builder.json(&body); // send request let req = req_builder.build()?; From 1d5525a49fb4e814aae505a563d88977847da3e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Courtine?= Date: Sat, 19 Jan 2019 18:23:28 +0100 Subject: [PATCH 5/6] Map file params (to "&std::path::Path") and support multipart operations (with file params) in Reqwest library. --- .../codegen/languages/RustClientCodegen.java | 11 +-- .../main/resources/rust/reqwest/api.mustache | 48 +++++---- .../resources/rust/reqwest/api_mod.mustache | 7 ++ .../petstore/rust-reqwest/docs/PetApi.md | 2 +- .../petstore/rust-reqwest/src/apis/mod.rs | 7 ++ .../petstore/rust-reqwest/src/apis/pet_api.rs | 98 +++++++++++++++---- .../rust-reqwest/src/apis/store_api.rs | 35 ++++++- .../rust-reqwest/src/apis/user_api.rs | 69 +++++++++++-- .../petstore/rust/.openapi-generator/VERSION | 2 +- samples/client/petstore/rust/docs/PetApi.md | 10 +- samples/client/petstore/rust/docs/StoreApi.md | 4 +- samples/client/petstore/rust/docs/UserApi.md | 16 +-- .../client/petstore/rust/src/apis/pet_api.rs | 16 +-- .../petstore/rust/src/apis/store_api.rs | 6 +- .../client/petstore/rust/src/apis/user_api.rs | 24 ++--- 15 files changed, 255 insertions(+), 100 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java index c6f9da87a6cc..c3636e215d6c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java @@ -119,8 +119,11 @@ public RustClientCodegen() { typeMapping.put("date", "string"); typeMapping.put("DateTime", "String"); typeMapping.put("password", "String"); - // TODO(farcaller): map file - typeMapping.put("file", "::models::File"); + // TODO(bcourtine): review file mapping. + // I tried to map as "std::io::File", but Reqwest multipart file requires a "AsRef" param. + // Getting a file from a Path is simple, but the opposite is difficult. So I map as "std::path::Path". + // Reference is required here because Path does not implement the "Sized" trait. + typeMapping.put("file", "&std::path::Path"); typeMapping.put("binary", "::models::File"); typeMapping.put("ByteArray", "String"); typeMapping.put("object", "Value"); @@ -379,7 +382,6 @@ public Map postProcessOperationsWithModels(Map o Map objectMap = (Map) objs.get("operations"); @SuppressWarnings("unchecked") List operations = (List) objectMap.get("operation"); - Set headerKeys = new HashSet<>(); for (CodegenOperation operation : operations) { // http method verb conversion, depending on client library (e.g. Hyper: PUT => Put, Reqwest: PUT => put) if (HYPER_LIBRARY.equals(getLibrary())) { @@ -439,9 +441,6 @@ public Map postProcessOperationsWithModels(Map o }*/ } - additionalProperties.put("headerKeys", headerKeys); - additionalProperties.putIfAbsent("authHeaderKey", "api-key"); - return objs; } diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache index b2456d3c5b75..4f05642c2365 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache @@ -1,7 +1,6 @@ {{>partial_header}} use std::rc::Rc; use std::borrow::Borrow; -use std::collections::HashMap; use reqwest; @@ -37,10 +36,10 @@ impl {{{classname}}} for {{{classname}}}Client { let uri_str = format!("{}{{{path}}}", configuration.base_path{{#pathParams}}, {{{baseName}}}={{#isString}}urlencode({{/isString}}{{{paramName}}}{{#isListContainer}}.join(",").as_ref(){{/isListContainer}}{{#isString}}){{/isString}}{{/pathParams}}); let mut req_builder = client.{{{httpMethod}}}(uri_str.as_str()); -{{#queryParams}} + {{#queryParams}} req_builder = req_builder.query(&[("{{{baseName}}}", &{{{paramName}}}{{#isListContainer}}.into_iter().map(|p| p.to_string()).collect::>().join(","){{/isListContainer}}.to_string())]); -{{/queryParams}} -{{#hasAuthMethods}}{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}} + {{/queryParams}} + {{#hasAuthMethods}}{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}} if let Some(ref apikey) = configuration.api_key { let key = apikey.key.clone(); let val = match apikey.prefix { @@ -49,20 +48,17 @@ impl {{{classname}}} for {{{classname}}}Client { }; req_builder = req_builder.query(&[("{{{keyParamName}}}", val)]); } -{{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}{{/hasAuthMethods}} + {{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}{{/hasAuthMethods}} if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - {{#hasHeaderParams}} - {{#headerParams}} + {{#hasHeaderParams}}{{#headerParams}} req_builder = req_builder.header("{{{baseName}}}", {{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}.to_string()); - {{/headerParams}} - {{/hasHeaderParams}} + {{/headerParams}}{{/hasHeaderParams}} - {{#hasAuthMethods}} - {{#authMethods}} + {{#hasAuthMethods}}{{#authMethods}} {{#isApiKey}}{{#isKeyInHeader}} if let Some(ref apikey) = configuration.api_key { let key = apikey.key.clone(); @@ -83,27 +79,37 @@ impl {{{classname}}} for {{{classname}}}Client { req_builder = req_builder.bearer_auth(token.to_owned()); }; {{/isOAuth}} - {{/authMethods}} - {{/hasAuthMethods}} + {{/authMethods}}{{/hasAuthMethods}} - {{#hasFormParams}} - let mut form_params = HashMap::new(); + {{#isMultipart}}{{#hasFormParams}} + let form = reqwest::multipart::Form::new() {{#formParams}} {{#isFile}} - form_params.insert("{{{baseName}}}".to_string(), unimplemented!()); + .file("{{{baseName}}}", {{{paramName}}})?{{#-last}};{{/-last}} + {{/isFile}} + {{^isFile}} + .text("{{{baseName}}}", {{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}.to_string()){{#-last}};{{/-last}} + {{/isFile}} + {{/formParams}} + req_builder = req_builder.multipart(form); + {{/hasFormParams}}{{/isMultipart}} + + {{^isMultipart}}{{#hasFormParams}} + let mut form_params = std::collections::HashMap::new(); + {{#formParams}} + {{#isFile}} + form_params.insert("{{{baseName}}}".to_string(), unimplemented!("File form param not supported with x-www-form-urlencoded content")); {{/isFile}} {{^isFile}} form_params.insert("{{{baseName}}}".to_string(), {{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}.to_string()); {{/isFile}} {{/formParams}} req_builder = req_builder.form(&form_params); - {{/hasFormParams}} + {{/hasFormParams}}{{/isMultipart}} - {{#hasBodyParam}} - {{#bodyParams}} + {{#hasBodyParam}}{{#bodyParams}} req_builder = req_builder.json(&{{{paramName}}}); - {{/bodyParams}} - {{/hasBodyParam}} + {{/bodyParams}}{{/hasBodyParam}} // send request let req = req_builder.build()?; diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache index 132521458f91..80c30e7dcb6f 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache @@ -5,6 +5,7 @@ use serde_json; pub enum Error { Reqwest(reqwest::Error), Serde(serde_json::Error), + Io(std::io::Error), } impl From for Error { @@ -19,6 +20,12 @@ impl From for Error { } } +impl From for Error { + fn from(e: std::io::Error) -> Self { + return Error::Io(e) + } +} + pub fn urlencode>(s: T) -> String { ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect() } diff --git a/samples/client/petstore/rust-reqwest/docs/PetApi.md b/samples/client/petstore/rust-reqwest/docs/PetApi.md index 96f84a7e6048..89a95eebaf19 100644 --- a/samples/client/petstore/rust-reqwest/docs/PetApi.md +++ b/samples/client/petstore/rust-reqwest/docs/PetApi.md @@ -240,7 +240,7 @@ Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- **pet_id** | **i64**| ID of pet to update | **additional_metadata** | **String**| Additional data to pass to server | - **file** | **::models::File**| file to upload | + **file** | **&std::path::Path**| file to upload | ### Return type diff --git a/samples/client/petstore/rust-reqwest/src/apis/mod.rs b/samples/client/petstore/rust-reqwest/src/apis/mod.rs index bd14b2994da6..99d5b323764a 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/mod.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/mod.rs @@ -5,6 +5,7 @@ use serde_json; pub enum Error { Reqwest(reqwest::Error), Serde(serde_json::Error), + Io(std::io::Error), } impl From for Error { @@ -19,6 +20,12 @@ impl From for Error { } } +impl From for Error { + fn from(e: std::io::Error) -> Self { + return Error::Io(e) + } +} + pub fn urlencode>(s: T) -> String { ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect() } diff --git a/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs b/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs index 310d587be7fd..bcfe9a3636e1 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs @@ -1,16 +1,15 @@ -/* +/* * OpenAPI Petstore * * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. * * OpenAPI spec version: 1.0.0 - * + * * Generated by: https://openapi-generator.tech */ use std::rc::Rc; use std::borrow::Borrow; -use std::collections::HashMap; use reqwest; @@ -36,7 +35,7 @@ pub trait PetApi { fn get_pet_by_id(&self, pet_id: i64) -> Result<::models::Pet, Error>; fn update_pet(&self, body: ::models::Pet) -> Result<(), Error>; fn update_pet_with_form(&self, pet_id: i64, name: &str, status: &str) -> Result<(), Error>; - fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: ::models::File) -> Result<::models::ApiResponse, Error>; + fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: &std::path::Path) -> Result<::models::ApiResponse, Error>; } impl PetApi for PetApiClient { @@ -54,14 +53,22 @@ impl PetApi for PetApiClient { } - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; + + + + + req_builder = req_builder.json(&body); + // send request let req = req_builder.build()?; @@ -82,15 +89,23 @@ impl PetApi for PetApiClient { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + req_builder = req_builder.header("api_key", api_key.to_string()); - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; + + + + + // send request let req = req_builder.build()?; @@ -113,13 +128,20 @@ impl PetApi for PetApiClient { } - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; + + + + + // send request let req = req_builder.build()?; @@ -141,13 +163,20 @@ impl PetApi for PetApiClient { } - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; + + + + + // send request let req = req_builder.build()?; @@ -168,7 +197,9 @@ impl PetApi for PetApiClient { } - + + + if let Some(ref apikey) = configuration.api_key { let key = apikey.key.clone(); let val = match apikey.prefix { @@ -177,7 +208,12 @@ impl PetApi for PetApiClient { }; req_builder = req_builder.header("api_key", val); }; - + + + + + + @@ -201,14 +237,22 @@ impl PetApi for PetApiClient { } - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; + + + + + req_builder = req_builder.json(&body); + // send request let req = req_builder.build()?; @@ -230,17 +274,25 @@ impl PetApi for PetApiClient { } - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; - let mut form_params = HashMap::new(); + + + + + let mut form_params = std::collections::HashMap::new(); form_params.insert("name".to_string(), name.to_string()); form_params.insert("status".to_string(), status.to_string()); req_builder = req_builder.form(&form_params); + + // send request let req = req_builder.build()?; @@ -248,7 +300,7 @@ impl PetApi for PetApiClient { Ok(()) } - fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: ::models::File) -> Result<::models::ApiResponse, Error> { + fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: &std::path::Path) -> Result<::models::ApiResponse, Error> { let configuration: &configuration::Configuration = self.configuration.borrow(); let client = &configuration.client; @@ -262,15 +314,23 @@ impl PetApi for PetApiClient { } - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; - let mut form_params = HashMap::new(); - form_params.insert("additionalMetadata".to_string(), additional_metadata.to_string()); - form_params.insert("file".to_string(), unimplemented!()); - req_builder = req_builder.form(&form_params); + + + + + let mut form_params = reqwest::multipart::Form::new() + .text("additionalMetadata".to_string(), additional_metadata.to_string()) + .file("file".to_string(), file)?; + req_builder = req_builder.multipart(form_params); + + // send request diff --git a/samples/client/petstore/rust-reqwest/src/apis/store_api.rs b/samples/client/petstore/rust-reqwest/src/apis/store_api.rs index 0fa7edcfc1d2..9ffb9de798e6 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/store_api.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/store_api.rs @@ -10,7 +10,6 @@ use std::rc::Rc; use std::borrow::Borrow; -use std::collections::HashMap; use reqwest; @@ -43,15 +42,21 @@ impl StoreApi for StoreApiClient { let uri_str = format!("{}/store/order/{orderId}", configuration.base_path, orderId=urlencode(order_id)); let mut req_builder = client.delete(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + // send request let req = req_builder.build()?; @@ -67,14 +72,16 @@ impl StoreApi for StoreApiClient { let uri_str = format!("{}/store/inventory", configuration.base_path); let mut req_builder = client.get(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + if let Some(ref apikey) = configuration.api_key { let key = apikey.key.clone(); let val = match apikey.prefix { @@ -84,8 +91,13 @@ impl StoreApi for StoreApiClient { req_builder = req_builder.header("api_key", val); }; + + + + + // send request let req = req_builder.build()?; @@ -100,15 +112,21 @@ impl StoreApi for StoreApiClient { let uri_str = format!("{}/store/order/{orderId}", configuration.base_path, orderId=order_id); let mut req_builder = client.get(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + // send request let req = req_builder.build()?; @@ -123,16 +141,23 @@ impl StoreApi for StoreApiClient { let uri_str = format!("{}/store/order", configuration.base_path); let mut req_builder = client.post(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + req_builder = req_builder.json(&body); + // send request let req = req_builder.build()?; diff --git a/samples/client/petstore/rust-reqwest/src/apis/user_api.rs b/samples/client/petstore/rust-reqwest/src/apis/user_api.rs index 7131969bbf11..557274499957 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/user_api.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/user_api.rs @@ -10,7 +10,6 @@ use std::rc::Rc; use std::borrow::Borrow; -use std::collections::HashMap; use reqwest; @@ -47,16 +46,23 @@ impl UserApi for UserApiClient { let uri_str = format!("{}/user", configuration.base_path); let mut req_builder = client.post(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + req_builder = req_builder.json(&body); + // send request let req = req_builder.build()?; @@ -72,16 +78,23 @@ impl UserApi for UserApiClient { let uri_str = format!("{}/user/createWithArray", configuration.base_path); let mut req_builder = client.post(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + req_builder = req_builder.json(&body); + // send request let req = req_builder.build()?; @@ -97,16 +110,23 @@ impl UserApi for UserApiClient { let uri_str = format!("{}/user/createWithList", configuration.base_path); let mut req_builder = client.post(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + req_builder = req_builder.json(&body); + // send request let req = req_builder.build()?; @@ -122,15 +142,21 @@ impl UserApi for UserApiClient { let uri_str = format!("{}/user/{username}", configuration.base_path, username=urlencode(username)); let mut req_builder = client.delete(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + // send request let req = req_builder.build()?; @@ -146,15 +172,21 @@ impl UserApi for UserApiClient { let uri_str = format!("{}/user/{username}", configuration.base_path, username=urlencode(username)); let mut req_builder = client.get(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + // send request let req = req_builder.build()?; @@ -171,15 +203,21 @@ impl UserApi for UserApiClient { req_builder = req_builder.query(&[("username", &username.to_string())]); req_builder = req_builder.query(&[("password", &password.to_string())]); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + // send request let req = req_builder.build()?; @@ -194,15 +232,21 @@ impl UserApi for UserApiClient { let uri_str = format!("{}/user/logout", configuration.base_path); let mut req_builder = client.get(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + // send request let req = req_builder.build()?; @@ -218,16 +262,23 @@ impl UserApi for UserApiClient { let uri_str = format!("{}/user/{username}", configuration.base_path, username=urlencode(username)); let mut req_builder = client.put(uri_str.as_str()); - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } + + + + + + req_builder = req_builder.json(&body); + // send request let req = req_builder.build()?; diff --git a/samples/client/petstore/rust/.openapi-generator/VERSION b/samples/client/petstore/rust/.openapi-generator/VERSION index d077ffb477a4..afa636560641 100644 --- a/samples/client/petstore/rust/.openapi-generator/VERSION +++ b/samples/client/petstore/rust/.openapi-generator/VERSION @@ -1 +1 @@ -3.3.4-SNAPSHOT \ No newline at end of file +4.0.0-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/rust/docs/PetApi.md b/samples/client/petstore/rust/docs/PetApi.md index 333648af8949..1603f57f4eee 100644 --- a/samples/client/petstore/rust/docs/PetApi.md +++ b/samples/client/petstore/rust/docs/PetApi.md @@ -15,7 +15,7 @@ Method | HTTP request | Description # **add_pet** -> add_pet(ctx, pet) +> add_pet(ctx, body) Add a new pet to the store ### Required Parameters @@ -23,7 +23,7 @@ Add a new pet to the store Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- **ctx** | **context.Context** | context containing the authentication | nil if no authentication - **pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | + **body** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | ### Return type @@ -160,7 +160,7 @@ Name | Type | Description | Notes [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) # **update_pet** -> update_pet(ctx, pet) +> update_pet(ctx, body) Update an existing pet ### Required Parameters @@ -168,7 +168,7 @@ Update an existing pet Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- **ctx** | **context.Context** | context containing the authentication | nil if no authentication - **pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | + **body** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | ### Return type @@ -240,7 +240,7 @@ Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- **pet_id** | **i64**| ID of pet to update | **additional_metadata** | **String**| Additional data to pass to server | - **file** | **::models::File**| file to upload | + **file** | **&std::path::Path**| file to upload | ### Return type diff --git a/samples/client/petstore/rust/docs/StoreApi.md b/samples/client/petstore/rust/docs/StoreApi.md index 0563ec09e408..507a0c4300cd 100644 --- a/samples/client/petstore/rust/docs/StoreApi.md +++ b/samples/client/petstore/rust/docs/StoreApi.md @@ -89,14 +89,14 @@ No authorization required [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) # **place_order** -> ::models::Order place_order(order) +> ::models::Order place_order(body) Place an order for a pet ### Required Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **order** | [**Order**](Order.md)| order placed for purchasing the pet | + **body** | [**Order**](Order.md)| order placed for purchasing the pet | ### Return type diff --git a/samples/client/petstore/rust/docs/UserApi.md b/samples/client/petstore/rust/docs/UserApi.md index ea16bc1218e2..69e7bc506275 100644 --- a/samples/client/petstore/rust/docs/UserApi.md +++ b/samples/client/petstore/rust/docs/UserApi.md @@ -15,7 +15,7 @@ Method | HTTP request | Description # **create_user** -> create_user(user) +> create_user(body) Create user This can only be done by the logged in user. @@ -24,7 +24,7 @@ This can only be done by the logged in user. Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **user** | [**User**](User.md)| Created user object | + **body** | [**User**](User.md)| Created user object | ### Return type @@ -42,14 +42,14 @@ No authorization required [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) # **create_users_with_array_input** -> create_users_with_array_input(user) +> create_users_with_array_input(body) Creates list of users with given input array ### Required Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **user** | [**Vec<::models::User>**](array.md)| List of user object | + **body** | [**Vec<::models::User>**](array.md)| List of user object | ### Return type @@ -67,14 +67,14 @@ No authorization required [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) # **create_users_with_list_input** -> create_users_with_list_input(user) +> create_users_with_list_input(body) Creates list of users with given input array ### Required Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **user** | [**Vec<::models::User>**](array.md)| List of user object | + **body** | [**Vec<::models::User>**](array.md)| List of user object | ### Return type @@ -192,7 +192,7 @@ No authorization required [[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) # **update_user** -> update_user(username, user) +> update_user(username, body) Updated user This can only be done by the logged in user. @@ -202,7 +202,7 @@ This can only be done by the logged in user. Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- **username** | **String**| name that need to be deleted | - **user** | [**User**](User.md)| Updated user object | + **body** | [**User**](User.md)| Updated user object | ### Return type diff --git a/samples/client/petstore/rust/src/apis/pet_api.rs b/samples/client/petstore/rust/src/apis/pet_api.rs index 0e8a803c5597..b6a9d9c72ac2 100644 --- a/samples/client/petstore/rust/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/src/apis/pet_api.rs @@ -31,22 +31,22 @@ impl PetApiClient { } pub trait PetApi { - fn add_pet(&self, pet: ::models::Pet) -> Box>>; + fn add_pet(&self, body: ::models::Pet) -> Box>>; fn delete_pet(&self, pet_id: i64, api_key: &str) -> Box>>; fn find_pets_by_status(&self, status: Vec) -> Box, Error = Error>>; fn find_pets_by_tags(&self, tags: Vec) -> Box, Error = Error>>; fn get_pet_by_id(&self, pet_id: i64) -> Box>>; - fn update_pet(&self, pet: ::models::Pet) -> Box>>; + fn update_pet(&self, body: ::models::Pet) -> Box>>; fn update_pet_with_form(&self, pet_id: i64, name: &str, status: &str) -> Box>>; - fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: ::models::File) -> Box>>; + fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: &std::path::Path) -> Box>>; } implPetApi for PetApiClient { - fn add_pet(&self, pet: ::models::Pet) -> Box>> { + fn add_pet(&self, body: ::models::Pet) -> Box>> { __internal_request::Request::new(hyper::Method::Post, "/pet".to_string()) .with_auth(__internal_request::Auth::Oauth) - .with_body_param(pet) + .with_body_param(body) .returns_nothing() .execute(self.configuration.borrow()) } @@ -85,10 +85,10 @@ implPetApi for PetApiClient { .execute(self.configuration.borrow()) } - fn update_pet(&self, pet: ::models::Pet) -> Box>> { + fn update_pet(&self, body: ::models::Pet) -> Box>> { __internal_request::Request::new(hyper::Method::Put, "/pet".to_string()) .with_auth(__internal_request::Auth::Oauth) - .with_body_param(pet) + .with_body_param(body) .returns_nothing() .execute(self.configuration.borrow()) } @@ -103,7 +103,7 @@ implPetApi for PetApiClient { .execute(self.configuration.borrow()) } - fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: ::models::File) -> Box>> { + fn upload_file(&self, pet_id: i64, additional_metadata: &str, file: &std::path::Path) -> Box>> { __internal_request::Request::new(hyper::Method::Post, "/pet/{petId}/uploadImage".to_string()) .with_auth(__internal_request::Auth::Oauth) .with_path_param("petId".to_string(), pet_id.to_string()) diff --git a/samples/client/petstore/rust/src/apis/store_api.rs b/samples/client/petstore/rust/src/apis/store_api.rs index dc05931697d6..a318fe6e8d55 100644 --- a/samples/client/petstore/rust/src/apis/store_api.rs +++ b/samples/client/petstore/rust/src/apis/store_api.rs @@ -34,7 +34,7 @@ pub trait StoreApi { fn delete_order(&self, order_id: &str) -> Box>>; fn get_inventory(&self, ) -> Box, Error = Error>>; fn get_order_by_id(&self, order_id: i64) -> Box>>; - fn place_order(&self, order: ::models::Order) -> Box>>; + fn place_order(&self, body: ::models::Order) -> Box>>; } @@ -62,9 +62,9 @@ implStoreApi for StoreApiClient { .execute(self.configuration.borrow()) } - fn place_order(&self, order: ::models::Order) -> Box>> { + fn place_order(&self, body: ::models::Order) -> Box>> { __internal_request::Request::new(hyper::Method::Post, "/store/order".to_string()) - .with_body_param(order) + .with_body_param(body) .execute(self.configuration.borrow()) } diff --git a/samples/client/petstore/rust/src/apis/user_api.rs b/samples/client/petstore/rust/src/apis/user_api.rs index f0976f80ee1f..82ee5749b219 100644 --- a/samples/client/petstore/rust/src/apis/user_api.rs +++ b/samples/client/petstore/rust/src/apis/user_api.rs @@ -31,35 +31,35 @@ impl UserApiClient { } pub trait UserApi { - fn create_user(&self, user: ::models::User) -> Box>>; - fn create_users_with_array_input(&self, user: Vec<::models::User>) -> Box>>; - fn create_users_with_list_input(&self, user: Vec<::models::User>) -> Box>>; + fn create_user(&self, body: ::models::User) -> Box>>; + fn create_users_with_array_input(&self, body: Vec<::models::User>) -> Box>>; + fn create_users_with_list_input(&self, body: Vec<::models::User>) -> Box>>; fn delete_user(&self, username: &str) -> Box>>; fn get_user_by_name(&self, username: &str) -> Box>>; fn login_user(&self, username: &str, password: &str) -> Box>>; fn logout_user(&self, ) -> Box>>; - fn update_user(&self, username: &str, user: ::models::User) -> Box>>; + fn update_user(&self, username: &str, body: ::models::User) -> Box>>; } implUserApi for UserApiClient { - fn create_user(&self, user: ::models::User) -> Box>> { + fn create_user(&self, body: ::models::User) -> Box>> { __internal_request::Request::new(hyper::Method::Post, "/user".to_string()) - .with_body_param(user) + .with_body_param(body) .returns_nothing() .execute(self.configuration.borrow()) } - fn create_users_with_array_input(&self, user: Vec<::models::User>) -> Box>> { + fn create_users_with_array_input(&self, body: Vec<::models::User>) -> Box>> { __internal_request::Request::new(hyper::Method::Post, "/user/createWithArray".to_string()) - .with_body_param(user) + .with_body_param(body) .returns_nothing() .execute(self.configuration.borrow()) } - fn create_users_with_list_input(&self, user: Vec<::models::User>) -> Box>> { + fn create_users_with_list_input(&self, body: Vec<::models::User>) -> Box>> { __internal_request::Request::new(hyper::Method::Post, "/user/createWithList".to_string()) - .with_body_param(user) + .with_body_param(body) .returns_nothing() .execute(self.configuration.borrow()) } @@ -90,10 +90,10 @@ implUserApi for UserApiClient { .execute(self.configuration.borrow()) } - fn update_user(&self, username: &str, user: ::models::User) -> Box>> { + fn update_user(&self, username: &str, body: ::models::User) -> Box>> { __internal_request::Request::new(hyper::Method::Put, "/user/{username}".to_string()) .with_path_param("username".to_string(), username.to_string()) - .with_body_param(user) + .with_body_param(body) .returns_nothing() .execute(self.configuration.borrow()) } From 1dda4ac51a86027adcf97f7a600081a6700e6a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Courtine?= Date: Sat, 19 Jan 2019 19:13:21 +0100 Subject: [PATCH 6/6] Cleanup: template compression to remove unecessary blank lines in generated code. --- .../main/resources/rust/reqwest/api.mustache | 10 +- .../petstore/rust-reqwest/src/apis/pet_api.rs | 204 +++++++----------- .../rust-reqwest/src/apis/store_api.rs | 24 --- .../rust-reqwest/src/apis/user_api.rs | 48 ----- 4 files changed, 80 insertions(+), 206 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache index 4f05642c2365..41cbe20f0416 100644 --- a/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache +++ b/modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache @@ -49,15 +49,12 @@ impl {{{classname}}} for {{{classname}}}Client { req_builder = req_builder.query(&[("{{{keyParamName}}}", val)]); } {{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}{{/hasAuthMethods}} - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - {{#hasHeaderParams}}{{#headerParams}} req_builder = req_builder.header("{{{baseName}}}", {{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}.to_string()); {{/headerParams}}{{/hasHeaderParams}} - {{#hasAuthMethods}}{{#authMethods}} {{#isApiKey}}{{#isKeyInHeader}} if let Some(ref apikey) = configuration.api_key { @@ -80,7 +77,6 @@ impl {{{classname}}} for {{{classname}}}Client { }; {{/isOAuth}} {{/authMethods}}{{/hasAuthMethods}} - {{#isMultipart}}{{#hasFormParams}} let form = reqwest::multipart::Form::new() {{#formParams}} @@ -93,20 +89,18 @@ impl {{{classname}}} for {{{classname}}}Client { {{/formParams}} req_builder = req_builder.multipart(form); {{/hasFormParams}}{{/isMultipart}} - {{^isMultipart}}{{#hasFormParams}} let mut form_params = std::collections::HashMap::new(); {{#formParams}} {{#isFile}} - form_params.insert("{{{baseName}}}".to_string(), unimplemented!("File form param not supported with x-www-form-urlencoded content")); + form_params.insert("{{{baseName}}}", unimplemented!("File form param not supported with x-www-form-urlencoded content")); {{/isFile}} {{^isFile}} - form_params.insert("{{{baseName}}}".to_string(), {{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}.to_string()); + form_params.insert("{{{baseName}}}", {{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}.to_string()); {{/isFile}} {{/formParams}} req_builder = req_builder.form(&form_params); {{/hasFormParams}}{{/isMultipart}} - {{#hasBodyParam}}{{#bodyParams}} req_builder = req_builder.json(&{{{paramName}}}); {{/bodyParams}}{{/hasBodyParam}} diff --git a/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs b/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs index bcfe9a3636e1..1f1e9095ce68 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/pet_api.rs @@ -1,10 +1,10 @@ -/* +/* * OpenAPI Petstore * * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. * * OpenAPI spec version: 1.0.0 - * + * * Generated by: https://openapi-generator.tech */ @@ -46,28 +46,22 @@ impl PetApi for PetApiClient { let uri_str = format!("{}/pet", configuration.base_path); let mut req_builder = client.post(uri_str.as_str()); - - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; - - - - - - - + + + + req_builder = req_builder.json(&body); - + // send request let req = req_builder.build()?; @@ -83,28 +77,22 @@ impl PetApi for PetApiClient { let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=pet_id); let mut req_builder = client.delete(uri_str.as_str()); - - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - + req_builder = req_builder.header("api_key", api_key.to_string()); - - - - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; - - - - - - - + + + + // send request let req = req_builder.build()?; @@ -121,26 +109,20 @@ impl PetApi for PetApiClient { let mut req_builder = client.get(uri_str.as_str()); req_builder = req_builder.query(&[("status", &status.into_iter().map(|p| p.to_string()).collect::>().join(",").to_string())]); - - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; - - - - - - - + + + + // send request let req = req_builder.build()?; @@ -156,26 +138,20 @@ impl PetApi for PetApiClient { let mut req_builder = client.get(uri_str.as_str()); req_builder = req_builder.query(&[("tags", &tags.into_iter().map(|p| p.to_string()).collect::>().join(",").to_string())]); - - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; - - - - - - - + + + + // send request let req = req_builder.build()?; @@ -190,16 +166,13 @@ impl PetApi for PetApiClient { let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=pet_id); let mut req_builder = client.get(uri_str.as_str()); - - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - + + + if let Some(ref apikey) = configuration.api_key { let key = apikey.key.clone(); let val = match apikey.prefix { @@ -208,14 +181,11 @@ impl PetApi for PetApiClient { }; req_builder = req_builder.header("api_key", val); }; - - - - - - - - + + + + + // send request let req = req_builder.build()?; @@ -230,28 +200,22 @@ impl PetApi for PetApiClient { let uri_str = format!("{}/pet", configuration.base_path); let mut req_builder = client.put(uri_str.as_str()); - - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; - - - - - - - + + + + req_builder = req_builder.json(&body); - + // send request let req = req_builder.build()?; @@ -267,31 +231,25 @@ impl PetApi for PetApiClient { let uri_str = format!("{}/pet/{petId}", configuration.base_path, petId=pet_id); let mut req_builder = client.post(uri_str.as_str()); - - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; - - - - - + + + let mut form_params = std::collections::HashMap::new(); - form_params.insert("name".to_string(), name.to_string()); - form_params.insert("status".to_string(), status.to_string()); + form_params.insert("name", name.to_string()); + form_params.insert("status", status.to_string()); req_builder = req_builder.form(&form_params); - - - + + // send request let req = req_builder.build()?; @@ -307,31 +265,25 @@ impl PetApi for PetApiClient { let uri_str = format!("{}/pet/{petId}/uploadImage", configuration.base_path, petId=pet_id); let mut req_builder = client.post(uri_str.as_str()); - - + if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - + + + if let Some(ref token) = configuration.oauth_access_token { req_builder = req_builder.bearer_auth(token.to_owned()); }; - - - - - - let mut form_params = reqwest::multipart::Form::new() - .text("additionalMetadata".to_string(), additional_metadata.to_string()) - .file("file".to_string(), file)?; - req_builder = req_builder.multipart(form_params); - - - + + + + let mut form_params = std::collections::HashMap::new(); + form_params.insert("additionalMetadata", additional_metadata.to_string()); + form_params.insert("file", unimplemented!("File form param not supported with x-www-form-urlencoded content")); + req_builder = req_builder.form(&form_params); + + // send request let req = req_builder.build()?; diff --git a/samples/client/petstore/rust-reqwest/src/apis/store_api.rs b/samples/client/petstore/rust-reqwest/src/apis/store_api.rs index 9ffb9de798e6..d6cec820256a 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/store_api.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/store_api.rs @@ -43,19 +43,13 @@ impl StoreApi for StoreApiClient { let mut req_builder = client.delete(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - // send request @@ -73,13 +67,10 @@ impl StoreApi for StoreApiClient { let mut req_builder = client.get(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - if let Some(ref apikey) = configuration.api_key { @@ -92,11 +83,8 @@ impl StoreApi for StoreApiClient { }; - - - // send request @@ -113,19 +101,13 @@ impl StoreApi for StoreApiClient { let mut req_builder = client.get(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - // send request @@ -142,19 +124,13 @@ impl StoreApi for StoreApiClient { let mut req_builder = client.post(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - req_builder = req_builder.json(&body); diff --git a/samples/client/petstore/rust-reqwest/src/apis/user_api.rs b/samples/client/petstore/rust-reqwest/src/apis/user_api.rs index 557274499957..43fa8c6f804f 100644 --- a/samples/client/petstore/rust-reqwest/src/apis/user_api.rs +++ b/samples/client/petstore/rust-reqwest/src/apis/user_api.rs @@ -47,19 +47,13 @@ impl UserApi for UserApiClient { let mut req_builder = client.post(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - req_builder = req_builder.json(&body); @@ -79,19 +73,13 @@ impl UserApi for UserApiClient { let mut req_builder = client.post(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - req_builder = req_builder.json(&body); @@ -111,19 +99,13 @@ impl UserApi for UserApiClient { let mut req_builder = client.post(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - req_builder = req_builder.json(&body); @@ -143,19 +125,13 @@ impl UserApi for UserApiClient { let mut req_builder = client.delete(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - // send request @@ -173,19 +149,13 @@ impl UserApi for UserApiClient { let mut req_builder = client.get(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - // send request @@ -204,19 +174,13 @@ impl UserApi for UserApiClient { req_builder = req_builder.query(&[("username", &username.to_string())]); req_builder = req_builder.query(&[("password", &password.to_string())]); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - // send request @@ -233,19 +197,13 @@ impl UserApi for UserApiClient { let mut req_builder = client.get(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - // send request @@ -263,19 +221,13 @@ impl UserApi for UserApiClient { let mut req_builder = client.put(uri_str.as_str()); - if let Some(ref user_agent) = configuration.user_agent { req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone()); } - - - - - req_builder = req_builder.json(&body);