Search in sources :

Example 1 with HTTPClientResponse

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))));
}
Also used : Single(io.reactivex.rxjava3.core.Single) Function(java.util.function.Function) HTTPResponseBody(com.github.ljtfreitas.julian.http.HTTPResponseBody) HTTPStatusCode(com.github.ljtfreitas.julian.http.HTTPStatusCode) Maybe(io.reactivex.rxjava3.core.Maybe) Response(com.github.ljtfreitas.julian.Response) HTTPHeader(com.github.ljtfreitas.julian.http.HTTPHeader) HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPStatus(com.github.ljtfreitas.julian.http.HTTPStatus) Optional(java.util.Optional) Buffer(io.vertx.rxjava3.core.buffer.Buffer) HTTPClientResponse(com.github.ljtfreitas.julian.http.client.HTTPClientResponse) HttpClientResponse(io.vertx.rxjava3.core.http.HttpClientResponse) HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPStatus(com.github.ljtfreitas.julian.http.HTTPStatus)

Example 2 with HTTPClientResponse

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))));
}
Also used : HTTPStatusCode(com.github.ljtfreitas.julian.http.HTTPStatusCode) Response(com.github.ljtfreitas.julian.Response) HttpClientResponse(reactor.netty.http.client.HttpClientResponse) HTTPHeader(com.github.ljtfreitas.julian.http.HTTPHeader) HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPStatus(com.github.ljtfreitas.julian.http.HTTPStatus) Optional(java.util.Optional) Mono(reactor.core.publisher.Mono) ByteBufMono(reactor.netty.ByteBufMono) HTTPClientResponse(com.github.ljtfreitas.julian.http.client.HTTPClientResponse) Function(java.util.function.Function) HTTPResponseBody(com.github.ljtfreitas.julian.http.HTTPResponseBody) HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPStatus(com.github.ljtfreitas.julian.http.HTTPStatus)

Example 3 with HTTPClientResponse

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()));
}
Also used : Response(com.github.ljtfreitas.julian.Response) HTTPClientResponse(com.github.ljtfreitas.julian.http.client.HTTPClientResponse) Function(java.util.function.Function) HTTPClientResponse(com.github.ljtfreitas.julian.http.client.HTTPClientResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with HTTPClientResponse

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());
}
Also used : HTTPClient(com.github.ljtfreitas.julian.http.client.HTTPClient) HTTPMessageException(com.github.ljtfreitas.julian.http.codec.HTTPMessageException) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Message.format(com.github.ljtfreitas.julian.Message.format) HTTPResponseReaderException(com.github.ljtfreitas.julian.http.codec.HTTPResponseReaderException) HTTPResponseReader(com.github.ljtfreitas.julian.http.codec.HTTPResponseReader) ExecutionException(java.util.concurrent.ExecutionException) CONTENT_TYPE(com.github.ljtfreitas.julian.http.HTTPHeader.CONTENT_TYPE) HTTPMessageCodecs(com.github.ljtfreitas.julian.http.codec.HTTPMessageCodecs) HTTPClientException(com.github.ljtfreitas.julian.http.client.HTTPClientException) Promise(com.github.ljtfreitas.julian.Promise) Optional(java.util.Optional) HTTPClientResponse(com.github.ljtfreitas.julian.http.client.HTTPClientResponse) HTTPResponseReaderException(com.github.ljtfreitas.julian.http.codec.HTTPResponseReaderException)

Aggregations

HTTPClientResponse (com.github.ljtfreitas.julian.http.client.HTTPClientResponse)4 Response (com.github.ljtfreitas.julian.Response)3 Optional (java.util.Optional)3 Function (java.util.function.Function)3 HTTPHeader (com.github.ljtfreitas.julian.http.HTTPHeader)2 HTTPHeaders (com.github.ljtfreitas.julian.http.HTTPHeaders)2 HTTPResponseBody (com.github.ljtfreitas.julian.http.HTTPResponseBody)2 HTTPStatus (com.github.ljtfreitas.julian.http.HTTPStatus)2 HTTPStatusCode (com.github.ljtfreitas.julian.http.HTTPStatusCode)2 Message.format (com.github.ljtfreitas.julian.Message.format)1 Promise (com.github.ljtfreitas.julian.Promise)1 CONTENT_TYPE (com.github.ljtfreitas.julian.http.HTTPHeader.CONTENT_TYPE)1 HTTPClient (com.github.ljtfreitas.julian.http.client.HTTPClient)1 HTTPClientException (com.github.ljtfreitas.julian.http.client.HTTPClientException)1 HTTPMessageCodecs (com.github.ljtfreitas.julian.http.codec.HTTPMessageCodecs)1 HTTPMessageException (com.github.ljtfreitas.julian.http.codec.HTTPMessageException)1 HTTPResponseReader (com.github.ljtfreitas.julian.http.codec.HTTPResponseReader)1 HTTPResponseReaderException (com.github.ljtfreitas.julian.http.codec.HTTPResponseReaderException)1 Maybe (io.reactivex.rxjava3.core.Maybe)1 Single (io.reactivex.rxjava3.core.Single)1