mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-11 10:02:46 +00:00
Adding Response Interceptor (#5500)
* added Response interceptor for native clients * added Response interceptor for native clients
This commit is contained in:
committed by
GitHub
parent
05cb1f88d1
commit
e99b3e038a
@@ -8,10 +8,12 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import org.openapitools.jackson.nullable.JsonNullableModule;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.Charset;
|
||||
import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
@@ -46,6 +48,7 @@ public class ApiClient {
|
||||
private int port;
|
||||
private String basePath;
|
||||
private Consumer<HttpRequest.Builder> interceptor;
|
||||
private Consumer<HttpResponse<InputStream>> responseInterceptor;
|
||||
private Duration readTimeout;
|
||||
|
||||
private static String valueToString(Object value) {
|
||||
@@ -161,6 +164,7 @@ public class ApiClient {
|
||||
basePath = baseURI.getRawPath();
|
||||
interceptor = null;
|
||||
readTimeout = null;
|
||||
responseInterceptor = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -292,6 +296,29 @@ public class ApiClient {
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom response interceptor.
|
||||
*
|
||||
* <p>This is useful for logging, monitoring or extraction of header variables</p>
|
||||
*
|
||||
* @param interceptor A function invoked before creating each request. A value
|
||||
* of null resets the interceptor to a no-op.
|
||||
* @return This object.
|
||||
*/
|
||||
public ApiClient setResponseInterceptor(Consumer<HttpResponse<InputStream>> interceptor) {
|
||||
this.responseInterceptor = interceptor;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the custom response interceptor.
|
||||
*
|
||||
* @return The custom interceptor that was set, or null if there isn't any.
|
||||
*/
|
||||
public Consumer<HttpResponse<InputStream>> getResponseInterceptor() {
|
||||
return responseInterceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the read timeout for the http client.
|
||||
*
|
||||
@@ -307,7 +334,7 @@ public class ApiClient {
|
||||
this.readTimeout = readTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the read timeout that was set.
|
||||
*
|
||||
|
||||
@@ -36,7 +36,8 @@ public class {{classname}} {
|
||||
private final String memberVarBaseUri;
|
||||
private final Consumer<HttpRequest.Builder> memberVarInterceptor;
|
||||
private final Duration memberVarReadTimeout;
|
||||
|
||||
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
|
||||
|
||||
public {{classname}}() {
|
||||
this(new ApiClient());
|
||||
}
|
||||
@@ -47,6 +48,7 @@ public class {{classname}} {
|
||||
memberVarBaseUri = apiClient.getBaseUri();
|
||||
memberVarInterceptor = apiClient.getRequestInterceptor();
|
||||
memberVarReadTimeout = apiClient.getReadTimeout();
|
||||
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
|
||||
}
|
||||
|
||||
{{#operation}}
|
||||
@@ -138,6 +140,9 @@ public class {{classname}} {
|
||||
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
|
||||
localVarRequestBuilder.build(),
|
||||
HttpResponse.BodyHandlers.ofInputStream());
|
||||
if (memberVarResponseInterceptor != null) {
|
||||
memberVarResponseInterceptor.accept(localVarResponse);
|
||||
}
|
||||
if (localVarResponse.statusCode()/ 100 != 2) {
|
||||
throw new ApiException(localVarResponse.statusCode(),
|
||||
"{{operationId}} call received non-success response",
|
||||
|
||||
Reference in New Issue
Block a user