@@ -17,6 +17,7 @@ import javax.ws.rs.core.Response.Status.Family;
17
17
import javax.ws.rs.core.MediaType;
18
18
19
19
import java.util.Collection;
20
+ import java.util.Collections;
20
21
import java.util.Map;
21
22
import java.util.Map.Entry;
22
23
import java.util.HashMap;
@@ -33,12 +34,19 @@ import java.text.DateFormat;
33
34
import java.text.SimpleDateFormat;
34
35
import java.text.ParseException;
35
36
37
+ import { {invokerPackage} }.auth.Authentication;
38
+ import { {invokerPackage} }.auth.HttpBasicAuth;
39
+ import { {invokerPackage} }.auth.ApiKeyAuth;
40
+ import { {invokerPackage} }.auth.OAuth;
41
+
36
42
public class ApiClient {
37
43
private Map< String, Client> hostMap = new HashMap< String, Client> ();
38
44
private Map< String, String> defaultHeaderMap = new HashMap< String, String> ();
39
45
private boolean debugging = false ;
40
46
private String basePath = " {{basePath}}" ;
41
47
48
+ private Map< String, Authentication> authentications;
49
+
42
50
private DateFormat dateFormat;
43
51
44
52
public ApiClient() {
@@ -51,6 +59,14 @@ public class ApiClient {
51
59
52
60
// Set default User-Agent.
53
61
setUserAgent(" Java-Swagger" );
62
+
63
+ // Setup authentications (key: authentication name, value: authentication).
64
+ authentications = new HashMap< String, Authentication> ();{{#authMethods} }{ {#isBasic} }
65
+ authentications.put("{ {name} }", new HttpBasicAuth());{ {/isBasic} }{ {#isApiKey} }
66
+ authentications.put("{ {name} }", new ApiKeyAuth({ {#isKeyInHeader} }"header"{ {/isKeyInHeader} }{ {^isKeyInHeader} }"query"{ {/isKeyInHeader} }, "{ {keyParamName} }"));{ {/isApiKey} }{ {#isOAuth} }
67
+ authentications.put("{ {name} }", new OAuth());{ {/isOAuth} }{ {/authMethods} }
68
+ // Prevent the authentications from being modified.
69
+ authentications = Collections.unmodifiableMap(authentications);
54
70
}
55
71
56
72
public String getBasePath() {
@@ -62,6 +78,75 @@ public class ApiClient {
62
78
return this;
63
79
}
64
80
81
+ /**
82
+ * Get authentications (key: authentication name, value: authentication).
83
+ */
84
+ public Map<String , Authentication > getAuthentications() {
85
+ return authentications;
86
+ }
87
+
88
+ /**
89
+ * Get authentication for the given name.
90
+ *
91
+ * @param authName The authentication name
92
+ * @return The authentication, null if not found
93
+ */
94
+ public Authentication getAuthentication(String authName) {
95
+ return authentications.get(authName);
96
+ }
97
+
98
+ /**
99
+ * Helper method to set username for the first HTTP basic authentication.
100
+ */
101
+ public void setUsername(String username) {
102
+ for (Authentication auth : authentications.values()) {
103
+ if (auth instanceof HttpBasicAuth) {
104
+ ((HttpBasicAuth) auth).setUsername(username);
105
+ return;
106
+ }
107
+ }
108
+ throw new RuntimeException("No HTTP basic authentication configured!");
109
+ }
110
+
111
+ /**
112
+ * Helper method to set password for the first HTTP basic authentication.
113
+ */
114
+ public void setPassword(String password) {
115
+ for (Authentication auth : authentications.values()) {
116
+ if (auth instanceof HttpBasicAuth) {
117
+ ((HttpBasicAuth) auth).setPassword(password);
118
+ return;
119
+ }
120
+ }
121
+ throw new RuntimeException("No HTTP basic authentication configured!");
122
+ }
123
+
124
+ /**
125
+ * Helper method to set API key value for the first API key authentication.
126
+ */
127
+ public void setApiKey(String apiKey) {
128
+ for (Authentication auth : authentications.values()) {
129
+ if (auth instanceof ApiKeyAuth) {
130
+ ((ApiKeyAuth) auth).setApiKey(apiKey);
131
+ return;
132
+ }
133
+ }
134
+ throw new RuntimeException("No API key authentication configured!");
135
+ }
136
+
137
+ /**
138
+ * Helper method to set API key prefix for the first API key authentication.
139
+ */
140
+ public void setApiKeyPrefix(String apiKeyPrefix) {
141
+ for (Authentication auth : authentications.values()) {
142
+ if (auth instanceof ApiKeyAuth) {
143
+ ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
144
+ return;
145
+ }
146
+ }
147
+ throw new RuntimeException("No API key authentication configured!");
148
+ }
149
+
65
150
/**
66
151
* Set the User-Agent header's value (by adding to the default header map).
67
152
*/
@@ -222,13 +307,15 @@ public class ApiClient {
222
307
* @param headerParams The header parameters
223
308
* @param formParams The form parameters
224
309
* @param contentType The request Content-Type
310
+ * @param authNames The authentications to apply
225
311
* @return The response body in type of string
226
312
*/
227
- public String invokeAPI(String path, String method, Map<String , String > queryParams, Object body, Map<String , String > headerParams, Map<String , String > formParams, String contentType) throws ApiException {
313
+ public String invokeAPI(String path, String method, Map<String , String > queryParams, Object body, Map<String , String > headerParams, Map<String , String > formParams, String contentType, String[] authNames) throws ApiException {
314
+ updateParamsForAuth(authNames, queryParams, headerParams);
315
+
228
316
Client client = getClient();
229
317
230
318
StringBuilder b = new StringBuilder();
231
-
232
319
for(String key : queryParams.keySet()) {
233
320
String value = queryParams.get(key);
234
321
if (value != null){
@@ -329,6 +416,19 @@ public class ApiClient {
329
416
}
330
417
}
331
418
419
+ /**
420
+ * Update query and header parameters based on authentication settings.
421
+ *
422
+ * @param authNames The authentications to apply
423
+ */
424
+ private void updateParamsForAuth(String[] authNames, Map<String , String > queryParams, Map<String , String > headerParams) {
425
+ for (String authName : authNames) {
426
+ Authentication auth = authentications.get(authName);
427
+ if (auth == null) throw new RuntimeException(" Authentication undefined: " + authName);
428
+ auth.applyToParams(queryParams, headerParams);
429
+ }
430
+ }
431
+
332
432
/**
333
433
* Encode the given form parameters as request body.
334
434
*/
0 commit comments