Skip to content

[Java jersey2 okhttp-gson] Fix compilation error, record status code and response headers of last request #1215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class ApiClient {

private Map<String, Authentication> authentications;

private int statusCode;
private Map<String, List<String>> responseHeaders;

private DateFormat dateFormat;

public ApiClient() {
Expand Down Expand Up @@ -84,6 +87,20 @@ public class ApiClient {
return this;
}

/**
* Gets the status code of the previous request
*/
public int getStatusCode() {
return statusCode;
}

/**
* Gets the response headers of the previous request
*/
public Map<String, List<String>> getResponseHeaders() {
return responseHeaders;
}

/**
* Get authentications (key: authentication name, value: authentication).
*/
Expand Down Expand Up @@ -484,6 +501,9 @@ public class ApiClient {
throw new ApiException(500, "unknown method type " + method);
}

statusCode = response.getStatusInfo().getStatusCode();
responseHeaders = buildResponseHeaders(response);

if (response.getStatus() == Status.NO_CONTENT.getStatusCode()) {
return null;
} else if (response.getStatusInfo().getFamily().equals(Status.Family.SUCCESSFUL)) {
Expand All @@ -502,23 +522,27 @@ public class ApiClient {
// e.printStackTrace();
}
}
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
for (String key: response.getHeaders().keySet()) {
List<Object> values = response.getHeaders().get(key);
List<String> headers = new ArrayList<String>();
for (Object o : values) {
headers.add(String.valueOf(o));
}
responseHeaders.put(key, headers);
}
throw new ApiException(
response.getStatus(),
message,
responseHeaders,
buildResponseHeaders(response),
respBody);
}
}

private Map<String, List<String>> buildResponseHeaders(Response response) {
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
List<Object> values = entry.getValue();
List<String> headers = new ArrayList<String>();
for (Object o : values) {
headers.add(String.valueOf(o));
}
responseHeaders.put(entry.getKey(), headers);
}
return responseHeaders;
}

/**
* Update query and header parameters based on authentication settings.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package {{package}};

import {{invokerPackage}}.ApiException;
import {{invokerPackage}}.ApiClient;
import {{invokerPackage}}.Configuration;
import {{invokerPackage}}.Pair;
import {{invokerPackage}}.TypeRef;

import {{modelPackage}}.*;

import java.util.*;

{{#imports}}import {{import}};
{{/imports}}

import java.io.File;
import java.util.Map;
import java.util.HashMap;

{{>generatedAnnotation}}
{{#operations}}
public class {{classname}} {
private ApiClient {{localVariablePrefix}}apiClient;

public {{classname}}() {
this(Configuration.getDefaultApiClient());
}

public {{classname}}(ApiClient apiClient) {
this.{{localVariablePrefix}}apiClient = apiClient;
}

public ApiClient getApiClient() {
return {{localVariablePrefix}}apiClient;
}

public void setApiClient(ApiClient apiClient) {
this.{{localVariablePrefix}}apiClient = apiClient;
}

{{#operation}}
/**
* {{summary}}
* {{notes}}
{{#allParams}} * @param {{paramName}} {{description}}
{{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
*/
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
Object {{localVariablePrefix}}postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
{{#allParams}}{{#required}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null) {
throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{nickname}}");
}
{{/required}}{{/allParams}}
// create path and map variables
String {{localVariablePrefix}}path = "{{{path}}}".replaceAll("\\{format\\}","json"){{#pathParams}}
.replaceAll("\\{" + "{{baseName}}" + "\\}", {{localVariablePrefix}}apiClient.escapeString({{{paramName}}}.toString())){{/pathParams}};

// query params
List<Pair> {{localVariablePrefix}}queryParams = new ArrayList<Pair>();
Map<String, String> {{localVariablePrefix}}headerParams = new HashMap<String, String>();
Map<String, Object> {{localVariablePrefix}}formParams = new HashMap<String, Object>();

{{#queryParams}}
{{localVariablePrefix}}queryParams.addAll({{localVariablePrefix}}apiClient.parameterToPairs("{{#collectionFormat}}{{{collectionFormat}}}{{/collectionFormat}}", "{{baseName}}", {{paramName}}));
{{/queryParams}}

{{#headerParams}}if ({{paramName}} != null)
{{localVariablePrefix}}headerParams.put("{{baseName}}", {{localVariablePrefix}}apiClient.parameterToString({{paramName}}));
{{/headerParams}}

{{#formParams}}if ({{paramName}} != null)
{{localVariablePrefix}}formParams.put("{{baseName}}", {{paramName}});
{{/formParams}}

final String[] {{localVariablePrefix}}accepts = {
{{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
};
final String {{localVariablePrefix}}accept = {{localVariablePrefix}}apiClient.selectHeaderAccept({{localVariablePrefix}}accepts);

final String[] {{localVariablePrefix}}contentTypes = {
{{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}
};
final String {{localVariablePrefix}}contentType = {{localVariablePrefix}}apiClient.selectHeaderContentType({{localVariablePrefix}}contentTypes);

String[] {{localVariablePrefix}}authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };

{{#returnType}}
TypeRef {{localVariablePrefix}}returnType = new TypeRef<{{{returnType}}}>() {};
return {{localVariablePrefix}}apiClient.invokeAPI({{localVariablePrefix}}path, "{{httpMethod}}", {{localVariablePrefix}}queryParams, {{localVariablePrefix}}postBody, {{localVariablePrefix}}headerParams, {{localVariablePrefix}}formParams, {{localVariablePrefix}}accept, {{localVariablePrefix}}contentType, {{localVariablePrefix}}authNames, {{localVariablePrefix}}returnType);
{{/returnType}}{{^returnType}}
{{localVariablePrefix}}apiClient.invokeAPI({{localVariablePrefix}}path, "{{httpMethod}}", {{localVariablePrefix}}queryParams, {{localVariablePrefix}}postBody, {{localVariablePrefix}}headerParams, {{localVariablePrefix}}formParams, {{localVariablePrefix}}accept, {{localVariablePrefix}}contentType, {{localVariablePrefix}}authNames, null);
{{/returnType}}
}
{{/operation}}
}
{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package {{invokerPackage}};

import java.io.IOException;

import java.util.Map;
import java.util.List;

/**
* Callback for asynchronous API call.
*
Expand All @@ -10,13 +13,19 @@ import java.io.IOException;
public interface ApiCallback<T> {
/**
* This is called when the API call fails.
*
* @param e The exception causing the failure
* @param statusCode Status code of the response if available, otherwise it would be 0
* @param responseHeaders Headers of the response if available, otherwise it would be null
*/
void onFailure(ApiException e);
void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders);

/**
* This is called when the API call succeeded.
*
* @param result The result deserialized from response
* @param statusCode Status code of the response
* @param responseHeaders Headers of the response
*/
void onSuccess(T result);
void onSuccess(T result, int statusCode, Map<String, List<String>> responseHeaders);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class ApiClient {

private Map<String, Authentication> authentications;

private int statusCode;
private Map<String, List<String>> responseHeaders;

private String dateFormat;
private DateFormat dateFormatter;
private int dateLength;
Expand Down Expand Up @@ -107,6 +110,24 @@ 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 String getDateFormat() {
return dateFormat;
}
Expand Down Expand Up @@ -534,6 +555,8 @@ public class ApiClient {
public <T> 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);
} catch (IOException e) {
throw new ApiException(e);
Expand All @@ -557,7 +580,7 @@ public class ApiClient {
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
callback.onFailure(new ApiException(e));
callback.onFailure(new ApiException(e), 0, null);
}

@Override
Expand All @@ -566,10 +589,10 @@ public class ApiClient {
try {
result = (T) handleResponse(response, returnType);
} catch (ApiException e) {
callback.onFailure(e);
callback.onFailure(e, response.code(), response.headers().toMultimap());
return;
}
callback.onSuccess(result);
callback.onSuccess(result, response.code(), response.headers().toMultimap());
}
});
}
Expand Down
Loading