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`

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

  Constructors
  Constructor
  Description
  `ClientResponseHandler(Class<T> successType, Class<? extends R> errorType, ClientExceptionFactory<E,R> exceptionFactory)`
  Creates a new `ClientResponseHandler` instance.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected void`
  `buildAndThrowException(org.apache.hc.core5.http.ClassicHttpResponse httpResponse)`
  Process the error response and throw an exception.
  `T`
  `handleResponse(org.apache.hc.core5.http.ClassicHttpResponse response)`
  Processes a `ClassicHttpResponse` and returns some value corresponding to that response.
  `ClientResponseHandler<T,R,E>`
  `objectMapper(com.fasterxml.jackson.databind.ObjectMapper jackson)`
  Set the `ObjectMapper` to use for parsing JSON responses.
  `protected void`
  `parseErrorResponseAndThrow(String content, org.apache.hc.core5.http.ClassicHttpResponse httpResponse)`
  Parses the JSON content of an error response and throws a module specific exception.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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`