From 34f196a144a3cb705ead9d5474320c0ee5b32fdd Mon Sep 17 00:00:00 2001 From: xhh Date: Fri, 11 Sep 2015 13:15:55 +0800 Subject: [PATCH] Java-okhttp-gson: allow access to status code and response headers of last (async) request --- .../okhttp-gson/ApiCallback.mustache | 13 +++++++-- .../libraries/okhttp-gson/ApiClient.mustache | 29 +++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiCallback.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiCallback.mustache index 4af16c9faa9..6eee875bdcc 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiCallback.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiCallback.mustache @@ -2,6 +2,9 @@ package {{invokerPackage}}; import java.io.IOException; +import java.util.Map; +import java.util.List; + /** * Callback for asynchronous API call. * @@ -10,13 +13,19 @@ import java.io.IOException; public interface ApiCallback { /** * 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> 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> responseHeaders); } diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache index 069aa273b14..f8d7fad3849 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache @@ -48,6 +48,9 @@ public class ApiClient { private Map authentications; + private int statusCode; + private Map> responseHeaders; + private String dateFormat; private DateFormat dateFormatter; private int dateLength; @@ -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> getResponseHeaders() { + return responseHeaders; + } + public String getDateFormat() { return dateFormat; } @@ -534,6 +555,8 @@ public class ApiClient { public 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); @@ -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 @@ -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()); } }); }