Package kong.unirest.core
Class BaseResponse<T>
- java.lang.Object
-
- kong.unirest.core.BaseResponse<T>
-
- All Implemented Interfaces:
HttpResponse<T>
- Direct Known Subclasses:
BasicResponse,ByteResponse,FileResponse,JsonResponse,MockResponse,ObjectResponse,StringResponse
abstract class BaseResponse<T> extends java.lang.Object implements HttpResponse<T>
-
-
Field Summary
Fields Modifier and Type Field Description private Configconfigprivate Cookiescookiesprivate Headersheadersprivate java.util.Optional<UnirestParsingException>parsingerrorprivate HttpRequestSummaryreqSummaryprivate intstatusCodeprivate java.lang.StringstatusText
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseResponse(BaseResponse other)protectedBaseResponse(RawResponse response)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract TgetBody()CookiesgetCookies()return a cookie collection parse from the set-cookie headerprivate java.lang.StringgetErrorBody()HeadersgetHeaders()java.util.Optional<UnirestParsingException>getParsingError()If the transformation to the body failed by an exception it will be kept hereprotected abstract java.lang.StringgetRawBody()HttpRequestSummarygetRequestSummary()intgetStatus()java.lang.StringgetStatusText()<E> HttpResponse<T>ifFailure(java.lang.Class<? extends E> errorClass, java.util.function.Consumer<HttpResponse<E>> consumer)If the response was NOT a 200-series response or a mapping exception happened.HttpResponse<T>ifFailure(java.util.function.Consumer<HttpResponse<T>> consumer)If the response was NOT a 200-series response or a mapping exception happened.HttpResponse<T>ifSuccess(java.util.function.Consumer<HttpResponse<T>> consumer)If the response was a 200-series response.booleanisSuccess()<V> HttpResponse<V>map(java.util.function.Function<T,V> func)Map the Response into another response with a different body<V> VmapBody(java.util.function.Function<T,V> func)Map the body into another type<E> EmapError(java.lang.Class<? extends E> errorClass)Map the body into a error class if the response was NOT a 200-series response or a mapping exception happened.protected voidsetParsingException(java.lang.String originalBody, java.lang.RuntimeException e)
-
-
-
Field Detail
-
headers
private final Headers headers
-
statusText
private final java.lang.String statusText
-
statusCode
private final int statusCode
-
reqSummary
private final HttpRequestSummary reqSummary
-
parsingerror
private java.util.Optional<UnirestParsingException> parsingerror
-
config
private final Config config
-
cookies
private Cookies cookies
-
-
Constructor Detail
-
BaseResponse
protected BaseResponse(RawResponse response)
-
BaseResponse
protected BaseResponse(BaseResponse other)
-
-
Method Detail
-
getStatus
public int getStatus()
- Specified by:
getStatusin interfaceHttpResponse<T>- Returns:
- the HTTP status code.
-
getStatusText
public java.lang.String getStatusText()
- Specified by:
getStatusTextin interfaceHttpResponse<T>- Returns:
- status text
-
getHeaders
public Headers getHeaders()
- Specified by:
getHeadersin interfaceHttpResponse<T>- Returns:
- Response Headers (map) with same case as server response.
For instance use
getHeaders().getFirst("Location")and notgetHeaders().getFirst("location")to get first header "Location"
-
getBody
public abstract T getBody()
- Specified by:
getBodyin interfaceHttpResponse<T>- Returns:
- the body
-
getParsingError
public java.util.Optional<UnirestParsingException> getParsingError()
Description copied from interface:HttpResponseIf the transformation to the body failed by an exception it will be kept here- Specified by:
getParsingErrorin interfaceHttpResponse<T>- Returns:
- a possible RuntimeException. Checked exceptions are wrapped in a UnirestException
-
mapBody
public <V> V mapBody(java.util.function.Function<T,V> func)
Description copied from interface:HttpResponseMap the body into another type- Specified by:
mapBodyin interfaceHttpResponse<T>- Type Parameters:
V- The return type of the function- Parameters:
func- a function to transform a body type to something else.- Returns:
- the return type
-
map
public <V> HttpResponse<V> map(java.util.function.Function<T,V> func)
Description copied from interface:HttpResponseMap the Response into another response with a different body- Specified by:
mapin interfaceHttpResponse<T>- Type Parameters:
V- The return type of the function- Parameters:
func- a function to transform a body type to something else.- Returns:
- the return type
-
setParsingException
protected void setParsingException(java.lang.String originalBody, java.lang.RuntimeException e)
-
isSuccess
public boolean isSuccess()
- Specified by:
isSuccessin interfaceHttpResponse<T>- Returns:
- true if the response was a 200-series response and no mapping exception happened, else false
-
ifSuccess
public HttpResponse<T> ifSuccess(java.util.function.Consumer<HttpResponse<T>> consumer)
Description copied from interface:HttpResponseIf the response was a 200-series response. Invoke this consumer can be chained with ifFailure- Specified by:
ifSuccessin interfaceHttpResponse<T>- Parameters:
consumer- a function to consume a HttpResponse- Returns:
- the same response
-
ifFailure
public HttpResponse<T> ifFailure(java.util.function.Consumer<HttpResponse<T>> consumer)
Description copied from interface:HttpResponseIf the response was NOT a 200-series response or a mapping exception happened. Invoke this consumer can be chained with ifSuccess- Specified by:
ifFailurein interfaceHttpResponse<T>- Parameters:
consumer- a function to consume a HttpResponse- Returns:
- the same response
-
mapError
public <E> E mapError(java.lang.Class<? extends E> errorClass)
Description copied from interface:HttpResponseMap the body into a error class if the response was NOT a 200-series response or a mapping exception happened. Uses the system Object Mapper- Specified by:
mapErrorin interfaceHttpResponse<T>- Type Parameters:
E- the response type- Parameters:
errorClass- the class for the error- Returns:
- the error object
-
getErrorBody
private java.lang.String getErrorBody()
-
ifFailure
public <E> HttpResponse<T> ifFailure(java.lang.Class<? extends E> errorClass, java.util.function.Consumer<HttpResponse<E>> consumer)
Description copied from interface:HttpResponseIf the response was NOT a 200-series response or a mapping exception happened. map the original body into a error type and invoke this consumer can be chained with ifSuccess- Specified by:
ifFailurein interfaceHttpResponse<T>- Type Parameters:
E- the type of error class to map the body- Parameters:
errorClass- the class of the error type to map toconsumer- a function to consume a HttpResponse- Returns:
- the same response
-
getCookies
public Cookies getCookies()
Description copied from interface:HttpResponsereturn a cookie collection parse from the set-cookie header- Specified by:
getCookiesin interfaceHttpResponse<T>- Returns:
- a Cookies collection
-
getRequestSummary
public HttpRequestSummary getRequestSummary()
- Specified by:
getRequestSummaryin interfaceHttpResponse<T>- Returns:
- a Summary of the HttpRequest that created this response
-
getRawBody
protected abstract java.lang.String getRawBody()
-
-