Skip to content

Commit

Permalink
[Rust] Improve auth formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
euank committed Mar 27, 2018
1 parent 80c592c commit 3d25e48
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 178 deletions.
67 changes: 27 additions & 40 deletions modules/swagger-codegen/src/main/resources/rust/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -45,52 +45,39 @@ impl<C: hyper::client::Connect>{{classname}} for {{classname}}Client<C> {
let mut auth_query = HashMap::<String, String>::new();
{{#authMethods}}
{{#isApiKey}}
match configuration.api_key {
None => (),
Some(ref apikey) => {
let key = apikey.key.clone();
let val = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
{{#isKeyInHeader}}
auth_headers.insert("{{keyParamName}}".to_owned(), val);
{{/isKeyInHeader}}
{{#isKeyInQuery}}
auth_query.insert("{{keyParamName}}".to_owned(), val);
{{/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,
};
{{#isKeyInHeader}}
auth_headers.insert("{{keyParamName}}".to_owned(), val);
{{/isKeyInHeader}}
{{#isKeyInQuery}}
auth_query.insert("{{keyParamName}}".to_owned(), val);
{{/isKeyInQuery}}
};
{{/isApiKey}}
{{#isBasic}}
{
match configuration.basic_auth {
Some(ref auth_conf) => {
let auth = hyper::header::Authorization(
hyper::header::Basic {
username: auth_conf.0.to_owned(),
password: auth_conf.1.to_owned(),
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
if let Some(ref auth_conf) = configuration.basic_auth {
let auth = hyper::header::Authorization(
hyper::header::Basic {
username: auth_conf.0.to_owned(),
password: auth_conf.1.to_owned(),
}
None => {}
}
);
auth_headers.insert("Authorization".to_owned(), auth.to_string());
};
{{/isBasic}}
{{#isOAuth}}
{
match configuration.oauth_access_token {
Some(ref token) => {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
if let Some(ref token) = configuration.oauth_access_token {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
None => {}
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
};
{{/isOAuth}}
{{/authMethods}}
Expand All @@ -107,9 +94,9 @@ impl<C: hyper::client::Connect>{{classname}} for {{classname}}Client<C> {
query.append_pair(key, val);
}
{{/hasAuthMethods}}
format!("?{}", query.finish())
query.finish()
};
let uri_str = format!("{}{{{path}}}{}", configuration.base_path, query_string{{#pathParams}}, {{baseName}}={{paramName}}{{#isListContainer}}.join(",").as_ref(){{/isListContainer}}{{/pathParams}});
let uri_str = format!("{}{{{path}}}?{}", configuration.base_path, query_string{{#pathParams}}, {{baseName}}={{paramName}}{{#isListContainer}}.join(",").as_ref(){{/isListContainer}}{{/pathParams}});

// TODO(farcaller): handle error
// if let Err(e) = uri {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub struct Configuration<C: hyper::client::Connect> {
pub base_path: String,
pub user_agent: Option<String>,
pub client: hyper::client::Client<C>,
pub basic_auth: Option<BasicAuth>,
pub oauth_access_token: Option<String>,
pub api_key: Option<ApiKey>,
Expand Down
168 changes: 65 additions & 103 deletions samples/client/petstore/rust/src/apis/pet_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,13 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {

let mut auth_headers = HashMap::<String, String>::new();
let mut auth_query = HashMap::<String, String>::new();
{
match configuration.oauth_access_token {
Some(ref token) => {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
if let Some(ref token) = configuration.oauth_access_token {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
None => {}
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
};
let method = hyper::Method::Post;

Expand All @@ -72,9 +67,9 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
for (key, val) in &auth_query {
query.append_pair(key, val);
}
format!("?{}", query.finish())
query.finish()
};
let uri_str = format!("{}/pet{}", configuration.base_path, query_string);
let uri_str = format!("{}/pet?{}", configuration.base_path, query_string);

// TODO(farcaller): handle error
// if let Err(e) = uri {
Expand Down Expand Up @@ -124,18 +119,13 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {

let mut auth_headers = HashMap::<String, String>::new();
let mut auth_query = HashMap::<String, String>::new();
{
match configuration.oauth_access_token {
Some(ref token) => {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
if let Some(ref token) = configuration.oauth_access_token {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
None => {}
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
};
let method = hyper::Method::Delete;

Expand All @@ -144,9 +134,9 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
for (key, val) in &auth_query {
query.append_pair(key, val);
}
format!("?{}", query.finish())
query.finish()
};
let uri_str = format!("{}/pet/{petId}{}", configuration.base_path, query_string, petId=pet_id);
let uri_str = format!("{}/pet/{petId}?{}", configuration.base_path, query_string, petId=pet_id);

// TODO(farcaller): handle error
// if let Err(e) = uri {
Expand Down Expand Up @@ -196,18 +186,13 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {

let mut auth_headers = HashMap::<String, String>::new();
let mut auth_query = HashMap::<String, String>::new();
{
match configuration.oauth_access_token {
Some(ref token) => {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
if let Some(ref token) = configuration.oauth_access_token {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
None => {}
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
};
let method = hyper::Method::Get;

Expand All @@ -217,9 +202,9 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
for (key, val) in &auth_query {
query.append_pair(key, val);
}
format!("?{}", query.finish())
query.finish()
};
let uri_str = format!("{}/pet/findByStatus{}", configuration.base_path, query_string);
let uri_str = format!("{}/pet/findByStatus?{}", configuration.base_path, query_string);

// TODO(farcaller): handle error
// if let Err(e) = uri {
Expand Down Expand Up @@ -268,18 +253,13 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {

let mut auth_headers = HashMap::<String, String>::new();
let mut auth_query = HashMap::<String, String>::new();
{
match configuration.oauth_access_token {
Some(ref token) => {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
if let Some(ref token) = configuration.oauth_access_token {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
None => {}
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
};
let method = hyper::Method::Get;

Expand All @@ -289,9 +269,9 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
for (key, val) in &auth_query {
query.append_pair(key, val);
}
format!("?{}", query.finish())
query.finish()
};
let uri_str = format!("{}/pet/findByTags{}", configuration.base_path, query_string);
let uri_str = format!("{}/pet/findByTags?{}", configuration.base_path, query_string);

// TODO(farcaller): handle error
// if let Err(e) = uri {
Expand Down Expand Up @@ -340,16 +320,13 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {

let mut auth_headers = HashMap::<String, String>::new();
let mut auth_query = HashMap::<String, String>::new();
match configuration.api_key {
None => (),
Some(ref apikey) => {
let key = apikey.key.clone();
let val = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
auth_headers.insert("api_key".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,
};
auth_headers.insert("api_key".to_owned(), val);
};
let method = hyper::Method::Get;

Expand All @@ -358,9 +335,9 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
for (key, val) in &auth_query {
query.append_pair(key, val);
}
format!("?{}", query.finish())
query.finish()
};
let uri_str = format!("{}/pet/{petId}{}", configuration.base_path, query_string, petId=pet_id);
let uri_str = format!("{}/pet/{petId}?{}", configuration.base_path, query_string, petId=pet_id);

// TODO(farcaller): handle error
// if let Err(e) = uri {
Expand Down Expand Up @@ -409,18 +386,13 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {

let mut auth_headers = HashMap::<String, String>::new();
let mut auth_query = HashMap::<String, String>::new();
{
match configuration.oauth_access_token {
Some(ref token) => {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
if let Some(ref token) = configuration.oauth_access_token {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
None => {}
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
};
let method = hyper::Method::Put;

Expand All @@ -429,9 +401,9 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
for (key, val) in &auth_query {
query.append_pair(key, val);
}
format!("?{}", query.finish())
query.finish()
};
let uri_str = format!("{}/pet{}", configuration.base_path, query_string);
let uri_str = format!("{}/pet?{}", configuration.base_path, query_string);

// TODO(farcaller): handle error
// if let Err(e) = uri {
Expand Down Expand Up @@ -481,18 +453,13 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {

let mut auth_headers = HashMap::<String, String>::new();
let mut auth_query = HashMap::<String, String>::new();
{
match configuration.oauth_access_token {
Some(ref token) => {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
if let Some(ref token) = configuration.oauth_access_token {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
None => {}
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
};
let method = hyper::Method::Post;

Expand All @@ -501,9 +468,9 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
for (key, val) in &auth_query {
query.append_pair(key, val);
}
format!("?{}", query.finish())
query.finish()
};
let uri_str = format!("{}/pet/{petId}{}", configuration.base_path, query_string, petId=pet_id);
let uri_str = format!("{}/pet/{petId}?{}", configuration.base_path, query_string, petId=pet_id);

// TODO(farcaller): handle error
// if let Err(e) = uri {
Expand Down Expand Up @@ -549,18 +516,13 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {

let mut auth_headers = HashMap::<String, String>::new();
let mut auth_query = HashMap::<String, String>::new();
{
match configuration.oauth_access_token {
Some(ref token) => {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
if let Some(ref token) = configuration.oauth_access_token {
let auth = hyper::header::Authorization(
hyper::header::Bearer {
token: token.to_owned(),
}
None => {}
}
);
auth_headers.insert("Authorization".to_owned(), format!("{}", auth));
};
let method = hyper::Method::Post;

Expand All @@ -569,9 +531,9 @@ impl<C: hyper::client::Connect>PetApi for PetApiClient<C> {
for (key, val) in &auth_query {
query.append_pair(key, val);
}
format!("?{}", query.finish())
query.finish()
};
let uri_str = format!("{}/pet/{petId}/uploadImage{}", configuration.base_path, query_string, petId=pet_id);
let uri_str = format!("{}/pet/{petId}/uploadImage?{}", configuration.base_path, query_string, petId=pet_id);

// TODO(farcaller): handle error
// if let Err(e) = uri {
Expand Down
Loading

0 comments on commit 3d25e48

Please sign in to comment.