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