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 Details

    • ClientResponseHandler

      public ClientResponseHandler(@Nonnull Class<T> successType, @Nonnull Class<? extends R> errorType, @Nonnull ClientExceptionFactory<E,R> exceptionFactory)
      Creates a new ClientResponseHandler instance.
      Parameters:
      successType - The HTTP success response type
      errorType - The HTTP error response type
      exceptionFactory - The factory to create exceptions for Http 4xx/5xx responses.
  • Method Details

    • objectMapper

      @Beta @Nonnull public ClientResponseHandler<T,R,E> objectMapper(@Nonnull com.fasterxml.jackson.databind.ObjectMapper jackson)
      Set the ObjectMapper to use for parsing JSON responses.
      Parameters:
      jackson - The ObjectMapper to use
      Returns:
      the current instance of ClientResponseHandler with the changed object mapper
    • handleResponse

      @Nonnull public T handleResponse(@Nonnull org.apache.hc.core5.http.ClassicHttpResponse response) throws E
      Processes a ClassicHttpResponse and returns some value corresponding to that response.
      Specified by:
      handleResponse in interface org.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