forked from loafle/openapi-generator-original
Java-okhttp-gson: allow access to status code and response headers of last (async) request
This commit is contained in:
parent
def3f5b3fb
commit
34f196a144
@ -2,6 +2,9 @@ package {{invokerPackage}};
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback for asynchronous API call.
|
* Callback for asynchronous API call.
|
||||||
*
|
*
|
||||||
@ -10,13 +13,19 @@ import java.io.IOException;
|
|||||||
public interface ApiCallback<T> {
|
public interface ApiCallback<T> {
|
||||||
/**
|
/**
|
||||||
* This is called when the API call fails.
|
* 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.
|
* This is called when the API call succeeded.
|
||||||
*
|
*
|
||||||
* @param result The result deserialized from response
|
* @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);
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,9 @@ public class ApiClient {
|
|||||||
|
|
||||||
private Map<String, Authentication> authentications;
|
private Map<String, Authentication> authentications;
|
||||||
|
|
||||||
|
private int statusCode;
|
||||||
|
private Map<String, List<String>> responseHeaders;
|
||||||
|
|
||||||
private String dateFormat;
|
private String dateFormat;
|
||||||
private DateFormat dateFormatter;
|
private DateFormat dateFormatter;
|
||||||
private int dateLength;
|
private int dateLength;
|
||||||
@ -107,6 +110,24 @@ public class ApiClient {
|
|||||||
return this;
|
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() {
|
public String getDateFormat() {
|
||||||
return dateFormat;
|
return dateFormat;
|
||||||
}
|
}
|
||||||
@ -534,6 +555,8 @@ public class ApiClient {
|
|||||||
public <T> T execute(Call call, Type returnType) throws ApiException {
|
public <T> T execute(Call call, Type returnType) throws ApiException {
|
||||||
try {
|
try {
|
||||||
Response response = call.execute();
|
Response response = call.execute();
|
||||||
|
this.statusCode = response.code();
|
||||||
|
this.responseHeaders = response.headers().toMultimap();
|
||||||
return handleResponse(response, returnType);
|
return handleResponse(response, returnType);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ApiException(e);
|
throw new ApiException(e);
|
||||||
@ -557,7 +580,7 @@ public class ApiClient {
|
|||||||
call.enqueue(new Callback() {
|
call.enqueue(new Callback() {
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Request request, IOException e) {
|
public void onFailure(Request request, IOException e) {
|
||||||
callback.onFailure(new ApiException(e));
|
callback.onFailure(new ApiException(e), 0, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -566,10 +589,10 @@ public class ApiClient {
|
|||||||
try {
|
try {
|
||||||
result = (T) handleResponse(response, returnType);
|
result = (T) handleResponse(response, returnType);
|
||||||
} catch (ApiException e) {
|
} catch (ApiException e) {
|
||||||
callback.onFailure(e);
|
callback.onFailure(e, response.code(), response.headers().toMultimap());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
callback.onSuccess(result);
|
callback.onSuccess(result, response.code(), response.headers().toMultimap());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user