Interface ResponseBuilder<T,​E extends RestException>

  • Type Parameters:
    T - the body type if the status code is considered successful
    E - the exception type if the status code is considered a failure
    All Known Implementing Classes:
    ServiceResponseBuilder

    public interface ResponseBuilder<T,​E extends RestException>
    Defines an interface that can process a Retrofit 2 response into a deserialized body or an exception, depending on the status code registered.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static interface  ResponseBuilder.Factory
      A factory that creates a builder based on the return type and the exception type.
    • Method Detail

      • register

        ResponseBuilder<T,​E> register​(int statusCode,
                                            Type type)
        Register a mapping from a response status code to a response destination type.
        Parameters:
        statusCode - the status code.
        type - the type to deserialize.
        Returns:
        the same builder instance.
      • registerError

        ResponseBuilder<T,​E> registerError​(Class<? extends RestException> type)
        Register a destination type for errors with models.
        Parameters:
        type - the type to deserialize.
        Returns:
        the same builder instance.
      • build

        ServiceResponse<T> build​(retrofit2.Response<okhttp3.ResponseBody> response)
                          throws IOException
        Build a ServiceResponse instance from a REST call response and a possible error.

        If the status code in the response is registered, the response will be considered valid and deserialized into the specified destination type. If the status code is not registered, the response will be considered invalid and deserialized into the specified error type if there is one. An AutoRestException is also thrown.

        Parameters:
        response - the Response instance from REST call
        Returns:
        a ServiceResponse instance of generic type ResponseBuilder
        Throws:
        E - exceptions from the REST call
        IOException - exceptions from deserialization
      • buildEmpty

        ServiceResponse<T> buildEmpty​(retrofit2.Response<Void> response)
                               throws IOException
        Build a ServiceResponse instance from a REST call response and a possible error, which does not have a response body.

        If the status code in the response is registered, the response will be considered valid. If the status code is not registered, the response will be considered invalid. An AutoRestException is also thrown.

        Parameters:
        response - the Response instance from REST call
        Returns:
        a ServiceResponse instance of generic type ResponseBuilder
        Throws:
        E - exceptions from the REST call
        IOException - exceptions from deserialization
      • buildWithHeaders

        <THeader> ServiceResponseWithHeaders<T,​THeader> buildWithHeaders​(retrofit2.Response<okhttp3.ResponseBody> response,
                                                                               Class<THeader> headerType)
                                                                        throws IOException
        Build a ServiceResponseWithHeaders instance from a REST call response, a header in JSON format, and a possible error.

        If the status code in the response is registered, the response will be considered valid and deserialized into the specified destination type. If the status code is not registered, the response will be considered invalid and deserialized into the specified error type if there is one. An AutoRestException is also thrown.

        Type Parameters:
        THeader - the type of the header
        Parameters:
        response - the Response instance from REST call
        headerType - the type of the header
        Returns:
        a ServiceResponseWithHeaders instance of generic type ResponseBuilder
        Throws:
        E - exceptions from the REST call
        IOException - exceptions from deserialization
      • buildEmptyWithHeaders

        <THeader> ServiceResponseWithHeaders<T,​THeader> buildEmptyWithHeaders​(retrofit2.Response<Void> response,
                                                                                    Class<THeader> headerType)
                                                                             throws IOException
        Build a ServiceResponseWithHeaders instance from a REST call response, a header in JSON format, and a possible error, which does not have a response body.

        If the status code in the response is registered, the response will be considered valid. If the status code is not registered, the response will be considered invalid. An AutoRestException is also thrown.

        Type Parameters:
        THeader - the type of the header
        Parameters:
        response - the Response instance from REST call
        headerType - the type of the header
        Returns:
        a ServiceResponseWithHeaders instance of generic type ResponseBuilder
        Throws:
        E - exceptions from the REST call
        IOException - exceptions from deserialization