Skip to content

Commit 46f78f2

Browse files
committed
Merge pull request #1215 from xhh/java-last-resp-info
[Java jersey2 okhttp-gson] Fix compilation error, record status code and response headers of last request
2 parents 1eed90f + 2e402bb commit 46f78f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+889
-291
lines changed

modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public class ApiClient {
5353
5454
private Map<String, Authentication> authentications;
5555
56+
private int statusCode;
57+
private Map<String, List<String>> responseHeaders;
58+
5659
private DateFormat dateFormat;
5760
5861
public ApiClient() {
@@ -84,6 +87,20 @@ public class ApiClient {
8487
return this;
8588
}
8689

90+
/**
91+
* Gets the status code of the previous request
92+
*/
93+
public int getStatusCode() {
94+
return statusCode;
95+
}
96+
97+
/**
98+
* Gets the response headers of the previous request
99+
*/
100+
public Map<String, List<String>> getResponseHeaders() {
101+
return responseHeaders;
102+
}
103+
87104
/**
88105
* Get authentications (key: authentication name, value: authentication).
89106
*/
@@ -484,6 +501,9 @@ public class ApiClient {
484501
throw new ApiException(500, "unknown method type " + method);
485502
}
486503

504+
statusCode = response.getStatusInfo().getStatusCode();
505+
responseHeaders = buildResponseHeaders(response);
506+
487507
if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
488508
return null;
489509
} else if (response.getStatusInfo().getFamily().equals(Status.Family.SUCCESSFUL)) {
@@ -502,23 +522,27 @@ public class ApiClient {
502522
// e.printStackTrace();
503523
}
504524
}
505-
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
506-
for (String key: response.getHeaders().keySet()) {
507-
List<Object> values = response.getHeaders().get(key);
508-
List<String> headers = new ArrayList<String>();
509-
for (Object o : values) {
510-
headers.add(String.valueOf(o));
511-
}
512-
responseHeaders.put(key, headers);
513-
}
514525
throw new ApiException(
515526
response.getStatus(),
516527
message,
517-
responseHeaders,
528+
buildResponseHeaders(response),
518529
respBody);
519530
}
520531
}
521532

533+
private Map<String, List<String>> buildResponseHeaders(Response response) {
534+
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
535+
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
536+
List<Object> values = entry.getValue();
537+
List<String> headers = new ArrayList<String>();
538+
for (Object o : values) {
539+
headers.add(String.valueOf(o));
540+
}
541+
responseHeaders.put(entry.getKey(), headers);
542+
}
543+
return responseHeaders;
544+
}
545+
522546
/**
523547
* Update query and header parameters based on authentication settings.
524548
*
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package {{package}};
2+
3+
import {{invokerPackage}}.ApiException;
4+
import {{invokerPackage}}.ApiClient;
5+
import {{invokerPackage}}.Configuration;
6+
import {{invokerPackage}}.Pair;
7+
import {{invokerPackage}}.TypeRef;
8+
9+
import {{modelPackage}}.*;
10+
11+
import java.util.*;
12+
13+
{{#imports}}import {{import}};
14+
{{/imports}}
15+
16+
import java.io.File;
17+
import java.util.Map;
18+
import java.util.HashMap;
19+
20+
{{>generatedAnnotation}}
21+
{{#operations}}
22+
public class {{classname}} {
23+
private ApiClient {{localVariablePrefix}}apiClient;
24+
25+
public {{classname}}() {
26+
this(Configuration.getDefaultApiClient());
27+
}
28+
29+
public {{classname}}(ApiClient apiClient) {
30+
this.{{localVariablePrefix}}apiClient = apiClient;
31+
}
32+
33+
public ApiClient getApiClient() {
34+
return {{localVariablePrefix}}apiClient;
35+
}
36+
37+
public void setApiClient(ApiClient apiClient) {
38+
this.{{localVariablePrefix}}apiClient = apiClient;
39+
}
40+
41+
{{#operation}}
42+
/**
43+
* {{summary}}
44+
* {{notes}}
45+
{{#allParams}} * @param {{paramName}} {{description}}
46+
{{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
47+
*/
48+
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
49+
Object {{localVariablePrefix}}postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
50+
{{#allParams}}{{#required}}
51+
// verify the required parameter '{{paramName}}' is set
52+
if ({{paramName}} == null) {
53+
throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{nickname}}");
54+
}
55+
{{/required}}{{/allParams}}
56+
// create path and map variables
57+
String {{localVariablePrefix}}path = "{{{path}}}".replaceAll("\\{format\\}","json"){{#pathParams}}
58+
.replaceAll("\\{" + "{{baseName}}" + "\\}", {{localVariablePrefix}}apiClient.escapeString({{{paramName}}}.toString())){{/pathParams}};
59+
60+
// query params
61+
List<Pair> {{localVariablePrefix}}queryParams = new ArrayList<Pair>();
62+
Map<String, String> {{localVariablePrefix}}headerParams = new HashMap<String, String>();
63+
Map<String, Object> {{localVariablePrefix}}formParams = new HashMap<String, Object>();
64+
65+
{{#queryParams}}
66+
{{localVariablePrefix}}queryParams.addAll({{localVariablePrefix}}apiClient.parameterToPairs("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
67+
{{/queryParams}}
68+
69+
{{#headerParams}}if ({{paramName}} != null)
70+
{{localVariablePrefix}}headerParams.put("{{baseName}}", {{localVariablePrefix}}apiClient.parameterToString({{paramName}}));
71+
{{/headerParams}}
72+
73+
{{#formParams}}if ({{paramName}} != null)
74+
{{localVariablePrefix}}formParams.put("{{baseName}}", {{paramName}});
75+
{{/formParams}}
76+
77+
final String[] {{localVariablePrefix}}accepts = {
78+
{{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
79+
};
80+
final String {{localVariablePrefix}}accept = {{localVariablePrefix}}apiClient.selectHeaderAccept({{localVariablePrefix}}accepts);
81+
82+
final String[] {{localVariablePrefix}}contentTypes = {
83+
{{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}
84+
};
85+
final String {{localVariablePrefix}}contentType = {{localVariablePrefix}}apiClient.selectHeaderContentType({{localVariablePrefix}}contentTypes);
86+
87+
String[] {{localVariablePrefix}}authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
88+
89+
{{#returnType}}
90+
TypeRef {{localVariablePrefix}}returnType = new TypeRef<{{{returnType}}}>() {};
91+
return {{localVariablePrefix}}apiClient.invokeAPI({{localVariablePrefix}}path, "{{httpMethod}}", {{localVariablePrefix}}queryParams, {{localVariablePrefix}}postBody, {{localVariablePrefix}}headerParams, {{localVariablePrefix}}formParams, {{localVariablePrefix}}accept, {{localVariablePrefix}}contentType, {{localVariablePrefix}}authNames, {{localVariablePrefix}}returnType);
92+
{{/returnType}}{{^returnType}}
93+
{{localVariablePrefix}}apiClient.invokeAPI({{localVariablePrefix}}path, "{{httpMethod}}", {{localVariablePrefix}}queryParams, {{localVariablePrefix}}postBody, {{localVariablePrefix}}headerParams, {{localVariablePrefix}}formParams, {{localVariablePrefix}}accept, {{localVariablePrefix}}contentType, {{localVariablePrefix}}authNames, null);
94+
{{/returnType}}
95+
}
96+
{{/operation}}
97+
}
98+
{{/operations}}

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiCallback.mustache

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package {{invokerPackage}};
22

33
import java.io.IOException;
44

5+
import java.util.Map;
6+
import java.util.List;
7+
58
/**
69
* Callback for asynchronous API call.
710
*
@@ -10,13 +13,19 @@ import java.io.IOException;
1013
public interface ApiCallback<T> {
1114
/**
1215
* This is called when the API call fails.
16+
*
17+
* @param e The exception causing the failure
18+
* @param statusCode Status code of the response if available, otherwise it would be 0
19+
* @param responseHeaders Headers of the response if available, otherwise it would be null
1320
*/
14-
void onFailure(ApiException e);
21+
void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders);
1522
1623
/**
1724
* This is called when the API call succeeded.
1825
*
1926
* @param result The result deserialized from response
27+
* @param statusCode Status code of the response
28+
* @param responseHeaders Headers of the response
2029
*/
21-
void onSuccess(T result);
30+
void onSuccess(T result, int statusCode, Map<String, List<String>> responseHeaders);
2231
}

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class ApiClient {
4848
4949
private Map<String, Authentication> authentications;
5050
51+
private int statusCode;
52+
private Map<String, List<String>> responseHeaders;
53+
5154
private String dateFormat;
5255
private DateFormat dateFormatter;
5356
private int dateLength;
@@ -107,6 +110,24 @@ public class ApiClient {
107110
return this;
108111
}
109112

113+
/**
114+
* Gets the status code of the previous request.
115+
* NOTE: Status code of last async response is not recorded here, it is
116+
* passed to the callback methods instead.
117+
*/
118+
public int getStatusCode() {
119+
return statusCode;
120+
}
121+
122+
/**
123+
* Gets the response headers of the previous request.
124+
* NOTE: Headers of last async response is not recorded here, it is passed
125+
* to callback methods instead.
126+
*/
127+
public Map<String, List<String>> getResponseHeaders() {
128+
return responseHeaders;
129+
}
130+
110131
public String getDateFormat() {
111132
return dateFormat;
112133
}
@@ -534,6 +555,8 @@ public class ApiClient {
534555
public <T> T execute(Call call, Type returnType) throws ApiException {
535556
try {
536557
Response response = call.execute();
558+
this.statusCode = response.code();
559+
this.responseHeaders = response.headers().toMultimap();
537560
return handleResponse(response, returnType);
538561
} catch (IOException e) {
539562
throw new ApiException(e);
@@ -557,7 +580,7 @@ public class ApiClient {
557580
call.enqueue(new Callback() {
558581
@Override
559582
public void onFailure(Request request, IOException e) {
560-
callback.onFailure(new ApiException(e));
583+
callback.onFailure(new ApiException(e), 0, null);
561584
}
562585

563586
@Override
@@ -566,10 +589,10 @@ public class ApiClient {
566589
try {
567590
result = (T) handleResponse(response, returnType);
568591
} catch (ApiException e) {
569-
callback.onFailure(e);
592+
callback.onFailure(e, response.code(), response.headers().toMultimap());
570593
return;
571594
}
572-
callback.onSuccess(result);
595+
callback.onSuccess(result, response.code(), response.headers().toMultimap());
573596
}
574597
});
575598
}

0 commit comments

Comments
 (0)