/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.Publisher} and converting the response body to the specified type. * * @param request The {@link HttpRequest} to execute * @param <I> The request body type * @return A string result or null if a 404 is returned * @throws HttpClientResponseException if an error status is returned */ default <I> String retrieve(HttpRequest<I> request) { return retrieve(request, String.class); }
/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.Publisher}. * * @param request The {@link HttpRequest} to execute * @param <I> The request body type * @param <O> The response body type * @return The full {@link HttpResponse} object */ default <I, O> HttpResponse<O> exchange(HttpRequest<I> request) { return exchange(request, (Argument<O>) null); }
return blockingHttpClient.exchange( request, returnType.asArgument().getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT), errorType ); } else if (void.class == javaReturnType) { blockingHttpClient.exchange(request, null, errorType); return null; } else { try { return blockingHttpClient.retrieve( request, returnType.asArgument(), errorType );
return blockingHttpClient.exchange( request, returnType.asArgument().getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT), errorType ); } else if (void.class == javaReturnType) { blockingHttpClient.exchange(request, null, errorType); return null; } else { try { return blockingHttpClient.retrieve( request, returnType.asArgument(), errorType );
/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.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 result of the given type or null the URI returns a 404 * @throws HttpClientResponseException if an error status is returned */ @SuppressWarnings("unchecked") default <I, O> O retrieve(HttpRequest<I> request, Argument<O> bodyType) { return retrieve(request, bodyType, HttpClient.DEFAULT_ERROR_TYPE); }
/** * <p>Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.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 The full {@link HttpResponse} object */ default <I, O> HttpResponse<O> exchange(HttpRequest<I> request, Argument<O> bodyType) { return exchange(request, bodyType, HttpClient.DEFAULT_ERROR_TYPE); }
return blockingHttpClient.exchange( request, returnType.asArgument().getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT), errorType ); } else if (void.class == javaReturnType) { blockingHttpClient.exchange(request, null, errorType); return null; } else { try { return blockingHttpClient.retrieve( request, returnType.asArgument(), errorType );
/** * Perform an HTTP GET request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.Publisher} and converting the response body to the specified type. * * @param uri The URI * @return A string result or null if a 404 is returned * @throws HttpClientResponseException if an error status is returned */ default String retrieve(String uri) { return retrieve(HttpRequest.GET(uri), String.class); }
/** * Perform a GET request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.Publisher}. * * @param uri The URI of the GET request * @param <O> The response body type * @return The full {@link HttpResponse} object */ default <O> HttpResponse<O> exchange(String uri) { return exchange(HttpRequest.GET(uri), (Argument<O>) null); }
/** * Perform an HTTP GET request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.Publisher} and converting the response body to the specified type. * * @param uri The URI * @param bodyType The body type * @param <O> The body generic type * @return A result or null if a 404 is returned * @throws HttpClientResponseException if an error status is returned */ default <O> O retrieve(String uri, Class<O> bodyType) { return retrieve(HttpRequest.GET(uri), bodyType); }
/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.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 The full {@link HttpResponse} object */ default <I, O> HttpResponse<O> exchange(HttpRequest<I> request, Class<O> bodyType) { return exchange(request, Argument.of(bodyType)); }
/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.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 result of the given type or null the URI returns a 404 * @throws HttpClientResponseException if an error status is returned */ default <I, O> O retrieve(HttpRequest<I> request, Class<O> bodyType) { return retrieve(request, Argument.of(bodyType)); }
/** * Perform a GET request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.Publisher}. * * @param uri The URI of the GET request * @param <O> The response body type * @param bodyType The body type * @return The full {@link HttpResponse} object */ default <O> HttpResponse<O> exchange(String uri, Class<O> bodyType) { return exchange(HttpRequest.GET(uri), Argument.of(bodyType)); }
/** * Perform a GET request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.Publisher}. * * @param uri The URI of the GET request * @param <O> The response body type * @param <E> The error type * @param bodyType The body type * @param errorType The error type * @return The full {@link HttpResponse} object */ default <O, E> O retrieve(String uri, Class<O> bodyType, Class<E> errorType) { return retrieve(HttpRequest.GET(uri), Argument.of(bodyType), Argument.of(errorType)); }
/** * Perform a GET request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.Publisher}. * * @param uri The URI of the GET request * @param <O> The response body type * @param <E> The error type * @param bodyType The body type * @param errorType The error type * @return The full {@link HttpResponse} object */ default <O, E> HttpResponse<O> exchange(String uri, Class<O> bodyType, Class<E> errorType) { return exchange(HttpRequest.GET(uri), Argument.of(bodyType), Argument.of(errorType)); } }
@Test public void testReadValueFromSpringCloudConfig() throws Exception { HttpRequest request = HttpRequest.GET("/spring-cloud/issues/1"); String body = client.toBlocking().retrieve(request); assertNotNull(body); assertThat(body, equalTo("test: issue # 1!")); } }
/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.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 result of the given type or null the URI returns a 404 * @throws HttpClientResponseException if an error status is returned */ @SuppressWarnings("unchecked") default <I, O, E> O retrieve(HttpRequest<I> request, Argument<O> bodyType, Argument<E> errorType) { HttpResponse<O> response = exchange(request, bodyType, errorType); if (HttpStatus.class.isAssignableFrom(bodyType.getType())) { return (O) response.getStatus(); } else { return response .getBody() .orElseThrow(() -> new HttpClientResponseException( "Empty body", response )); } }
@Override public O invoke(FunctionDefinition definition, I input, Argument<O> outputType) { Optional<URI> opt = definition.getURI(); if (!opt.isPresent()) { throw new FunctionNotFoundException(definition.getName()); } else { URI uri = opt.get(); HttpRequest request; if (input == null) { request = HttpRequest.GET(uri.toString()); } else { request = HttpRequest.POST(uri.toString(), input); } if (input != null && ClassUtils.isJavaLangType(input.getClass())) { ((MutableHttpRequest) request).contentType(MediaType.TEXT_PLAIN_TYPE); } Class<O> outputJavaType = outputType.getType(); if (ClassUtils.isJavaLangType(outputJavaType)) { ((MutableHttpRequest) request).accept(MediaType.TEXT_PLAIN_TYPE); } if (Publishers.isConvertibleToPublisher(outputJavaType)) { Publisher publisher = httpClient.retrieve(request, outputType.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT)); return ConversionService.SHARED.convert(publisher, outputType).orElseThrow(() -> new FunctionExecutionException("Unsupported Reactive type: " + outputJavaType) ); } else { return (O) httpClient.toBlocking().retrieve(request, outputType); } } }
/** * Perform an HTTP request for the given request object emitting the full HTTP response from returned * {@link org.reactivestreams.Publisher}. * * @param request The {@link HttpRequest} to execute * @param <I> The request body type * @param <O> The response body type * @return The full {@link HttpResponse} object */ default <I, O> HttpResponse<O> exchange(HttpRequest<I> request) { return exchange(request, (Argument<O>) null); }
@Test public void testSimpleRetrieve() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::simple[] String uri = UriBuilder.of("/hello/{name}") .expand(Collections.singletonMap("name", "John")) .toString(); assertEquals("/hello/John", uri); String result = client.toBlocking().retrieve(uri); assertEquals( "Hello John", result ); // end::simple[] embeddedServer.stop(); client.stop(); }