T
- the Java class of the originals SphereRequest
s result.public final class JavaAndHttpResponseSphereRequest<T> extends Base implements SphereRequest<org.apache.commons.lang3.tuple.Pair<T,HttpResponse>>
SphereRequest
to receive the Java object as well as the HttpResponse
representation.
withCategory(client(), category -> { final CategoryByIdGet originalRequest = CategoryByIdGet.of(category); final JavaAndHttpResponseSphereRequest<Category> request = JavaAndHttpResponseSphereRequest.of(originalRequest); final Pair<Category, HttpResponse> result = client().executeBlocking(request); final Category categoryJavaObject = result.getLeft(); final HttpResponse httpResponse = result.getRight(); assertThat(categoryJavaObject).isEqualTo(category); assertThat(httpResponse.getHeaders().findFlatHeader(HttpHeaders.CONTENT_TYPE).get()) .contains("application/json"); });
See the test code.
Modifier and Type | Method and Description |
---|---|
boolean |
canDeserialize(HttpResponse httpResponse)
Checks if the response can be handled by
SphereRequest.deserialize(HttpResponse) . |
org.apache.commons.lang3.tuple.Pair<T,HttpResponse> |
deserialize(HttpResponse httpResponse)
Takes an http response and maps it into a Java object of type T.
|
HttpRequestIntent |
httpRequestIntent()
Provides an http request intent, this does not include the execution of it.
|
static <T> JavaAndHttpResponseSphereRequest<T> |
of(SphereRequest<T> delegate) |
public static <T> JavaAndHttpResponseSphereRequest<T> of(SphereRequest<T> delegate)
@Nullable public org.apache.commons.lang3.tuple.Pair<T,HttpResponse> deserialize(HttpResponse httpResponse)
SphereRequest
SphereRequest.canDeserialize(HttpResponse)
if the response can be consumed.deserialize
in interface SphereRequest<org.apache.commons.lang3.tuple.Pair<T,HttpResponse>>
httpResponse
- the http response of the APIpublic HttpRequestIntent httpRequestIntent()
SphereRequest
httpRequestIntent
in interface SphereRequest<org.apache.commons.lang3.tuple.Pair<T,HttpResponse>>
public boolean canDeserialize(HttpResponse httpResponse)
SphereRequest
SphereRequest.deserialize(HttpResponse)
.
Use case 1: A http response returns 404 and the this SphereRequest
can handle this error by returning an empty optional, an empty list or throwing a domain specific exception.canDeserialize
in interface SphereRequest<org.apache.commons.lang3.tuple.Pair<T,HttpResponse>>
httpResponse
- the http response which shall be transformed