/** * Creates a simple HTTP Client with no address to which this client should connect * and with default sslContext support. * * @return a simple HTTP client */ public static HttpClient create() { return create(HttpClientOptions.Builder::sslSupport); }
public HttpClient build() { return new HttpClient(this); } }
/** * HTTP GET the passed URL. When connection has been made, the passed handler is * invoked and can be used to tune the request and write data to it. * * @param url the target remote URL * @param handler the {@link Function} to invoke on open channel * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> get(String url, Function<? super HttpClientRequest, ? extends Publisher<Void>> handler) { return request(HttpMethod.GET, url, handler); }
@Test public void hoverflyProxy() { Mono<HttpClientResponse> remote = HttpClient.create(o -> o.httpProxy(ops -> ops .address(new InetSocketAddress("localhost", hoverflyRule.getProxyPort())) )) .get("http://www.my-test.com/api/bookings/1", HttpClientRequest::sendHeaders); Mono<String> page = remote .flatMapMany(r -> r.receive() .retain() .asString() .limitRate(1)) .reduce(String::concat); StepVerifier.create(page) .expectNextMatches(s -> s.contains("bookingId")) .expectComplete() .verify(Duration.ofSeconds(30)); } }
private static Function<Integer, Mono<InputStream>> zipkinServerStream(String zipkinUrl, Mono<HttpClient> client) { return lookbackSeconds -> client .flatMap(c -> c .get(zipkinQuery(zipkinUrl, lookbackSeconds)) .flatMap(resp -> resp.receive() .aggregate() .asInputStream())); }
/** * Use the passed HTTP method to send to the given URL. When connection has been made, * the passed handler is invoked and can be used to tune the request and * write data to it. * * @param method the HTTP method to send * @param url the target remote URL * @param handler the {@link Function} to invoke on opened TCP connection * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public Mono<HttpClientResponse> request(HttpMethod method, String url, Function<? super HttpClientRequest, ? extends Publisher<Void>> handler) { if (method == null || url == null) { throw new IllegalArgumentException("Method && url cannot be both null"); } return new MonoHttpClientResponse(this, url, method, handler(handler, options)); }
/** * Creates a simple HTTP client using provided {@link HttpClientOptions options} * * @param options the options for the client, including the address to which this * client should connect * @return a simple HTTP client using provided {@link HttpClientOptions options} */ public static HttpClient create(Consumer<? super HttpClientOptions.Builder> options) { return builder().options(options).build(); }
spans -> getClient() .post( zipkinSpansUrl, request -> {
@Override public Mono<Void> execute(URI url, HttpHeaders headers, WebSocketHandler handler) { String[] protocols = beforeHandshake(url, headers, handler); // TODO: https://github.com/reactor/reactor-netty/issues/20 return getHttpClient() .get(url.toString(), request -> addHeaders(request, headers).sendWebsocket()) .then(response -> { HandshakeInfo info = afterHandshake(url, toHttpHeaders(response)); ByteBufAllocator allocator = response.channel().alloc(); NettyDataBufferFactory factory = new NettyDataBufferFactory(allocator); return response.receiveWebsocket((in, out) -> { WebSocketSession session = new ReactorNettyWebSocketSession(in, out, info, factory); return handler.handle(session); }); }); }
/** * Constructor that accepts an {@link HttpClientOptions} consumer to supply * to {@link HttpClient#create(Consumer)}. */ public ReactorNettyWebSocketClient(Consumer<? super HttpClientOptions> clientOptions) { this.httpClient = HttpClient.create(clientOptions); }
/** * WebSocket to the passed URL. * * @param url the target remote URL * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> ws(String url) { return request(WS, url, HttpClientRequest::sendWebsocket); }
/** * Creates a simple HTTP client bound on the provided address and port 80 * * @param address the host to which this client should connect. * @return a simple HTTP client bound on the provided address and port 80 */ public static HttpClient create(String address) { return create(address, 80); }
/** * HTTP DELETE the passed URL. * * @param url the target remote URL * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> delete(String url) { return request(HttpMethod.DELETE, url, null); }
/** * Creates a simple HTTP client bound on localhost and the provided port * * @param port the port to which this client should connect. * @return a simple HTTP client bound on localhost and the provided port */ public static HttpClient create(int port) { return create("localhost", port); }
/** * HTTP PATCH the passed URL. * * @param url the target remote URL * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> patch(String url) { return request(HttpMethod.PATCH, url, null); }
/** * Creates a simple HTTP client bound on the provided address and port * * @param address the host to which this client should connect. * @param port the port to which this client should connect. * @return a simple HTTP client bound on the provided address and port */ public static HttpClient create(String address, int port) { return create(opts -> opts.host(address).port(port)); }
/** * HTTP PATCH the passed URL. When connection has been made, the passed handler is * invoked and can be used to tune the request and write data to it. * * @param url the target remote URL * @param handler the {@link Function} to invoke on open channel * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> patch(String url, Function<? super HttpClientRequest, ? extends Publisher<Void>> handler) { return request(HttpMethod.PATCH, url, handler); }
/** * HTTP GET the passed URL. * * @param url the target remote URL * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> get(String url) { return request(HttpMethod.GET, url, null); }
/** * HTTP PUT the passed URL. When connection has been made, the passed handler is * invoked and can be used to tune the request and write data to it. * * @param url the target remote URL * @param handler the {@link Function} to invoke on open channel * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> put(String url, Function<? super HttpClientRequest, ? extends Publisher<Void>> handler) { return request(HttpMethod.PUT, url, handler); }
/** * HTTP DELETE the passed URL. When connection has been established, the passed handler is * invoked and can be used to tune the request and write data to it. * * @param url the target remote URL * @param handler the {@link Function} to invoke on open channel * @return a {@link Mono} of the {@link HttpServerResponse} ready to consume for * response */ public final Mono<HttpClientResponse> delete(String url, Function<? super HttpClientRequest, ? extends Publisher<Void>> handler) { return request(HttpMethod.DELETE, url, handler); }