/** * 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} 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); }
/** * 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 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 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 <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)); }
@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!")); } }
@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); } } }
} else { try { return blockingHttpClient.retrieve( request, returnType.asArgument(), errorType );
@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(); }
/** * 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} 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} 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); }
/** * 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); }
/** * 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 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 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 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 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 <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)); }