Search in sources :

Example 1 with HTTPHeaders

use of com.github.ljtfreitas.julian.http.HTTPHeaders in project julian-http-client by ljtfreitas.

the class DefaultHTTPClientResponse method valueOf.

static DefaultHTTPClientResponse valueOf(HttpResponse<Publisher<List<ByteBuffer>>> response) {
    HTTPStatus status = HTTPStatusCode.select(response.statusCode()).map(HTTPStatus::new).orElseGet(() -> HTTPStatus.valueOf(response.statusCode()));
    HTTPHeaders headers = response.headers().map().entrySet().stream().map(e -> HTTPHeader.create(e.getKey(), e.getValue())).reduce(HTTPHeaders.empty(), HTTPHeaders::join, (a, b) -> b);
    HTTPResponseBody body = HTTPResponseBody.optional(status, headers, () -> HTTPResponseBody.lazy(response.body()));
    return new DefaultHTTPClientResponse(status, headers, body);
}
Also used : HTTPStatusCode(com.github.ljtfreitas.julian.http.HTTPStatusCode) Response(com.github.ljtfreitas.julian.Response) List(java.util.List) Publisher(java.util.concurrent.Flow.Publisher) 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) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) HTTPResponseBody(com.github.ljtfreitas.julian.http.HTTPResponseBody) HttpResponse(java.net.http.HttpResponse) HTTPResponseBody(com.github.ljtfreitas.julian.http.HTTPResponseBody) HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPStatus(com.github.ljtfreitas.julian.http.HTTPStatus)

Example 2 with HTTPHeaders

use of com.github.ljtfreitas.julian.http.HTTPHeaders in project julian-http-client by ljtfreitas.

the class WebClientHTTPRequest method read.

Mono<HTTPResponse<T>> read(ClientResponse response) {
    HTTPStatus status = new HTTPStatus(response.rawStatusCode(), response.statusCode().getReasonPhrase());
    HTTPHeaders headers = response.headers().asHttpHeaders().entrySet().stream().reduce(HTTPHeaders.empty(), (h, e) -> h.join(new HTTPHeader(e.getKey(), e.getValue())), (a, b) -> b);
    if (response.statusCode().isError()) {
        return response.createException().map(e -> failure(status, headers, e));
    } else {
        return success(status, headers, response);
    }
}
Also used : HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPStatus(com.github.ljtfreitas.julian.http.HTTPStatus) HTTPHeader(com.github.ljtfreitas.julian.http.HTTPHeader)

Example 3 with HTTPHeaders

use of com.github.ljtfreitas.julian.http.HTTPHeaders 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 4 with HTTPHeaders

use of com.github.ljtfreitas.julian.http.HTTPHeaders in project julian-http-client by ljtfreitas.

the class DebugHTTPClientTest method show.

@Test
void show(@Mock HTTPRequest<String> httpRequest) {
    mockServer.when(request("/debug").withMethod("POST").withBody("request body")).respond(response().withStatusCode(200).withContentType(TEXT_PLAIN).withHeader("x-some-header", "some-content").withBody("it works!"));
    DebugHTTPClient debugHTTPClient = new DebugHTTPClient(new DefaultHTTPClient());
    when(httpRequest.path()).thenReturn(URI.create("http://localhost:8090/debug"));
    when(httpRequest.method()).thenReturn(HTTPMethod.POST);
    when(httpRequest.headers()).thenReturn(new HTTPHeaders(List.of(new HTTPHeader("X-Some-Header", "some-content"), new HTTPHeader("Accept", "text/plain"))));
    when(httpRequest.body()).thenReturn(Optional.of(new DefaultHTTPRequestBody(MediaType.TEXT_PLAIN, () -> BodyPublishers.ofString("request body"))));
    HTTPClientRequest intercepted = debugHTTPClient.request(httpRequest);
    intercepted.execute().join().unsafe();
    verify(httpRequest, atLeastOnce()).path();
    verify(httpRequest, atLeastOnce()).headers();
    verify(httpRequest, atLeastOnce()).method();
}
Also used : HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) DefaultHTTPRequestBody(com.github.ljtfreitas.julian.http.DefaultHTTPRequestBody) HTTPHeader(com.github.ljtfreitas.julian.http.HTTPHeader) Test(org.junit.jupiter.api.Test)

Example 5 with HTTPHeaders

use of com.github.ljtfreitas.julian.http.HTTPHeaders 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)

Aggregations

HTTPHeader (com.github.ljtfreitas.julian.http.HTTPHeader)5 HTTPHeaders (com.github.ljtfreitas.julian.http.HTTPHeaders)5 HTTPStatus (com.github.ljtfreitas.julian.http.HTTPStatus)4 Response (com.github.ljtfreitas.julian.Response)3 HTTPResponseBody (com.github.ljtfreitas.julian.http.HTTPResponseBody)3 HTTPStatusCode (com.github.ljtfreitas.julian.http.HTTPStatusCode)3 Optional (java.util.Optional)3 Function (java.util.function.Function)3 HTTPClientResponse (com.github.ljtfreitas.julian.http.client.HTTPClientResponse)2 DefaultHTTPRequestBody (com.github.ljtfreitas.julian.http.DefaultHTTPRequestBody)1 Maybe (io.reactivex.rxjava3.core.Maybe)1 Single (io.reactivex.rxjava3.core.Single)1 Buffer (io.vertx.rxjava3.core.buffer.Buffer)1 HttpClientResponse (io.vertx.rxjava3.core.http.HttpClientResponse)1 HttpResponse (java.net.http.HttpResponse)1 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 Publisher (java.util.concurrent.Flow.Publisher)1 Test (org.junit.jupiter.api.Test)1 Mono (reactor.core.publisher.Mono)1