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

public class ClientResponseHandler<T,R extends ClientError,E extends ClientException> extends Object implements org.apache.hc.core5.http.io.HttpClientResponseHandler<T>
Parses 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

      @Nonnull public ClientResponseHandler<T,R,E> objectMapper(@Nonnull com.fasterxml.jackson.databind.ObjectMapper jackson)
      Internal SDK usage only. 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
      Internal SDK usage only. 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
      For internal SDK usage only. 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
      For internal SDK usage only. 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