Skip to content

Commit

Permalink
[Rust] Add user agent handling for rust template (master) (#7375)
Browse files Browse the repository at this point in the history
* Add user agent handling for rust template

* Run ./bin/rust-petstore.sh
  • Loading branch information
tbourvon authored and wing328 committed Jan 14, 2018
1 parent ea8b184 commit 66be7a7
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 1 deletion.
7 changes: 7 additions & 0 deletions modules/swagger-codegen/src/main/resources/rust/api.mustache
Original file line number Diff line number Diff line change
@@ -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<C: hyper::client::Connect> {
Expand Down Expand Up @@ -57,6 +60,10 @@ impl<C: hyper::client::Connect>{{classname}} for {{classname}}Client<C> {
// }
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use hyper;

pub struct Configuration<C: hyper::client::Connect> {
pub base_path: String,
pub user_agent: Option<String>,
pub client: hyper::client::Client<C>,
}

impl<C: hyper::client::Connect> Configuration<C> {
pub fn new(client: hyper::client::Client<C>) -> Configuration<C> {
Configuration {
base_path: "{{{basePath}}}".to_owned(),
user_agent: {{#httpUserAgent}}Some("{{{.}}}".to_owned()){{/httpUserAgent}}{{^httpUserAgent}}None{{/httpUserAgent}},
client: client,
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/rust/.swagger-codegen/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0-SNAPSHOT
2.3.1-SNAPSHOT
2 changes: 2 additions & 0 deletions samples/client/petstore/rust/src/apis/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use hyper;

pub struct Configuration<C: hyper::client::Connect> {
pub base_path: String,
pub user_agent: Option<String>,
pub client: hyper::client::Client<C>,
}

impl<C: hyper::client::Connect> Configuration<C> {
pub fn new(client: hyper::client::Client<C>) -> Configuration<C> {
Configuration {
base_path: "http://petstore.swagger.io/v2".to_owned(),
user_agent: None,
client: client,
}
}
Expand Down
35 changes: 35 additions & 0 deletions samples/client/petstore/rust/src/apis/pet_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C: hyper::client::Connect> {
Expand Down Expand Up @@ -57,6 +60,10 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
// }
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());
Expand Down Expand Up @@ -85,6 +92,10 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
// }
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);
Expand Down Expand Up @@ -116,6 +127,10 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
// }
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
Expand Down Expand Up @@ -146,6 +161,10 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
// }
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
Expand Down Expand Up @@ -173,6 +192,10 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
// }
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
Expand Down Expand Up @@ -200,6 +223,10 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
// }
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());
Expand Down Expand Up @@ -228,6 +255,10 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
// }
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
Expand All @@ -252,6 +283,10 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
// }
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
Expand Down
19 changes: 19 additions & 0 deletions samples/client/petstore/rust/src/apis/store_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C: hyper::client::Connect> {
Expand Down Expand Up @@ -53,6 +56,10 @@ impl<C: hyper::client::Connect>StoreApi for StoreApiClient<C> {
// }
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
Expand All @@ -77,6 +84,10 @@ impl<C: hyper::client::Connect>StoreApi for StoreApiClient<C> {
// }
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
Expand Down Expand Up @@ -104,6 +115,10 @@ impl<C: hyper::client::Connect>StoreApi for StoreApiClient<C> {
// }
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
Expand Down Expand Up @@ -131,6 +146,10 @@ impl<C: hyper::client::Connect>StoreApi for StoreApiClient<C> {
// }
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());
Expand Down
35 changes: 35 additions & 0 deletions samples/client/petstore/rust/src/apis/user_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C: hyper::client::Connect> {
Expand Down Expand Up @@ -57,6 +60,10 @@ impl<C: hyper::client::Connect>UserApi for UserApiClient<C> {
// }
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());
Expand Down Expand Up @@ -85,6 +92,10 @@ impl<C: hyper::client::Connect>UserApi for UserApiClient<C> {
// }
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());
Expand Down Expand Up @@ -113,6 +124,10 @@ impl<C: hyper::client::Connect>UserApi for UserApiClient<C> {
// }
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());
Expand Down Expand Up @@ -141,6 +156,10 @@ impl<C: hyper::client::Connect>UserApi for UserApiClient<C> {
// }
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
Expand All @@ -165,6 +184,10 @@ impl<C: hyper::client::Connect>UserApi for UserApiClient<C> {
// }
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
Expand Down Expand Up @@ -196,6 +219,10 @@ impl<C: hyper::client::Connect>UserApi for UserApiClient<C> {
// }
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
Expand Down Expand Up @@ -223,6 +250,10 @@ impl<C: hyper::client::Connect>UserApi for UserApiClient<C> {
// }
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
Expand All @@ -247,6 +278,10 @@ impl<C: hyper::client::Connect>UserApi for UserApiClient<C> {
// }
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());
Expand Down

0 comments on commit 66be7a7

Please # to comment.