Class ServiceResponseBuilder<T,E extends RestException>
- java.lang.Object
-
- com.microsoft.rest.ServiceResponseBuilder<T,E>
-
- 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 aServiceResponse.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classServiceResponseBuilder.FactoryA factory to create a service response builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ServiceResponse<T>build(retrofit2.Response<okhttp3.ResponseBody> response)Build a ServiceResponse instance from a REST call response and a possible error.ServiceResponse<T>buildEmpty(retrofit2.Response<Void> response)Build a ServiceResponse instance from a REST call response and a possible error, which does not have a response body.<THeader> ServiceResponseWithHeaders<T,THeader>buildEmptyWithHeaders(retrofit2.Response<Void> response, Class<THeader> headerType)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.<THeader> ServiceResponseWithHeaders<T,THeader>buildWithHeaders(retrofit2.Response<okhttp3.ResponseBody> response, Class<THeader> headerType)Build a ServiceResponseWithHeaders instance from a REST call response, a header in JSON format, and a possible error.Class<? extends RestException>exceptionType()booleanisSuccessful(int statusCode)Check if the returned status code will be considered a success for this builder.ServiceResponseBuilder<T,E>register(int statusCode, Type type)Register a mapping from a response status code to a response destination type.ServiceResponseBuilder<T,E>registerAll(Map<Integer,Type> responseTypes)Register all the mappings from a response status code to a response destination type stored in aMap.ServiceResponseBuilder<T,E>registerError(Class<? extends RestException> type)Register a destination type for errors with models.ServiceResponseBuilderwithThrowOnGet404(boolean throwOnGet404)Specifies whether to throw on 404 responses from a GET call.
-
-
-
Method Detail
-
register
public ServiceResponseBuilder<T,E> register(int statusCode, Type type)
Description copied from interface:ResponseBuilderRegister a mapping from a response status code to a response destination type.- Specified by:
registerin interfaceResponseBuilder<T,E extends RestException>- Parameters:
statusCode- the status code.type- the type to deserialize.- Returns:
- the same builder instance.
-
registerError
public ServiceResponseBuilder<T,E> registerError(Class<? extends RestException> type)
Description copied from interface:ResponseBuilderRegister a destination type for errors with models.- Specified by:
registerErrorin interfaceResponseBuilder<T,E extends RestException>- Parameters:
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 aMap.- 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:ResponseBuilderBuild 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:
buildin interfaceResponseBuilder<T,E extends RestException>- Parameters:
response- theResponseinstance 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:ResponseBuilderBuild 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:
buildEmptyin interfaceResponseBuilder<T,E extends RestException>- Parameters:
response- theResponseinstance 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:ResponseBuilderBuild 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:
buildWithHeadersin interfaceResponseBuilder<T,E extends RestException>- Type Parameters:
THeader- the type of the header- Parameters:
response- theResponseinstance from REST callheaderType- 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:ResponseBuilderBuild 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:
buildEmptyWithHeadersin interfaceResponseBuilder<T,E extends RestException>- Type Parameters:
THeader- the type of the header- Parameters:
response- theResponseinstance from REST callheaderType- 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
-
-