Search in sources :

Example 1 with Response

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

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

the class SetResponseTTest method bindNullCollection.

@Test
void bindNullCollection() {
    when(endpoint.returnType()).thenReturn(JavaType.parameterized(Collection.class, String.class));
    Promise<Response<Collection<String>>> promise = Promise.done(Response.done(null));
    ResponseFn<Collection<String>, Collection<Object>> fn = new CollectionResponseT().bind(endpoint, new ObjectResponseT<Collection<Object>>().bind(endpoint, null));
    Set<Object> set = responseT.bind(endpoint, fn).join(promise, Arguments.empty());
    assertTrue(set.isEmpty());
}
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 3 with Response

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

the class TraversableResponseTTest method bind.

@Test
void bind() {
    when(endpoint.returnType()).thenReturn(JavaType.parameterized(Collection.class, String.class));
    Collection<String> values = java.util.List.of("one", "two", "three");
    Promise<Response<Collection<String>>> promise = Promise.done(Response.done(values));
    ResponseFn<Collection<String>, Collection<Object>> fn = new CollectionResponseT().bind(endpoint, new ObjectResponseT<Collection<Object>>().bind(endpoint, null));
    Traversable<Object> traversable = responseT.bind(endpoint, fn).join(promise, Arguments.empty());
    assertTrue(traversable.containsAll(values));
}
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 4 with Response

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

the class TryResponseTTest method bindFailure.

@Test
void bindFailure() {
    RuntimeException failure = new RuntimeException("oops");
    ResponseFn<String, Object> fn = new ObjectResponseT<>().bind(endpoint, null);
    Promise<Response<String>> promise = Promise.failed(failure);
    Arguments arguments = Arguments.empty();
    Try<Object> attempt = responseT.bind(endpoint, fn).join(promise, arguments);
    assertTrue(attempt.isFailure());
    Exception exception = (Exception) attempt.getCause();
    assertSame(failure, exception);
}
Also used : Response(com.github.ljtfreitas.julian.Response) Arguments(com.github.ljtfreitas.julian.Arguments) Test(org.junit.jupiter.api.Test)

Example 5 with Response

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

the class VectorResponseTTest method bindNullCollection.

@Test
void bindNullCollection() {
    when(endpoint.returnType()).thenReturn(JavaType.parameterized(Collection.class, String.class));
    Promise<Response<Collection<String>>> promise = Promise.done(Response.done(null));
    ResponseFn<Collection<String>, Collection<Object>> fn = new CollectionResponseT().bind(endpoint, new ObjectResponseT<Collection<Object>>().bind(endpoint, null));
    Vector<Object> vector = responseT.bind(endpoint, fn).join(promise, Arguments.empty());
    assertTrue(vector.isEmpty());
}
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)

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