use of com.github.ljtfreitas.julian.http.client.HTTPClientResponse in project julian-http-client by ljtfreitas.
the class VertxHTTPClientResponse method valueOf.
static Single<VertxHTTPClientResponse> valueOf(HttpClientResponse response) {
HTTPStatus status = HTTPStatusCode.select(response.statusCode()).map(HTTPStatus::new).orElseGet(() -> new HTTPStatus(response.statusCode(), response.statusMessage()));
HTTPHeaders headers = response.headers().names().stream().map(name -> HTTPHeader.create(name, response.headers().getAll(name))).reduce(HTTPHeaders.empty(), HTTPHeaders::join, (a, b) -> b);
Maybe<byte[]> bodyAsBytes = response.body().map(Buffer::getBytes).toMaybe();
return bodyAsBytes.map(body -> HTTPResponseBody.optional(status, headers, () -> HTTPResponseBody.some(body))).map(body -> new VertxHTTPClientResponse(HTTPClientResponse.create(status, headers, body))).switchIfEmpty(Single.fromCallable(() -> new VertxHTTPClientResponse(HTTPClientResponse.empty(status, headers))));
}
use of com.github.ljtfreitas.julian.http.client.HTTPClientResponse in project julian-http-client by ljtfreitas.
the class ReactorNettyHTTPClientResponse method valueOf.
static Mono<ReactorNettyHTTPClientResponse> valueOf(HttpClientResponse response, ByteBufMono bodyAsBuffer) {
HTTPStatus status = HTTPStatusCode.select(response.status().code()).map(HTTPStatus::new).orElseGet(() -> new HTTPStatus(response.status().code(), response.status().reasonPhrase()));
HTTPHeaders headers = response.responseHeaders().names().stream().map(name -> HTTPHeader.create(name, response.responseHeaders().getAll(name))).reduce(HTTPHeaders.empty(), HTTPHeaders::join, (a, b) -> b);
return bodyAsBuffer.asByteArray().map(bodyAsBytes -> HTTPResponseBody.optional(status, headers, () -> HTTPResponseBody.some(bodyAsBytes))).map(body -> new ReactorNettyHTTPClientResponse(HTTPClientResponse.create(status, headers, body))).switchIfEmpty(Mono.fromCallable(() -> new ReactorNettyHTTPClientResponse(HTTPClientResponse.empty(status, headers))));
}
use of com.github.ljtfreitas.julian.http.client.HTTPClientResponse in project julian-http-client by ljtfreitas.
the class DefaultHTTPResponseFailureTest method shouldCreateTheFailureResponse.
@ParameterizedTest(name = "HTTP Status: {0}")
@MethodSource("failureHTTPStatuses")
void shouldCreateTheFailureResponse(HTTPStatus status, Class<? extends HTTPResponseException> exception) {
HTTPHeaders headers = HTTPHeaders.empty();
String responseBody = "response body";
HTTPClientResponse source = new HTTPClientResponse() {
@Override
public HTTPStatus status() {
return status;
}
@Override
public HTTPHeaders headers() {
return headers;
}
@Override
public HTTPResponseBody body() {
return new PublisherHTTPResponseBody(new SimplePublisher(responseBody));
}
@Override
public <T, R extends Response<T>> Optional<R> failure(Function<? super HTTPClientResponse, R> fn) {
return Optional.empty();
}
@Override
public <T, R extends Response<T>> Optional<R> success(Function<? super HTTPClientResponse, R> fn) {
return Optional.empty();
}
};
HTTPResponse<Object> failed = failure.apply(source, JavaType.valueOf(String.class));
assertAll(() -> assertNotNull(failed), () -> assertEquals(status, failed.status()), () -> assertEquals(headers, failed.headers()));
HTTPResponseException httpResponseException = assertThrows(exception, failed.body()::unsafe);
assertAll(() -> assertEquals(status, httpResponseException.status()), () -> assertEquals(headers, httpResponseException.headers()), () -> assertEquals(responseBody, httpResponseException.bodyAsString()));
}
use of com.github.ljtfreitas.julian.http.client.HTTPClientResponse in project julian-http-client by ljtfreitas.
the class DefaultHTTPRequestIO method deserialize.
private Optional<CompletableFuture<T>> deserialize(HTTPClientResponse response) {
MediaType mediaType = response.headers().select(CONTENT_TYPE).map(h -> MediaType.valueOf(h.value())).orElseGet(MediaType::wildcard);
HTTPResponseReader<?> reader = codecs.readers().select(mediaType, source.returnType()).orElseThrow(() -> new HTTPResponseReaderException(format("There is no a HTTPResponseReader able to convert {0} to {1}", mediaType, source.returnType())));
return reader.read(response.body(), source.returnType()).map(c -> c.handleAsync(this::handle).toCompletableFuture());
}
Aggregations