From 3d25e48492f0f404bbb95e2b7fe44a93ae55d1af Mon Sep 17 00:00:00 2001 From: Euan Kemp Date: Mon, 26 Mar 2018 21:30:31 -0700 Subject: [PATCH] [Rust] Improve auth formatting --- .../src/main/resources/rust/api.mustache | 67 +++---- .../resources/rust/configuration.mustache | 1 - .../client/petstore/rust/src/apis/pet_api.rs | 168 +++++++----------- .../petstore/rust/src/apis/store_api.rs | 33 ++-- .../client/petstore/rust/src/apis/user_api.rs | 32 ++-- 5 files changed, 123 insertions(+), 178 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/rust/api.mustache b/modules/swagger-codegen/src/main/resources/rust/api.mustache index 34149650291b..dfb191423515 100644 --- a/modules/swagger-codegen/src/main/resources/rust/api.mustache +++ b/modules/swagger-codegen/src/main/resources/rust/api.mustache @@ -45,52 +45,39 @@ impl{{classname}} for {{classname}}Client { let mut auth_query = HashMap::::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}} @@ -107,9 +94,9 @@ impl{{classname}} for {{classname}}Client { 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 { diff --git a/modules/swagger-codegen/src/main/resources/rust/configuration.mustache b/modules/swagger-codegen/src/main/resources/rust/configuration.mustache index 7319f4a3d414..ff2d673b27c5 100644 --- a/modules/swagger-codegen/src/main/resources/rust/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/rust/configuration.mustache @@ -6,7 +6,6 @@ pub struct Configuration { pub base_path: String, pub user_agent: Option, pub client: hyper::client::Client, - pub basic_auth: Option, pub oauth_access_token: Option, pub api_key: Option, diff --git a/samples/client/petstore/rust/src/apis/pet_api.rs b/samples/client/petstore/rust/src/apis/pet_api.rs index d4fc7edee6b1..f7b0cc605777 100644 --- a/samples/client/petstore/rust/src/apis/pet_api.rs +++ b/samples/client/petstore/rust/src/apis/pet_api.rs @@ -52,18 +52,13 @@ implPetApi for PetApiClient { let mut auth_headers = HashMap::::new(); let mut auth_query = HashMap::::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; @@ -72,9 +67,9 @@ implPetApi for PetApiClient { 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 { @@ -124,18 +119,13 @@ implPetApi for PetApiClient { let mut auth_headers = HashMap::::new(); let mut auth_query = HashMap::::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; @@ -144,9 +134,9 @@ implPetApi for PetApiClient { 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 { @@ -196,18 +186,13 @@ implPetApi for PetApiClient { let mut auth_headers = HashMap::::new(); let mut auth_query = HashMap::::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; @@ -217,9 +202,9 @@ implPetApi for PetApiClient { 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 { @@ -268,18 +253,13 @@ implPetApi for PetApiClient { let mut auth_headers = HashMap::::new(); let mut auth_query = HashMap::::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; @@ -289,9 +269,9 @@ implPetApi for PetApiClient { 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 { @@ -340,16 +320,13 @@ implPetApi for PetApiClient { let mut auth_headers = HashMap::::new(); let mut auth_query = HashMap::::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; @@ -358,9 +335,9 @@ implPetApi for PetApiClient { 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 { @@ -409,18 +386,13 @@ implPetApi for PetApiClient { let mut auth_headers = HashMap::::new(); let mut auth_query = HashMap::::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; @@ -429,9 +401,9 @@ implPetApi for PetApiClient { 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 { @@ -481,18 +453,13 @@ implPetApi for PetApiClient { let mut auth_headers = HashMap::::new(); let mut auth_query = HashMap::::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; @@ -501,9 +468,9 @@ implPetApi for PetApiClient { 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 { @@ -549,18 +516,13 @@ implPetApi for PetApiClient { let mut auth_headers = HashMap::::new(); let mut auth_query = HashMap::::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; @@ -569,9 +531,9 @@ implPetApi for PetApiClient { 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 { diff --git a/samples/client/petstore/rust/src/apis/store_api.rs b/samples/client/petstore/rust/src/apis/store_api.rs index 74135f4deb52..a4726b7b1392 100644 --- a/samples/client/petstore/rust/src/apis/store_api.rs +++ b/samples/client/petstore/rust/src/apis/store_api.rs @@ -50,9 +50,9 @@ implStoreApi for StoreApiClient { let query_string = { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/store/order/{orderId}{}", configuration.base_path, query_string, orderId=order_id); + let uri_str = format!("{}/store/order/{orderId}?{}", configuration.base_path, query_string, orderId=order_id); // TODO(farcaller): handle error // if let Err(e) = uri { @@ -95,16 +95,13 @@ implStoreApi for StoreApiClient { let mut auth_headers = HashMap::::new(); let mut auth_query = HashMap::::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; @@ -113,9 +110,9 @@ implStoreApi for StoreApiClient { for (key, val) in &auth_query { query.append_pair(key, val); } - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/store/inventory{}", configuration.base_path, query_string); + let uri_str = format!("{}/store/inventory?{}", configuration.base_path, query_string); // TODO(farcaller): handle error // if let Err(e) = uri { @@ -166,9 +163,9 @@ implStoreApi for StoreApiClient { let query_string = { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/store/order/{orderId}{}", configuration.base_path, query_string, orderId=order_id); + let uri_str = format!("{}/store/order/{orderId}?{}", configuration.base_path, query_string, orderId=order_id); // TODO(farcaller): handle error // if let Err(e) = uri { @@ -216,9 +213,9 @@ implStoreApi for StoreApiClient { let query_string = { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/store/order{}", configuration.base_path, query_string); + let uri_str = format!("{}/store/order?{}", configuration.base_path, query_string); // TODO(farcaller): handle error // if let Err(e) = uri { diff --git a/samples/client/petstore/rust/src/apis/user_api.rs b/samples/client/petstore/rust/src/apis/user_api.rs index 9b49f4e077ec..947932c5a702 100644 --- a/samples/client/petstore/rust/src/apis/user_api.rs +++ b/samples/client/petstore/rust/src/apis/user_api.rs @@ -54,9 +54,9 @@ implUserApi for UserApiClient { let query_string = { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/user{}", configuration.base_path, query_string); + let uri_str = format!("{}/user?{}", configuration.base_path, query_string); // TODO(farcaller): handle error // if let Err(e) = uri { @@ -105,9 +105,9 @@ implUserApi for UserApiClient { let query_string = { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/user/createWithArray{}", configuration.base_path, query_string); + let uri_str = format!("{}/user/createWithArray?{}", configuration.base_path, query_string); // TODO(farcaller): handle error // if let Err(e) = uri { @@ -156,9 +156,9 @@ implUserApi for UserApiClient { let query_string = { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/user/createWithList{}", configuration.base_path, query_string); + let uri_str = format!("{}/user/createWithList?{}", configuration.base_path, query_string); // TODO(farcaller): handle error // if let Err(e) = uri { @@ -207,9 +207,9 @@ implUserApi for UserApiClient { let query_string = { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/user/{username}{}", configuration.base_path, query_string, username=username); + let uri_str = format!("{}/user/{username}?{}", configuration.base_path, query_string, username=username); // TODO(farcaller): handle error // if let Err(e) = uri { @@ -254,9 +254,9 @@ implUserApi for UserApiClient { let query_string = { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/user/{username}{}", configuration.base_path, query_string, username=username); + let uri_str = format!("{}/user/{username}?{}", configuration.base_path, query_string, username=username); // TODO(farcaller): handle error // if let Err(e) = uri { @@ -306,9 +306,9 @@ implUserApi for UserApiClient { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); query.append_pair("username", &username.to_string()); query.append_pair("password", &password.to_string()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/user/login{}", configuration.base_path, query_string); + let uri_str = format!("{}/user/login?{}", configuration.base_path, query_string); // TODO(farcaller): handle error // if let Err(e) = uri { @@ -356,9 +356,9 @@ implUserApi for UserApiClient { let query_string = { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/user/logout{}", configuration.base_path, query_string); + let uri_str = format!("{}/user/logout?{}", configuration.base_path, query_string); // TODO(farcaller): handle error // if let Err(e) = uri { @@ -403,9 +403,9 @@ implUserApi for UserApiClient { let query_string = { let mut query = ::url::form_urlencoded::Serializer::new(String::new()); - format!("?{}", query.finish()) + query.finish() }; - let uri_str = format!("{}/user/{username}{}", configuration.base_path, query_string, username=username); + let uri_str = format!("{}/user/{username}?{}", configuration.base_path, query_string, username=username); // TODO(farcaller): handle error // if let Err(e) = uri {