Search in sources :

Example 1 with ResponsePredicate

use of io.vertx.ext.web.client.predicate.ResponsePredicate in project vertx-web by vert-x3.

the class WebClientExamples method predicateCustomErrorWithBody.

public void predicateCustomErrorWithBody() {
    ErrorConverter converter = ErrorConverter.createFullBody(result -> {
        // Invoked after the response body is fully received
        HttpResponse<Buffer> response = result.response();
        if (response.getHeader("content-type").equals("application/json")) {
            // Error body is JSON data
            JsonObject body = response.bodyAsJsonObject();
            return new MyCustomException(body.getString("code"), body.getString("message"));
        }
        // Fallback to defaut message
        return new MyCustomException(result.message());
    });
    ResponsePredicate predicate = ResponsePredicate.create(ResponsePredicate.SC_SUCCESS, converter);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) JsonObject(io.vertx.core.json.JsonObject) ErrorConverter(io.vertx.ext.web.client.predicate.ErrorConverter)

Example 2 with ResponsePredicate

use of io.vertx.ext.web.client.predicate.ResponsePredicate in project vertx-web by vert-x3.

the class WebClientTest method testErrorConverterReturnsNull.

@Test
public void testErrorConverterReturnsNull() throws Exception {
    ResponsePredicate predicate = ResponsePredicate.create(r -> {
        return ResponsePredicateResult.failure("boom");
    }, r -> null);
    testExpectation(true, req -> req.expect(predicate), HttpServerResponse::end, ar -> {
        assertThat(ar.cause(), not(instanceOf(NullPointerException.class)));
    });
}
Also used : ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) Test(org.junit.Test)

Example 3 with ResponsePredicate

use of io.vertx.ext.web.client.predicate.ResponsePredicate in project vertx-web by vert-x3.

the class WebClientTest method testErrorConverterThrowsException.

@Test
public void testErrorConverterThrowsException() throws Exception {
    ResponsePredicate predicate = ResponsePredicate.create(r -> {
        return ResponsePredicateResult.failure("boom");
    }, result -> {
        throw new IndexOutOfBoundsException();
    });
    testExpectation(true, req -> req.expect(predicate), HttpServerResponse::end, ar -> {
        assertThat(ar.cause(), instanceOf(IndexOutOfBoundsException.class));
    });
}
Also used : ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) Test(org.junit.Test)

Example 4 with ResponsePredicate

use of io.vertx.ext.web.client.predicate.ResponsePredicate in project vertx-web by vert-x3.

the class PredicateInterceptor method handle.

@Override
public void handle(HttpContext<?> httpContext) {
    if (httpContext.phase() == ClientPhase.RECEIVE_RESPONSE) {
        // Run expectations
        HttpRequestImpl request = (HttpRequestImpl) httpContext.request();
        HttpClientResponse resp = httpContext.clientResponse();
        List<ResponsePredicate> expectations = request.expectations;
        if (expectations != null) {
            for (ResponsePredicate expectation : expectations) {
                ResponsePredicateResultImpl predicateResult;
                try {
                    predicateResult = (ResponsePredicateResultImpl) expectation.apply(responseCopy(resp, httpContext, null));
                } catch (Exception e) {
                    httpContext.fail(e);
                    return;
                }
                if (!predicateResult.succeeded()) {
                    ErrorConverter errorConverter = expectation.errorConverter();
                    if (!errorConverter.requiresBody()) {
                        predicateResult.setHttpResponse(responseCopy(resp, httpContext, null));
                        failOnPredicate(httpContext, errorConverter, predicateResult);
                    } else {
                        resp.bodyHandler(buffer -> {
                            predicateResult.setHttpResponse(responseCopy(resp, httpContext, buffer));
                            failOnPredicate(httpContext, errorConverter, predicateResult);
                        });
                        resp.resume();
                    }
                    return;
                }
            }
        }
    }
    httpContext.next();
}
Also used : ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) HttpClientResponse(io.vertx.core.http.HttpClientResponse) HttpRequestImpl(io.vertx.ext.web.client.impl.HttpRequestImpl) ErrorConverter(io.vertx.ext.web.client.predicate.ErrorConverter)

Example 5 with ResponsePredicate

use of io.vertx.ext.web.client.predicate.ResponsePredicate in project hono by eclipse.

the class DeviceRegistryHttpClient method searchTenants.

/**
 * Finds tenants with optional filters, paging and sorting options.
 *
 * @param pageSize The maximum number of results to include in a response.
 * @param pageOffset The offset into the result set from which to include objects in the response.
 * @param filters The filters are predicates that objects in the result set must match.
 * @param sortOptions A list of sort options.
 * @return A future indicating the outcome of the operation. The future will contain the response if the
 *         response contained the expected status code. Otherwise the future will fail.
 * @throws NullPointerException if any of the parameters is {@code null}.
 */
public Future<HttpResponse<Buffer>> searchTenants(final Optional<Integer> pageSize, final Optional<Integer> pageOffset, final List<String> filters, final List<String> sortOptions) {
    Objects.requireNonNull(pageSize);
    Objects.requireNonNull(pageOffset);
    Objects.requireNonNull(filters);
    Objects.requireNonNull(sortOptions);
    final MultiMap queryParams = MultiMap.caseInsensitiveMultiMap();
    pageSize.ifPresent(pSize -> queryParams.add(RegistryManagementConstants.PARAM_PAGE_SIZE, String.valueOf(pSize)));
    pageOffset.ifPresent(pOffset -> queryParams.add(RegistryManagementConstants.PARAM_PAGE_OFFSET, String.valueOf(pOffset)));
    filters.forEach(filterJson -> queryParams.add(RegistryManagementConstants.PARAM_FILTER_JSON, filterJson));
    sortOptions.forEach(sortJson -> queryParams.add(RegistryManagementConstants.PARAM_SORT_JSON, sortJson));
    return httpClient.get(searchTenantsUri(), getRequestHeaders(), queryParams, (ResponsePredicate[]) null);
}
Also used : MultiMap(io.vertx.core.MultiMap) ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate)

Aggregations

ResponsePredicate (io.vertx.ext.web.client.predicate.ResponsePredicate)9 Test (org.junit.Test)6 ErrorConverter (io.vertx.ext.web.client.predicate.ErrorConverter)4 Buffer (io.vertx.core.buffer.Buffer)3 JsonObject (io.vertx.core.json.JsonObject)3 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)2 io.vertx.core (io.vertx.core)2 AsyncFile (io.vertx.core.file.AsyncFile)2 OpenOptions (io.vertx.core.file.OpenOptions)2 io.vertx.core.http (io.vertx.core.http)2 DecodeException (io.vertx.core.json.DecodeException)2 JsonArray (io.vertx.core.json.JsonArray)2 NetServer (io.vertx.core.net.NetServer)2 ProxyOptions (io.vertx.core.net.ProxyOptions)2 ProxyType (io.vertx.core.net.ProxyType)2 SocketAddress (io.vertx.core.net.SocketAddress)2 ReadStream (io.vertx.core.streams.ReadStream)2 WriteStream (io.vertx.core.streams.WriteStream)2 TokenCredentials (io.vertx.ext.auth.authentication.TokenCredentials)2 UsernamePasswordCredentials (io.vertx.ext.auth.authentication.UsernamePasswordCredentials)2