mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 06:00:52 +00:00
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.util.Map;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Callback for asynchronous API call.
|
||||
*
|
||||
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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;
|
||||
}
|
||||
@ -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);
|
||||
@ -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());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user