Package com.sap.ai.sdk.core.common
Class ClientResponseHandler<T,R extends ClientError,E extends ClientException>
java.lang.Object
com.sap.ai.sdk.core.common.ClientResponseHandler<T,R,E>
- Type Parameters:
T- The type of the successful response.R- The type of the error response.E- The type of the exception to throw.
- All Implemented Interfaces:
org.apache.hc.core5.http.io.HttpClientResponseHandler<T>
- Direct Known Subclasses:
ClientStreamingHandler
@Beta
public class ClientResponseHandler<T,R extends ClientError,E extends ClientException>
extends Object
implements org.apache.hc.core5.http.io.HttpClientResponseHandler<T>
Parse incoming JSON responses and handles any errors. For internal use only.
- Since:
- 1.1.0
-
Constructor Summary
ConstructorsConstructorDescriptionClientResponseHandler(Class<T> successType, Class<? extends R> errorType, ClientExceptionFactory<E, R> exceptionFactory) Creates a newClientResponseHandlerinstance. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbuildAndThrowException(org.apache.hc.core5.http.ClassicHttpResponse httpResponse) Process the error response and throw an exception.handleResponse(org.apache.hc.core5.http.ClassicHttpResponse response) Processes aClassicHttpResponseand returns some value corresponding to that response.objectMapper(com.fasterxml.jackson.databind.ObjectMapper jackson) Set theObjectMapperto use for parsing JSON responses.protected voidparseErrorResponseAndThrow(String content, org.apache.hc.core5.http.ClassicHttpResponse httpResponse) Parses the JSON content of an error response and throws a module specific exception.
-
Constructor Details
-
ClientResponseHandler
public ClientResponseHandler(@Nonnull Class<T> successType, @Nonnull Class<? extends R> errorType, @Nonnull ClientExceptionFactory<E, R> exceptionFactory) Creates a newClientResponseHandlerinstance.- Parameters:
successType- The HTTP success response typeerrorType- The HTTP error response typeexceptionFactory- The factory to create exceptions for Http 4xx/5xx responses.
-
-
Method Details
-
objectMapper
@Beta @Nonnull public ClientResponseHandler<T,R, objectMapperE> (@Nonnull com.fasterxml.jackson.databind.ObjectMapper jackson) Set theObjectMapperto use for parsing JSON responses.- Parameters:
jackson- TheObjectMapperto use- Returns:
- the current instance of
ClientResponseHandlerwith the changed object mapper
-
handleResponse
@Nonnull public T handleResponse(@Nonnull org.apache.hc.core5.http.ClassicHttpResponse response) throws E Processes aClassicHttpResponseand returns some value corresponding to that response.- Specified by:
handleResponsein interfaceorg.apache.hc.core5.http.io.HttpClientResponseHandler<T>- Parameters:
response- The response to process- Returns:
- A model class instantiated from the response
- Throws:
E- in case of a problem or the connection was aborted
-
buildAndThrowException
protected void buildAndThrowException(@Nonnull org.apache.hc.core5.http.ClassicHttpResponse httpResponse) throws E Process the error response and throw an exception.- Parameters:
httpResponse- The response to process- Throws:
ClientException- if the response is an error (4xx/5xx)E
-
parseErrorResponseAndThrow
protected void parseErrorResponseAndThrow(@Nonnull String content, @Nonnull org.apache.hc.core5.http.ClassicHttpResponse httpResponse) throws E Parses the JSON content of an error response and throws a module specific exception.- Parameters:
content- The JSON content of the error response.httpResponse- The HTTP response that contains the error.- Throws:
ClientException- if the response is an error (4xx/5xx)E
-