Add WithHttpInfo API methods to Java okhttp-gson client

to allow accessing response status code and headers
and removed the methods of recording last response info from
ApiClient.
This commit is contained in:
xhh
2015-12-05 20:34:50 +08:00
parent c5a48d9891
commit b1bc75189a
10 changed files with 378 additions and 114 deletions

View File

@@ -104,9 +104,6 @@ public class ApiClient {
private Map<String, Authentication> authentications;
private int statusCode;
private Map<String, List<String>> responseHeaders;
private DateFormat dateFormat;
private DateFormat datetimeFormat;
private boolean lenientDatetimeFormat;
@@ -177,24 +174,6 @@ public class ApiClient {
return this;
}
/**
* Gets the status code of the previous request.
* NOTE: Status code of last async response is not recorded here, it is
* passed to the callback methods instead.
*/
public int getStatusCode() {
return statusCode;
}
/**
* Gets the response headers of the previous request.
* NOTE: Headers of last async response is not recorded here, it is passed
* to callback methods instead.
*/
public Map<String, List<String>> getResponseHeaders() {
return responseHeaders;
}
public boolean isVerifyingSsl() {
return verifyingSsl;
}
@@ -731,7 +710,7 @@ public class ApiClient {
/**
* @see #execute(Call, Type)
*/
public <T> T execute(Call call) throws ApiException {
public <T> ApiResponse<T> execute(Call call) throws ApiException {
return execute(call, null);
}
@@ -740,14 +719,15 @@ public class ApiClient {
*
* @param returnType The return type used to deserialize HTTP response body
* @param <T> The return type corresponding to (same with) returnType
* @return The Java object deserialized from response body. Returns null if returnType is null.
* @return <code>ApiResponse</code> object containing response status, headers and
* data, which is a Java object deserialized from response body and would be null
* when returnType is null.
*/
public <T> T execute(Call call, Type returnType) throws ApiException {
public <T> ApiResponse<T> execute(Call call, Type returnType) throws ApiException {
try {
Response response = call.execute();
this.statusCode = response.code();
this.responseHeaders = response.headers().toMultimap();
return handleResponse(response, returnType);
T data = handleResponse(response, returnType);
return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
} catch (IOException e) {
throw new ApiException(e);
}

View File

@@ -0,0 +1,32 @@
package {{invokerPackage}};
import java.util.List;
import java.util.Map;
public class ApiResponse<T> {
final private int statusCode;
final private Map<String, List<String>> headers;
final private T data;
public ApiResponse(int statusCode, Map<String, List<String>> headers) {
this(statusCode, headers, null);
}
public ApiResponse(int statusCode, Map<String, List<String>> headers, T data) {
this.statusCode = statusCode;
this.headers = headers;
this.data = data;
}
public int getStatusCode() {
return statusCode;
}
public Map<String, List<String>> getHeaders() {
return headers;
}
public T getData() {
return data;
}
}

View File

@@ -3,6 +3,7 @@ package {{package}};
import {{invokerPackage}}.ApiCallback;
import {{invokerPackage}}.ApiClient;
import {{invokerPackage}}.ApiException;
import {{invokerPackage}}.ApiResponse;
import {{invokerPackage}}.Configuration;
import {{invokerPackage}}.Pair;
import {{invokerPackage}}.ProgressRequestBody;
@@ -106,9 +107,20 @@ public class {{classname}} {
* @return {{{returnType}}}{{/returnType}}
*/
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
{{#returnType}}ApiResponse<{{{returnType}}}> {{localVariablePrefix}}resp = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}}
return {{localVariablePrefix}}resp.getData();{{/returnType}}
}
/**
* {{summary}}
* {{notes}}{{#allParams}}
* @param {{paramName}} {{description}}{{/allParams}}
* @return ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>
*/
public ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
Call {{localVariablePrefix}}call = {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}null, null);
{{#returnType}}Type {{localVariablePrefix}}returnType = new TypeToken<{{{returnType}}}>(){}.getType();
return {{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call, {{localVariablePrefix}}returnType);{{/returnType}}{{^returnType}}{{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call);{{/returnType}}
return {{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call, {{localVariablePrefix}}returnType);{{/returnType}}{{^returnType}}return {{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call);{{/returnType}}
}
/**