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);
}
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);
}
}
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))));
}
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();
}
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))));
}
Aggregations