2
2
use std::rc::Rc;
3
3
use std::borrow::Borrow;
4
4
use std::borrow::Cow;
5
+ use std::collections::HashMap;
5
6
6
7
use hyper;
7
8
use serde_json;
@@ -39,26 +40,71 @@ impl<C: hyper::client::Connect>{{classname}} for {{classname}}Client<C> {
39
40
fn { {{operationId} }}(&self, { {#allParams} }{ {paramName} }: { {#isString} }&str{ {/isString} }{ {#isUuid} }&str{ {/isUuid} }{ {^isString} }{ {^isUuid} }{ {^isPrimitiveType} }{ {^isContainer} }::models::{ {/isContainer} }{ {/isPrimitiveType} }{ {{dataType} }}{ {/isUuid} }{ {/isString} }{ {#hasMore} }, { {/hasMore} }{ {/allParams} }) -> Box<Future <Item = { { ^returnType} } (){{ /returnType}}{{#returnType}}{{{.}}}{{ /returnType}}, Error = Error <serde_json::Value >>> {
40
41
let configuration: &configuration::Configuration< C> = self.configuration.borrow();
41
42
43
+ {{#hasAuthMethods} }
44
+ let mut auth_headers = HashMap::<String , String >::new();
45
+ let mut auth_query = HashMap::<String , String >::new();
46
+ { {#authMethods} }
47
+ { {#isApiKey} }
48
+ if let Some(ref apikey) = configuration.api_key {
49
+ let key = apikey.key.clone();
50
+ let val = match apikey.prefix {
51
+ Some(ref prefix) => format! (" {} {}" , prefix, key),
52
+ None => key,
53
+ } ;
54
+ { {#isKeyInHeader} }
55
+ auth_headers.insert("{ {keyParamName} }".to_owned(), val);
56
+ { {/isKeyInHeader} }
57
+ { {#isKeyInQuery} }
58
+ auth_query.insert("{ {keyParamName} }".to_owned(), val);
59
+ { {/isKeyInQuery} }
60
+ };
61
+ { {/isApiKey} }
62
+ { {#isBasic} }
63
+ if let Some(ref auth_conf) = configuration.basic_auth {
64
+ let auth = hyper::header::Authorization(
65
+ hyper::header::Basic {
66
+ username: auth_conf.0.to_owned(),
67
+ password: auth_conf.1.to_owned(),
68
+ }
69
+ );
70
+ auth_headers.insert("Authorization".to_owned(), auth.to_string());
71
+ };
72
+ { {/isBasic} }
73
+ { {#isOAuth} }
74
+ if let Some(ref token) = configuration.oauth_access_token {
75
+ let auth = hyper::header::Authorization(
76
+ hyper::header::Bearer {
77
+ token: token.to_owned(),
78
+ }
79
+ );
80
+ auth_headers.insert("Authorization".to_owned(), auth.to_string());
81
+ };
82
+ { {/isOAuth} }
83
+ { {/authMethods} }
84
+ { {/hasAuthMethods} }
42
85
let method = hyper::Method::{ {httpMethod} };
43
86
44
- { {^hasQueryParams} }
45
- let uri_str = format!("{ } { {{path} }}", configuration.base_path{ {#pathParams} }, { {baseName} }={ {paramName} }{ {#isListContainer} }.join(",").as_ref(){ {/isListContainer} }{ {/pathParams} });
46
- { {/hasQueryParams} }
47
- { {#hasQueryParams} }
48
- let query = ::url::form_urlencoded::Serializer::new(String::new())
49
- { {#queryParams} }
50
- .append_pair("{ {baseName} }", & { {paramName} }{ {#isListContainer} }.join(","){ {/isListContainer} }.to_string())
51
- { {/queryParams} }
52
- .finish();
53
- let uri_str = format!("{ } { {{path} }}{ } ", configuration.base_path, query{ {#pathParams} }, { {baseName} }={ {paramName} }{ {#isListContainer} }.join(",").as_ref(){ {/isListContainer} }{ {/pathParams} });
54
- { {/hasQueryParams} }
55
-
56
- let uri = uri_str.parse();
87
+ let query_string = {
88
+ let mut query = ::url::form_urlencoded::Serializer::new(String::new());
89
+ {{#queryParams} }
90
+ query.append_pair("{ {baseName} }", & { {paramName} }{ {#isListContainer} }.join(","){ {/isListContainer} }.to_string());
91
+ { {/queryParams} }
92
+ { {#hasAuthMethods} }
93
+ for (key, val) in &auth_query {
94
+ query.append_pair(key, val);
95
+ }
96
+ { {/hasAuthMethods} }
97
+ query.finish()
98
+ };
99
+ let uri_str = format!("{ } { {{path} }}?{ } ", configuration.base_path, query_string{ {#pathParams} }, { {baseName} }={ {paramName} }{ {#isListContainer} }.join(",").as_ref(){ {/isListContainer} }{ {/pathParams} });
100
+
57
101
// TODO(farcaller): handle error
58
102
// if let Err(e) = uri {
59
103
// return Box::new(futures::future::err(e));
60
104
// }
61
- let mut req = hyper::Request::new(method, uri.unwrap());
105
+ let mut uri: hyper::Uri = uri_str.parse().unwrap();
106
+
107
+ let mut req = hyper::Request::new(method, uri);
62
108
63
109
if let Some(ref user_agent) = configuration.user_agent {
64
110
req.headers_mut().set(UserAgent::new(Cow::Owned(user_agent.clone())));
@@ -73,6 +119,12 @@ impl<C: hyper::client::Connect>{{classname}} for {{classname}}Client<C> {
73
119
}
74
120
{ {/hasHeaderParams} }
75
121
122
+ { {#hasAuthMethods} }
123
+ for (key, val) in auth_headers {
124
+ req.headers_mut().set_raw(key, val);
125
+ }
126
+ { {/hasAuthMethods} }
127
+
76
128
{ {#hasBodyParam} }
77
129
{ {#bodyParams} }
78
130
let serialized = serde_json::to_string(& { {paramName} }).unwrap();
0 commit comments