Search in sources :

Example 11 with Response

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

the class ObservableResponseTTest method bind.

@Test
void bind() {
    Promise<Response<Collection<String>>> response = new SinglePromise<>(Single.just(Response.done(List.of("one", "two", "three"))));
    ResponseFn<Collection<String>, Collection<Object>> fn = new CollectionResponseT().bind(endpoint, new ObjectResponseT<Collection<Object>>().bind(endpoint, null));
    when(endpoint.returnType()).thenReturn(JavaType.parameterized(Collection.class, String.class));
    Observable<Object> observable = subject.bind(endpoint, fn).join(response, Arguments.empty());
    TestObserver<Object> observer = new TestObserver<>();
    observable.subscribe(observer);
    observer.assertComplete().assertNoErrors().assertValues("one", "two", "three");
}
Also used : Response(com.github.ljtfreitas.julian.Response) CollectionResponseT(com.github.ljtfreitas.julian.CollectionResponseT) Collection(java.util.Collection) ObjectResponseT(com.github.ljtfreitas.julian.ObjectResponseT) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.jupiter.api.Test)

Example 12 with Response

use of com.github.ljtfreitas.julian.Response 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 13 with Response

use of com.github.ljtfreitas.julian.Response 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 14 with Response

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

the class MultiResponseTTest method bind.

@Test
void bind() {
    Promise<Response<Collection<String>>> response = Promise.done(Response.done(List.of("one", "two", "three")));
    when(endpoint.returnType()).thenReturn(JavaType.parameterized(Collection.class, String.class));
    ResponseFn<Collection<String>, Collection<Object>> fn = new CollectionResponseT().bind(endpoint, new ObjectResponseT<Collection<Object>>().bind(endpoint, null));
    Multi<Object> multi = subject.bind(endpoint, fn).join(response, Arguments.empty());
    AssertSubscriber<Object> subscriber = multi.subscribe().withSubscriber(AssertSubscriber.create(3));
    subscriber.assertCompleted().assertItems("one", "two", "three");
}
Also used : Response(com.github.ljtfreitas.julian.Response) CollectionResponseT(com.github.ljtfreitas.julian.CollectionResponseT) Collection(java.util.Collection) ObjectResponseT(com.github.ljtfreitas.julian.ObjectResponseT) Test(org.junit.jupiter.api.Test)

Example 15 with Response

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

the class CompletableResponseTTest method bind.

@Test
void bind() {
    Promise<Response<Void>> response = new SinglePromise<>(Single.just(Response.done(null)));
    ResponseFn<Void, Response<Object>> fn = new DefaultResponseT().bind(endpoint, new ObjectResponseT<>().bind(endpoint, null));
    Completable completable = subject.bind(endpoint, fn).join(response, Arguments.empty());
    TestObserver<Void> observer = new TestObserver<>();
    completable.subscribe(observer);
    observer.assertComplete().assertNoErrors();
}
Also used : Response(com.github.ljtfreitas.julian.Response) Completable(io.reactivex.rxjava3.core.Completable) DefaultResponseT(com.github.ljtfreitas.julian.DefaultResponseT) ObjectResponseT(com.github.ljtfreitas.julian.ObjectResponseT) TestObserver(io.reactivex.rxjava3.observers.TestObserver) Test(org.junit.jupiter.api.Test)

Aggregations

Response (com.github.ljtfreitas.julian.Response)29 Test (org.junit.jupiter.api.Test)25 ObjectResponseT (com.github.ljtfreitas.julian.ObjectResponseT)24 CollectionResponseT (com.github.ljtfreitas.julian.CollectionResponseT)22 Collection (java.util.Collection)22 Function (java.util.function.Function)4 HTTPHeader (com.github.ljtfreitas.julian.http.HTTPHeader)3 HTTPHeaders (com.github.ljtfreitas.julian.http.HTTPHeaders)3 HTTPResponseBody (com.github.ljtfreitas.julian.http.HTTPResponseBody)3 HTTPStatus (com.github.ljtfreitas.julian.http.HTTPStatus)3 HTTPStatusCode (com.github.ljtfreitas.julian.http.HTTPStatusCode)3 HTTPClientResponse (com.github.ljtfreitas.julian.http.client.HTTPClientResponse)3 Optional (java.util.Optional)3 DefaultResponseT (com.github.ljtfreitas.julian.DefaultResponseT)2 TestObserver (io.reactivex.rxjava3.observers.TestObserver)2 Arguments (com.github.ljtfreitas.julian.Arguments)1 Completable (io.reactivex.rxjava3.core.Completable)1 Maybe (io.reactivex.rxjava3.core.Maybe)1 Single (io.reactivex.rxjava3.core.Single)1 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)1