/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link Publisher}. * * @param request The {@link HttpRequest} to execute * @param <I> The request body type * @return A {@link Publisher} that emits the full {@link HttpResponse} object */ default <I> Publisher<HttpResponse<ByteBuffer>> exchange(HttpRequest<I> request) { return exchange(request, ByteBuffer.class); }
/** * <p>Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link Publisher} and converting the response body to the specified type.</p> * <p> * <p>This method will send a {@code Content-Length} header and except a content length header the response and is * designed for simple non-streaming exchanges of data</p> * <p> * <p>By default the exchange {@code Content-Type} is application/json, unless otherwise specified in the passed * {@link HttpRequest}</p> * * @param request The {@link HttpRequest} to execute * @param bodyType The body type * @param <I> The request body type * @param <O> The response body type * @return A {@link Publisher} that emits the full {@link HttpResponse} object */ default <I, O> Publisher<HttpResponse<O>> exchange(HttpRequest<I> request, Argument<O> bodyType) { return exchange(request, bodyType, DEFAULT_ERROR_TYPE); }
/** * Perform an HTTP GET request for the given request object emitting the full HTTP response from returned * {@link Publisher}. * * @param uri The Uri * @return A {@link Publisher} that emits the full {@link HttpResponse} object */ default Publisher<HttpResponse<ByteBuffer>> exchange(String uri) { return exchange(HttpRequest.GET(uri), ByteBuffer.class); }
@Override default <O> Flowable<HttpResponse<O>> exchange(String uri, Class<O> bodyType) { return Flowable.fromPublisher(HttpClient.super.exchange(uri, bodyType)); }
@Override default Flowable<HttpResponse<ByteBuffer>> exchange(String uri) { return Flowable.fromPublisher(HttpClient.super.exchange(uri)); }
@Override default <I, O> Flowable<HttpResponse<O>> exchange(HttpRequest<I> request, Class<O> bodyType) { return Flowable.fromPublisher(HttpClient.super.exchange(request, bodyType)); }
@Override default <I> Flowable<HttpResponse<ByteBuffer>> exchange(HttpRequest<I> request) { return Flowable.fromPublisher(HttpClient.super.exchange(request)); }
/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link Publisher} and converting the response body to the specified type. * * @param request The {@link HttpRequest} to execute * @param bodyType The body type * @param <I> The request body type * @param <O> The response body type * @return A {@link Publisher} that emits the full {@link HttpResponse} object */ default <I, O> Publisher<HttpResponse<O>> exchange(HttpRequest<I> request, Class<O> bodyType) { return exchange(request, Argument.of(bodyType)); }
@Override <I, O, E> Flowable<HttpResponse<O>> exchange(HttpRequest<I> request, Argument<O> bodyType, Argument<E> errorType);
/** * Perform an HTTP GET request for the given request object emitting the full HTTP response from returned * {@link Publisher}. * * @param uri The request URI * @param bodyType The body type * @param <O> The response body type * @return A {@link Publisher} that emits the full {@link HttpResponse} object */ default <O> Publisher<HttpResponse<O>> exchange(String uri, Class<O> bodyType) { return exchange(HttpRequest.GET(uri), Argument.of(bodyType)); }
/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link Publisher} and converting the response body to the specified type. * * @param request The {@link HttpRequest} to execute * @param bodyType The body type * @param errorType The error type * @param <I> The request body type * @param <O> The response body type * @param <E> The error type * @return A {@link Publisher} that emits a result of the given type */ default <I, O, E> Publisher<O> retrieve(HttpRequest<I> request, Argument<O> bodyType, Argument<E> errorType) { return Publishers.map(exchange(request, bodyType, errorType), response -> { if (bodyType.getType() == HttpStatus.class) { return (O) response.getStatus(); } else { Optional<O> body = response.getBody(); return body .orElseThrow(() -> new HttpClientResponseException( "Empty body", response )); } }); }
@Test public void testGetPojoList() { ApplicationContext applicationContext = ApplicationContext.run(); EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start(); HttpClient client = applicationContext.createBean(HttpClient.class, server.getURL()); Flowable<HttpResponse<List>> flowable = Flowable.fromPublisher(client.exchange( HttpRequest.GET("/get/pojoList"), Argument.of(List.class, HttpGetSpec.Book.class) )); HttpResponse<List> response = flowable.blockingFirst(); assertEquals(response.getStatus(), HttpStatus.OK); Optional<List> body = response.getBody(); assertTrue(body.isPresent()); List<HttpGetSpec.Book> list = body.get(); assertEquals(list.size(), 1); assertTrue(list.get(0) instanceof HttpGetSpec.Book); client.stop(); applicationContext.stop(); } }
@Test public void testSimpleGet() { ApplicationContext applicationContext = ApplicationContext.run(); EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start(); HttpClient client = new DefaultHttpClient(server.getURL()); Flowable<HttpResponse<String>> flowable = Flowable.fromPublisher(client.exchange( HttpRequest.GET("/get/simple"), String.class )); HttpResponse<String> response = flowable.blockingFirst(); Assert.assertEquals(response.getStatus(), HttpStatus.OK); Optional<String> body = response.getBody(String.class); assertTrue(body.isPresent()); assertEquals(body.get(), "success"); client.stop(); applicationContext.stop(); }
/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link Publisher}. * * @param request The {@link HttpRequest} to execute * @param <I> The request body type * @return A {@link Publisher} that emits the full {@link HttpResponse} object */ default <I> Publisher<HttpResponse<ByteBuffer>> exchange(HttpRequest<I> request) { return exchange(request, ByteBuffer.class); }
/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link Publisher}. * * @param request The {@link HttpRequest} to execute * @param <I> The request body type * @return A {@link Publisher} that emits the full {@link HttpResponse} object */ default <I> Publisher<HttpResponse<ByteBuffer>> exchange(HttpRequest<I> request) { return exchange(request, ByteBuffer.class); }
@Override default <I, O> Flowable<HttpResponse<O>> exchange(HttpRequest<I> request, Class<O> bodyType) { return Flowable.fromPublisher(HttpClient.super.exchange(request, bodyType)); }
@Override default <O> Flowable<HttpResponse<O>> exchange(String uri, Class<O> bodyType) { return Flowable.fromPublisher(HttpClient.super.exchange(uri, bodyType)); }
@Override default <I, O> Flowable<HttpResponse<O>> exchange(HttpRequest<I> request, Class<O> bodyType) { return Flowable.fromPublisher(HttpClient.super.exchange(request, bodyType)); }
/** * Perform an HTTP GET request for the given request object emitting the full HTTP response from returned * {@link Publisher}. * * @param uri The request URI * @param bodyType The body type * @param <O> The response body type * @return A {@link Publisher} that emits the full {@link HttpResponse} object */ default <O> Publisher<HttpResponse<O>> exchange(String uri, Class<O> bodyType) { return exchange(HttpRequest.GET(uri), Argument.of(bodyType)); }