Class ServiceResponseBuilder<T,​E extends RestException>

  • Type Parameters:
    T - The return type the caller expects from the REST response.
    E - the exception to throw in case of error.
    All Implemented Interfaces:
    ResponseBuilder<T,​E>

    public final class ServiceResponseBuilder<T,​E extends RestException>
    extends Object
    implements ResponseBuilder<T,​E>
    The builder for building a ServiceResponse.
    • Method Detail

      • register

        public ServiceResponseBuilder<T,​E> register​(int statusCode,
                                                          Type type)
        Description copied from interface: ResponseBuilder
        Register a mapping from a response status code to a response destination type.
        Specified by:
        register in interface ResponseBuilder<T,​E extends RestException>
        Parameters:
        statusCode - the status code.
        type - the type to deserialize.
        Returns:
        the same builder instance.
      • registerAll

        public ServiceResponseBuilder<T,​E> registerAll​(Map<Integer,​Type> responseTypes)
        Register all the mappings from a response status code to a response destination type stored in a Map.
        Parameters:
        responseTypes - the mapping from response status codes to response types.
        Returns:
        the same builder instance.
      • build

        public ServiceResponse<T> build​(retrofit2.Response<okhttp3.ResponseBody> response)
                                 throws IOException
        Description copied from interface: ResponseBuilder
        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.

        Specified by:
        build in interface ResponseBuilder<T,​E extends RestException>
        Parameters:
        response - the Response instance from REST call
        Returns:
        a ServiceResponse instance of generic type ResponseBuilder
        Throws:
        IOException - exceptions from deserialization
      • buildEmpty

        public ServiceResponse<T> buildEmpty​(retrofit2.Response<Void> response)
                                      throws IOException
        Description copied from interface: ResponseBuilder
        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.

        Specified by:
        buildEmpty in interface ResponseBuilder<T,​E extends RestException>
        Parameters:
        response - the Response instance from REST call
        Returns:
        a ServiceResponse instance of generic type ResponseBuilder
        Throws:
        IOException - exceptions from deserialization
      • buildWithHeaders

        public <THeader> ServiceResponseWithHeaders<T,​THeader> buildWithHeaders​(retrofit2.Response<okhttp3.ResponseBody> response,
                                                                                      Class<THeader> headerType)
                                                                               throws IOException
        Description copied from interface: ResponseBuilder
        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.

        Specified by:
        buildWithHeaders in interface ResponseBuilder<T,​E extends RestException>
        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:
        IOException - exceptions from deserialization
      • buildEmptyWithHeaders

        public <THeader> ServiceResponseWithHeaders<T,​THeader> buildEmptyWithHeaders​(retrofit2.Response<Void> response,
                                                                                           Class<THeader> headerType)
                                                                                    throws IOException
        Description copied from interface: ResponseBuilder
        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.

        Specified by:
        buildEmptyWithHeaders in interface ResponseBuilder<T,​E extends RestException>
        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:
        IOException - exceptions from deserialization
      • exceptionType

        public Class<? extends RestException> exceptionType()
        Returns:
        the exception type to thrown in case of error.
      • isSuccessful

        public boolean isSuccessful​(int statusCode)
        Check if the returned status code will be considered a success for this builder.
        Parameters:
        statusCode - the status code to check
        Returns:
        true if it's a success, false otherwise.
      • withThrowOnGet404

        public ServiceResponseBuilder withThrowOnGet404​(boolean throwOnGet404)
        Specifies whether to throw on 404 responses from a GET call.
        Parameters:
        throwOnGet404 - true if to throw; false to simply return null. Default is false.
        Returns:
        the response builder itself