From 66be7a7910e9c2b684d234f36b954b40e68f2a5c Mon Sep 17 00:00:00 2001 From: Tristan Bourvon Date: Sun, 14 Jan 2018 11:18:58 +0100 Subject: [PATCH] [Rust] Add user agent handling for rust template (master) (#7375) * Add user agent handling for rust template * Run ./bin/rust-petstore.sh --- .../src/main/resources/rust/api.mustache | 7 ++++ .../resources/rust/configuration.mustache | 2 ++ .../petstore/rust/.swagger-codegen/VERSION | 2 +- .../petstore/rust/src/apis/configuration.rs | 2 ++ .../client/petstore/rust/src/apis/pet_api.rs | 35 +++++++++++++++++++ .../petstore/rust/src/apis/store_api.rs | 19 ++++++++++ .../client/petstore/rust/src/apis/user_api.rs | 35 +++++++++++++++++++ 7 files changed, 101 insertions(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/resources/rust/api.mustache b/modules/swagger-codegen/src/main/resources/rust/api.mustache index 811f88704481..518908b2aa68 100644 --- a/modules/swagger-codegen/src/main/resources/rust/api.mustache +++ b/modules/swagger-codegen/src/main/resources/rust/api.mustache @@ -1,12 +1,15 @@ {{>partial_header}} use std::rc::Rc; use std::borrow::Borrow; +use std::borrow::Cow; use hyper; use serde_json; use futures; use futures::{Future, Stream}; +use hyper::header::UserAgent; + use super::{Error, configuration}; pub struct {{{classname}}}Client { @@ -57,6 +60,10 @@ impl{{classname}} for {{classname}}Client { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + {{#hasHeaderParams}} { let mut headers = req.headers_mut(); diff --git a/modules/swagger-codegen/src/main/resources/rust/configuration.mustache b/modules/swagger-codegen/src/main/resources/rust/configuration.mustache index 59e82af56b19..52b402c5f7d2 100644 --- a/modules/swagger-codegen/src/main/resources/rust/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/rust/configuration.mustache @@ -3,6 +3,7 @@ use hyper; pub struct Configuration { pub base_path: String, + pub user_agent: Option, pub client: hyper::client::Client, } @@ -10,6 +11,7 @@ impl Configuration { pub fn new(client: hyper::client::Client) -> Configuration { Configuration { base_path: "{{{basePath}}}".to_owned(), + user_agent: {{#httpUserAgent}}Some("{{{.}}}".to_owned()){{/httpUserAgent}}{{^httpUserAgent}}None{{/httpUserAgent}}, client: client, } } diff --git a/samples/client/petstore/rust/.swagger-codegen/VERSION b/samples/client/petstore/rust/.swagger-codegen/VERSION index f9f7450d1359..50794f17f1a5 100644 --- a/samples/client/petstore/rust/.swagger-codegen/VERSION +++ b/samples/client/petstore/rust/.swagger-codegen/VERSION @@ -1 +1 @@ -2.3.0-SNAPSHOT \ No newline at end of file +2.3.1-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/rust/src/apis/configuration.rs b/samples/client/petstore/rust/src/apis/configuration.rs index 9ea9213594ea..9446c8fbdd30 100644 --- a/samples/client/petstore/rust/src/apis/configuration.rs +++ b/samples/client/petstore/rust/src/apis/configuration.rs @@ -12,6 +12,7 @@ use hyper; pub struct Configuration { pub base_path: String, + pub user_agent: Option, pub client: hyper::client::Client, } @@ -19,6 +20,7 @@ impl Configuration { pub fn new(client: hyper::client::Client) -> Configuration { Configuration { base_path: "http://petstore.swagger.io/v2".to_owned(), + user_agent: None, client: client, } } diff --git a/samples/client/petstore/rust/src/apis/pet_api.rs b/samples/client/petstore/rust/src/apis/pet_api.rs index 246f7b0ed13a..081ff02a618c 100644 --- a/samples/client/petstore/rust/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/src/apis/pet_api.rs @@ -10,12 +10,15 @@ use std::rc::Rc; use std::borrow::Borrow; +use std::borrow::Cow; use hyper; use serde_json; use futures; use futures::{Future, Stream}; +use hyper::header::UserAgent; + use super::{Error, configuration}; pub struct PetApiClient { @@ -57,6 +60,10 @@ implPetApi for PetApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + let serialized = serde_json::to_string(&body).unwrap(); req.headers_mut().set(hyper::header::ContentType::json()); @@ -85,6 +92,10 @@ implPetApi for PetApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + { let mut headers = req.headers_mut(); headers.set_raw("api_key", api_key); @@ -116,6 +127,10 @@ implPetApi for PetApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -146,6 +161,10 @@ implPetApi for PetApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -173,6 +192,10 @@ implPetApi for PetApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -200,6 +223,10 @@ implPetApi for PetApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + let serialized = serde_json::to_string(&body).unwrap(); req.headers_mut().set(hyper::header::ContentType::json()); @@ -228,6 +255,10 @@ implPetApi for PetApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -252,6 +283,10 @@ implPetApi for PetApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request diff --git a/samples/client/petstore/rust/src/apis/store_api.rs b/samples/client/petstore/rust/src/apis/store_api.rs index fbae7a25883c..5f493f2961a7 100644 --- a/samples/client/petstore/rust/src/apis/store_api.rs +++ b/samples/client/petstore/rust/src/apis/store_api.rs @@ -10,12 +10,15 @@ use std::rc::Rc; use std::borrow::Borrow; +use std::borrow::Cow; use hyper; use serde_json; use futures; use futures::{Future, Stream}; +use hyper::header::UserAgent; + use super::{Error, configuration}; pub struct StoreApiClient { @@ -53,6 +56,10 @@ implStoreApi for StoreApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -77,6 +84,10 @@ implStoreApi for StoreApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -104,6 +115,10 @@ implStoreApi for StoreApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -131,6 +146,10 @@ implStoreApi for StoreApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + let serialized = serde_json::to_string(&body).unwrap(); req.headers_mut().set(hyper::header::ContentType::json()); diff --git a/samples/client/petstore/rust/src/apis/user_api.rs b/samples/client/petstore/rust/src/apis/user_api.rs index 6a96c86d5c2e..71a35e49b268 100644 --- a/samples/client/petstore/rust/src/apis/user_api.rs +++ b/samples/client/petstore/rust/src/apis/user_api.rs @@ -10,12 +10,15 @@ use std::rc::Rc; use std::borrow::Borrow; +use std::borrow::Cow; use hyper; use serde_json; use futures; use futures::{Future, Stream}; +use hyper::header::UserAgent; + use super::{Error, configuration}; pub struct UserApiClient { @@ -57,6 +60,10 @@ implUserApi for UserApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + let serialized = serde_json::to_string(&body).unwrap(); req.headers_mut().set(hyper::header::ContentType::json()); @@ -85,6 +92,10 @@ implUserApi for UserApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + let serialized = serde_json::to_string(&body).unwrap(); req.headers_mut().set(hyper::header::ContentType::json()); @@ -113,6 +124,10 @@ implUserApi for UserApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + let serialized = serde_json::to_string(&body).unwrap(); req.headers_mut().set(hyper::header::ContentType::json()); @@ -141,6 +156,10 @@ implUserApi for UserApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -165,6 +184,10 @@ implUserApi for UserApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -196,6 +219,10 @@ implUserApi for UserApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -223,6 +250,10 @@ implUserApi for UserApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + // send request @@ -247,6 +278,10 @@ implUserApi for UserApiClient { // } let mut req = hyper::Request::new(method, uri.unwrap()); + if let Some(ref user_agent) = configuration.user_agent { + req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone()))); + } + let serialized = serde_json::to_string(&body).unwrap(); req.headers_mut().set(hyper::header::ContentType::json());