Skip to content

Commit f616605

Browse files
committed
Regenerate Java Petstore sample
1 parent 3d4b5a1 commit f616605

File tree

8 files changed

+180
-22
lines changed

8 files changed

+180
-22
lines changed

samples/client/petstore/java/src/main/java/io/swagger/client/ApiInvoker.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.text.SimpleDateFormat;
3232
import java.text.ParseException;
3333

34+
import io.swagger.client.auth.Authentication;
35+
3436
public class ApiInvoker {
3537
private static ApiInvoker INSTANCE = new ApiInvoker();
3638
private Map<String, Client> hostMap = new HashMap<String, Client>();
@@ -162,11 +164,12 @@ public static String serialize(Object obj) throws ApiException {
162164
}
163165
}
164166

165-
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType) throws ApiException {
167+
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException {
168+
processAuthParams(authNames, queryParams, headerParams);
169+
166170
Client client = getClient(host);
167171

168172
StringBuilder b = new StringBuilder();
169-
170173
for(String key : queryParams.keySet()) {
171174
String value = queryParams.get(key);
172175
if (value != null){
@@ -267,6 +270,14 @@ else if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
267270
}
268271
}
269272

273+
private void processAuthParams(String[] authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
274+
for(String authName : authNames) {
275+
Authentication auth = Configuration.getAuthentication(authName);
276+
if(auth == null) throw new RuntimeException("Authentication has not been setup for " + authName);
277+
auth.processParams(queryParams, headerParams);
278+
}
279+
}
280+
270281
private Client getClient(String host) {
271282
if(!hostMap.containsKey(host)) {
272283
Client client = Client.create();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.swagger.client;
2+
3+
import java.util.Map;
4+
import java.util.HashMap;
5+
6+
import io.swagger.client.auth.Authentication;
7+
import io.swagger.client.auth.HttpBasicAuth;
8+
import io.swagger.client.auth.ApiKeyAuth;
9+
10+
public class Configuration {
11+
private static final Map<String, Authentication> AUTH;
12+
13+
static {
14+
AUTH = new HashMap<String, Authentication>();
15+
16+
17+
AUTH.put("api_key", new ApiKeyAuth("header", "api_key"));
18+
19+
20+
21+
22+
// TODO: support oauth
23+
24+
}
25+
26+
public static Authentication getAuthentication(String authName) {
27+
return AUTH.get(authName);
28+
}
29+
}

samples/client/petstore/java/src/main/java/io/swagger/client/api/PetApi.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public void updatePet (Pet body) throws ApiException {
7474
}
7575

7676
try {
77-
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType);
77+
String[] authNames = new String[] { "petstore_auth" };
78+
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType, authNames);
7879
if(response != null){
7980
return ;
8081
}
@@ -124,7 +125,8 @@ public void addPet (Pet body) throws ApiException {
124125
}
125126

126127
try {
127-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType);
128+
String[] authNames = new String[] { "petstore_auth" };
129+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
128130
if(response != null){
129131
return ;
130132
}
@@ -176,7 +178,8 @@ public List<Pet> findPetsByStatus (List<String> status) throws ApiException {
176178
}
177179

178180
try {
179-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
181+
String[] authNames = new String[] { "petstore_auth" };
182+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
180183
if(response != null){
181184
return (List<Pet>) ApiInvoker.deserialize(response, "array", Pet.class);
182185
}
@@ -228,7 +231,8 @@ public List<Pet> findPetsByTags (List<String> tags) throws ApiException {
228231
}
229232

230233
try {
231-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
234+
String[] authNames = new String[] { "petstore_auth" };
235+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
232236
if(response != null){
233237
return (List<Pet>) ApiInvoker.deserialize(response, "array", Pet.class);
234238
}
@@ -284,7 +288,8 @@ public Pet getPetById (Long petId) throws ApiException {
284288
}
285289

286290
try {
287-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
291+
String[] authNames = new String[] { "api_key", "petstore_auth" };
292+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
288293
if(response != null){
289294
return (Pet) ApiInvoker.deserialize(response, "", Pet.class);
290295
}
@@ -350,7 +355,8 @@ public void updatePetWithForm (String petId, String name, String status) throws
350355
}
351356

352357
try {
353-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType);
358+
String[] authNames = new String[] { "petstore_auth" };
359+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
354360
if(response != null){
355361
return ;
356362
}
@@ -408,7 +414,8 @@ public void deletePet (String apiKey, Long petId) throws ApiException {
408414
}
409415

410416
try {
411-
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType);
417+
String[] authNames = new String[] { "petstore_auth" };
418+
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType, authNames);
412419
if(response != null){
413420
return ;
414421
}
@@ -475,7 +482,8 @@ public void uploadFile (Long petId, String additionalMetadata, File file) throws
475482
}
476483

477484
try {
478-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType);
485+
String[] authNames = new String[] { "petstore_auth" };
486+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
479487
if(response != null){
480488
return ;
481489
}

samples/client/petstore/java/src/main/java/io/swagger/client/api/StoreApi.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public Map<String, Integer> getInventory () throws ApiException {
7373
}
7474

7575
try {
76-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
76+
String[] authNames = new String[] { "api_key" };
77+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
7778
if(response != null){
7879
return (Map<String, Integer>) ApiInvoker.deserialize(response, "map", Map.class);
7980
}
@@ -123,7 +124,8 @@ public Order placeOrder (Order body) throws ApiException {
123124
}
124125

125126
try {
126-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType);
127+
String[] authNames = new String[] { };
128+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
127129
if(response != null){
128130
return (Order) ApiInvoker.deserialize(response, "", Order.class);
129131
}
@@ -179,7 +181,8 @@ public Order getOrderById (String orderId) throws ApiException {
179181
}
180182

181183
try {
182-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
184+
String[] authNames = new String[] { };
185+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
183186
if(response != null){
184187
return (Order) ApiInvoker.deserialize(response, "", Order.class);
185188
}
@@ -235,7 +238,8 @@ public void deleteOrder (String orderId) throws ApiException {
235238
}
236239

237240
try {
238-
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType);
241+
String[] authNames = new String[] { };
242+
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType, authNames);
239243
if(response != null){
240244
return ;
241245
}

samples/client/petstore/java/src/main/java/io/swagger/client/api/UserApi.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public void createUser (User body) throws ApiException {
7474
}
7575

7676
try {
77-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType);
77+
String[] authNames = new String[] { };
78+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
7879
if(response != null){
7980
return ;
8081
}
@@ -124,7 +125,8 @@ public void createUsersWithArrayInput (List<User> body) throws ApiException {
124125
}
125126

126127
try {
127-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType);
128+
String[] authNames = new String[] { };
129+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
128130
if(response != null){
129131
return ;
130132
}
@@ -174,7 +176,8 @@ public void createUsersWithListInput (List<User> body) throws ApiException {
174176
}
175177

176178
try {
177-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType);
179+
String[] authNames = new String[] { };
180+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, formParams, contentType, authNames);
178181
if(response != null){
179182
return ;
180183
}
@@ -229,7 +232,8 @@ public String loginUser (String username, String password) throws ApiException {
229232
}
230233

231234
try {
232-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
235+
String[] authNames = new String[] { };
236+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
233237
if(response != null){
234238
return (String) ApiInvoker.deserialize(response, "", String.class);
235239
}
@@ -278,7 +282,8 @@ public void logoutUser () throws ApiException {
278282
}
279283

280284
try {
281-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
285+
String[] authNames = new String[] { };
286+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
282287
if(response != null){
283288
return ;
284289
}
@@ -334,7 +339,8 @@ public User getUserByName (String username) throws ApiException {
334339
}
335340

336341
try {
337-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType);
342+
String[] authNames = new String[] { };
343+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
338344
if(response != null){
339345
return (User) ApiInvoker.deserialize(response, "", User.class);
340346
}
@@ -391,7 +397,8 @@ public void updateUser (String username, User body) throws ApiException {
391397
}
392398

393399
try {
394-
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType);
400+
String[] authNames = new String[] { };
401+
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, formParams, contentType, authNames);
395402
if(response != null){
396403
return ;
397404
}
@@ -447,7 +454,8 @@ public void deleteUser (String username) throws ApiException {
447454
}
448455

449456
try {
450-
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType);
457+
String[] authNames = new String[] { };
458+
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, formParams, contentType, authNames);
451459
if(response != null){
452460
return ;
453461
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.swagger.client.auth;
2+
3+
import java.util.Map;
4+
5+
public class ApiKeyAuth implements Authentication {
6+
private final String location;
7+
private final String paramName;
8+
9+
private String apiKey;
10+
private String apiKeyPrefix;
11+
12+
public ApiKeyAuth(String location, String paramName) {
13+
this.location = location;
14+
this.paramName = paramName;
15+
}
16+
17+
public String getLocation() {
18+
return location;
19+
}
20+
21+
public String getParamName() {
22+
return paramName;
23+
}
24+
25+
public String getApiKey() {
26+
return apiKey;
27+
}
28+
29+
public void setApiKey(String apiKey) {
30+
this.apiKey = apiKey;
31+
}
32+
33+
public String getApiKeyPrefix() {
34+
return apiKeyPrefix;
35+
}
36+
37+
public void setApiKeyPrefix(String apiKeyPrefix) {
38+
this.apiKeyPrefix = apiKeyPrefix;
39+
}
40+
41+
@Override
42+
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
43+
String value;
44+
if (apiKeyPrefix != null) {
45+
value = apiKeyPrefix + " " + apiKey;
46+
} else {
47+
value = apiKey;
48+
}
49+
if (location == "query") {
50+
queryParams.put(paramName, value);
51+
} else if (location == "header") {
52+
headerParams.put(paramName, value);
53+
}
54+
}
55+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.swagger.client.auth;
2+
3+
import java.util.Map;
4+
5+
public interface Authentication {
6+
void processParams(Map<String, String> queryParams, Map<String, String> headerParams);
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.swagger.client.auth;
2+
3+
import java.util.Map;
4+
5+
import java.io.UnsupportedEncodingException;
6+
import javax.xml.bind.DatatypeConverter;
7+
8+
public class HttpBasicAuth implements Authentication {
9+
private String username;
10+
private String password;
11+
12+
public String getUsername() {
13+
return username;
14+
}
15+
16+
public void setUsername(String username) {
17+
this.username = username;
18+
}
19+
20+
public String getPassword() {
21+
return password;
22+
}
23+
24+
public void setPassword(String password) {
25+
this.password = password;
26+
}
27+
28+
@Override
29+
public void processParams(Map<String, String> queryParams, Map<String, String> headerParams) {
30+
try {
31+
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
32+
} catch (UnsupportedEncodingException e) {
33+
throw new RuntimeException(e);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)