io.vertx.rxjava.core.http.HttpClient Maven / Gradle / Ivy
/*
 * Copyright 2014 Red Hat, Inc.
 *
 * Red Hat licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.vertx.rxjava.core.http;
import rx.Observable;
import rx.Single;
import io.vertx.rx.java.RxHelper;
import io.vertx.rx.java.WriteStreamSubscriber;
import io.vertx.rx.java.SingleOnSubscribeAdapter;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Collectors;
import io.vertx.core.Handler;
import io.vertx.core.AsyncResult;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.lang.rx.RxGen;
import io.vertx.lang.rx.TypeArg;
import io.vertx.lang.rx.MappingIterator;
/**
 * An asynchronous HTTP client.
 * 
 * It allows you to make requests to HTTP servers, and a single client can make requests to any server.
 * 
 * It also allows you to open WebSockets to servers.
 * 
 * The client can also pool HTTP connections.
 * 
 * For pooling to occur, keep-alive must be true on the {@link io.vertx.core.http.HttpClientOptions} (default is true).
 * In this case connections will be pooled and re-used if there are pending HTTP requests waiting to get a connection,
 * otherwise they will be closed.
 * 
 * This gives the benefits of keep alive when the client is loaded but means we don't keep connections hanging around
 * unnecessarily when there would be no benefits anyway.
 * 
 * The client also supports pipe-lining of requests. Pipe-lining means another request is sent on the same connection
 * before the response from the preceding one has returned. Pipe-lining is not appropriate for all requests.
 * 
 * To enable pipe-lining, it must be enabled on the {@link io.vertx.core.http.HttpClientOptions} (default is false).
 * 
 * When pipe-lining is enabled the connection will be automatically closed when all in-flight responses have returned
 * and there are no outstanding pending requests to write.
 * 
 * The client is designed to be reused between requests.
 *
 * 
 * NOTE: This class has been automatically generated from the {@link io.vertx.core.http.HttpClient original} non RX-ified interface using Vert.x codegen.
 */
@RxGen(io.vertx.core.http.HttpClient.class)
public class HttpClient implements io.vertx.rxjava.core.metrics.Measured {
  @Override
  public String toString() {
    return delegate.toString();
  }
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    HttpClient that = (HttpClient) o;
    return delegate.equals(that.delegate);
  }
  
  @Override
  public int hashCode() {
    return delegate.hashCode();
  }
  public static final TypeArg __TYPE_ARG = new TypeArg<>(    obj -> new HttpClient((io.vertx.core.http.HttpClient) obj),
    HttpClient::getDelegate
  );
  private final io.vertx.core.http.HttpClient delegate;
  
  public HttpClient(io.vertx.core.http.HttpClient delegate) {
    this.delegate = delegate;
  }
  public HttpClient(Object delegate) {
    this.delegate = (io.vertx.core.http.HttpClient)delegate;
  }
  public io.vertx.core.http.HttpClient getDelegate() {
    return delegate;
  }
  /**
   * Whether the metrics are enabled for this measured object
   * @return true if metrics are enabled
   */
  public boolean isMetricsEnabled() { 
    boolean ret = delegate.isMetricsEnabled();
    return ret;
  }
  /**
   * Create an HTTP request to send to the server. The handler
   * is called when the request is ready to be sent.
   * @param options the request options
   * @param handler the handler called when the request is ready to be sent
   */
  public void request(io.vertx.core.http.RequestOptions options, io.vertx.core.Handler> handler) { 
    delegate.request(options, new Handler>() {
      public void handle(AsyncResult ar) {
        if (ar.succeeded()) {
          handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.http.HttpClientRequest.newInstance((io.vertx.core.http.HttpClientRequest)ar.result())));
        } else {
          handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
        }
      }
    });
  }
  /**
   * Create an HTTP request to send to the server. The handler
   * is called when the request is ready to be sent.
   * @param options the request options
   */
  public void request(io.vertx.core.http.RequestOptions options) {
    request(options, ar -> { });
  }
    /**
   * Create an HTTP request to send to the server. The handler
   * is called when the request is ready to be sent.
   * @param options the request options
   * @return 
   */
  public rx.Single rxRequest(io.vertx.core.http.RequestOptions options) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      request(options, fut);
    }));
  }
  /**
   * Create an HTTP request to send to the server at the host and port. The handler
   * is called when the request is ready to be sent.
   * @param method the HTTP method
   * @param port the port
   * @param host the host
   * @param requestURI the relative URI
   * @param handler the handler called when the request is ready to be sent
   */
  public void request(io.vertx.core.http.HttpMethod method, int port, java.lang.String host, java.lang.String requestURI, io.vertx.core.Handler> handler) { 
    delegate.request(method, port, host, requestURI, new Handler>() {
      public void handle(AsyncResult ar) {
        if (ar.succeeded()) {
          handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.http.HttpClientRequest.newInstance((io.vertx.core.http.HttpClientRequest)ar.result())));
        } else {
          handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
        }
      }
    });
  }
  /**
   * Create an HTTP request to send to the server at the host and port. The handler
   * is called when the request is ready to be sent.
   * @param method the HTTP method
   * @param port the port
   * @param host the host
   * @param requestURI the relative URI
   */
  public void request(io.vertx.core.http.HttpMethod method, int port, java.lang.String host, java.lang.String requestURI) {
    request(method, port, host, requestURI, ar -> { });
  }
    /**
   * Create an HTTP request to send to the server at the host and port. The handler
   * is called when the request is ready to be sent.
   * @param method the HTTP method
   * @param port the port
   * @param host the host
   * @param requestURI the relative URI
   * @return 
   */
  public rx.Single rxRequest(io.vertx.core.http.HttpMethod method, int port, java.lang.String host, java.lang.String requestURI) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      request(method, port, host, requestURI, fut);
    }));
  }
  /**
   * Create an HTTP request to send to the server at the host and default port. The handler
   * is called when the request is ready to be sent.
   * @param method the HTTP method
   * @param host the host
   * @param requestURI the relative URI
   * @param handler the handler called when the request is ready to be sent
   */
  public void request(io.vertx.core.http.HttpMethod method, java.lang.String host, java.lang.String requestURI, io.vertx.core.Handler> handler) { 
    delegate.request(method, host, requestURI, new Handler>() {
      public void handle(AsyncResult ar) {
        if (ar.succeeded()) {
          handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.http.HttpClientRequest.newInstance((io.vertx.core.http.HttpClientRequest)ar.result())));
        } else {
          handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
        }
      }
    });
  }
  /**
   * Create an HTTP request to send to the server at the host and default port. The handler
   * is called when the request is ready to be sent.
   * @param method the HTTP method
   * @param host the host
   * @param requestURI the relative URI
   */
  public void request(io.vertx.core.http.HttpMethod method, java.lang.String host, java.lang.String requestURI) {
    request(method, host, requestURI, ar -> { });
  }
    /**
   * Create an HTTP request to send to the server at the host and default port. The handler
   * is called when the request is ready to be sent.
   * @param method the HTTP method
   * @param host the host
   * @param requestURI the relative URI
   * @return 
   */
  public rx.Single rxRequest(io.vertx.core.http.HttpMethod method, java.lang.String host, java.lang.String requestURI) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      request(method, host, requestURI, fut);
    }));
  }
  /**
   * Create an HTTP request to send to the server at the default host and port. The handler
   * is called when the request is ready to be sent.
   * @param method the HTTP method
   * @param requestURI the relative URI
   * @param handler the handler called when the request is ready to be sent
   */
  public void request(io.vertx.core.http.HttpMethod method, java.lang.String requestURI, io.vertx.core.Handler> handler) { 
    delegate.request(method, requestURI, new Handler>() {
      public void handle(AsyncResult ar) {
        if (ar.succeeded()) {
          handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.http.HttpClientRequest.newInstance((io.vertx.core.http.HttpClientRequest)ar.result())));
        } else {
          handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
        }
      }
    });
  }
  /**
   * Create an HTTP request to send to the server at the default host and port. The handler
   * is called when the request is ready to be sent.
   * @param method the HTTP method
   * @param requestURI the relative URI
   */
  public void request(io.vertx.core.http.HttpMethod method, java.lang.String requestURI) {
    request(method, requestURI, ar -> { });
  }
    /**
   * Create an HTTP request to send to the server at the default host and port. The handler
   * is called when the request is ready to be sent.
   * @param method the HTTP method
   * @param requestURI the relative URI
   * @return 
   */
  public rx.Single rxRequest(io.vertx.core.http.HttpMethod method, java.lang.String requestURI) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      request(method, requestURI, fut);
    }));
  }
  /**
   * Connect a WebSocket to the specified port, host and relative request URI
   * @param port the port
   * @param host the host
   * @param requestURI the relative URI
   * @param handler handler that will be called with the WebSocket when connected
   */
  public void webSocket(int port, java.lang.String host, java.lang.String requestURI, io.vertx.core.Handler> handler) { 
    delegate.webSocket(port, host, requestURI, new Handler>() {
      public void handle(AsyncResult ar) {
        if (ar.succeeded()) {
          handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.http.WebSocket.newInstance((io.vertx.core.http.WebSocket)ar.result())));
        } else {
          handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
        }
      }
    });
  }
  /**
   * Connect a WebSocket to the specified port, host and relative request URI
   * @param port the port
   * @param host the host
   * @param requestURI the relative URI
   */
  public void webSocket(int port, java.lang.String host, java.lang.String requestURI) {
    webSocket(port, host, requestURI, ar -> { });
  }
    /**
   * Connect a WebSocket to the specified port, host and relative request URI
   * @param port the port
   * @param host the host
   * @param requestURI the relative URI
   * @return 
   */
  public rx.Single rxWebSocket(int port, java.lang.String host, java.lang.String requestURI) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      webSocket(port, host, requestURI, fut);
    }));
  }
  /**
   * Connect a WebSocket to the host and relative request URI and default port
   * @param host the host
   * @param requestURI the relative URI
   * @param handler handler that will be called with the WebSocket when connected
   */
  public void webSocket(java.lang.String host, java.lang.String requestURI, io.vertx.core.Handler> handler) { 
    delegate.webSocket(host, requestURI, new Handler>() {
      public void handle(AsyncResult ar) {
        if (ar.succeeded()) {
          handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.http.WebSocket.newInstance((io.vertx.core.http.WebSocket)ar.result())));
        } else {
          handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
        }
      }
    });
  }
  /**
   * Connect a WebSocket to the host and relative request URI and default port
   * @param host the host
   * @param requestURI the relative URI
   */
  public void webSocket(java.lang.String host, java.lang.String requestURI) {
    webSocket(host, requestURI, ar -> { });
  }
    /**
   * Connect a WebSocket to the host and relative request URI and default port
   * @param host the host
   * @param requestURI the relative URI
   * @return 
   */
  public rx.Single rxWebSocket(java.lang.String host, java.lang.String requestURI) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      webSocket(host, requestURI, fut);
    }));
  }
  /**
   * Connect a WebSocket at the relative request URI using the default host and port
   * @param requestURI the relative URI
   * @param handler handler that will be called with the WebSocket when connected
   */
  public void webSocket(java.lang.String requestURI, io.vertx.core.Handler> handler) { 
    delegate.webSocket(requestURI, new Handler>() {
      public void handle(AsyncResult ar) {
        if (ar.succeeded()) {
          handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.http.WebSocket.newInstance((io.vertx.core.http.WebSocket)ar.result())));
        } else {
          handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
        }
      }
    });
  }
  /**
   * Connect a WebSocket at the relative request URI using the default host and port
   * @param requestURI the relative URI
   */
  public void webSocket(java.lang.String requestURI) {
    webSocket(requestURI, ar -> { });
  }
    /**
   * Connect a WebSocket at the relative request URI using the default host and port
   * @param requestURI the relative URI
   * @return 
   */
  public rx.Single rxWebSocket(java.lang.String requestURI) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      webSocket(requestURI, fut);
    }));
  }
  /**
   * Connect a WebSocket with the specified options.
   * @param options the request options
   * @param handler 
   */
  public void webSocket(io.vertx.core.http.WebSocketConnectOptions options, io.vertx.core.Handler> handler) { 
    delegate.webSocket(options, new Handler>() {
      public void handle(AsyncResult ar) {
        if (ar.succeeded()) {
          handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.http.WebSocket.newInstance((io.vertx.core.http.WebSocket)ar.result())));
        } else {
          handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
        }
      }
    });
  }
  /**
   * Connect a WebSocket with the specified options.
   * @param options the request options
   */
  public void webSocket(io.vertx.core.http.WebSocketConnectOptions options) {
    webSocket(options, ar -> { });
  }
    /**
   * Connect a WebSocket with the specified options.
   * @param options the request options
   * @return 
   */
  public rx.Single rxWebSocket(io.vertx.core.http.WebSocketConnectOptions options) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      webSocket(options, fut);
    }));
  }
  /**
   * Connect a WebSocket with the specified absolute url, with the specified headers, using
   * the specified version of WebSockets, and the specified WebSocket sub protocols.
   * @param url the absolute url
   * @param headers the headers
   * @param version the WebSocket version
   * @param subProtocols the subprotocols to use
   * @param handler handler that will be called if WebSocket connection fails
   */
  public void webSocketAbs(java.lang.String url, io.vertx.rxjava.core.MultiMap headers, io.vertx.core.http.WebsocketVersion version, java.util.List subProtocols, io.vertx.core.Handler> handler) { 
    delegate.webSocketAbs(url, headers.getDelegate(), version, subProtocols, new Handler>() {
      public void handle(AsyncResult ar) {
        if (ar.succeeded()) {
          handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.core.http.WebSocket.newInstance((io.vertx.core.http.WebSocket)ar.result())));
        } else {
          handler.handle(io.vertx.core.Future.failedFuture(ar.cause()));
        }
      }
    });
  }
  /**
   * Connect a WebSocket with the specified absolute url, with the specified headers, using
   * the specified version of WebSockets, and the specified WebSocket sub protocols.
   * @param url the absolute url
   * @param headers the headers
   * @param version the WebSocket version
   * @param subProtocols the subprotocols to use
   */
  public void webSocketAbs(java.lang.String url, io.vertx.rxjava.core.MultiMap headers, io.vertx.core.http.WebsocketVersion version, java.util.List subProtocols) {
    webSocketAbs(url, headers, version, subProtocols, ar -> { });
  }
    /**
   * Connect a WebSocket with the specified absolute url, with the specified headers, using
   * the specified version of WebSockets, and the specified WebSocket sub protocols.
   * @param url the absolute url
   * @param headers the headers
   * @param version the WebSocket version
   * @param subProtocols the subprotocols to use
   * @return 
   */
  public rx.Single rxWebSocketAbs(java.lang.String url, io.vertx.rxjava.core.MultiMap headers, io.vertx.core.http.WebsocketVersion version, java.util.List subProtocols) { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      webSocketAbs(url, headers, version, subProtocols, fut);
    }));
  }
  /**
   * Set a connection handler for the client. This handler is called when a new connection is established.
   * @param handler 
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.http.HttpClient connectionHandler(io.vertx.core.Handler handler) { 
    delegate.connectionHandler(new Handler() {
      public void handle(io.vertx.core.http.HttpConnection event) {
        handler.handle(io.vertx.rxjava.core.http.HttpConnection.newInstance((io.vertx.core.http.HttpConnection)event));
      }
    });
    return this;
  }
  /**
   * Set a redirect handler for the http client.
   * 
   * The redirect handler is called when a 3xx response is received and the request is configured to
   * follow redirects with {@link io.vertx.rxjava.core.http.HttpClientRequest#setFollowRedirects}.
   * 
   * The redirect handler is passed the {@link io.vertx.rxjava.core.http.HttpClientResponse}, it can return an {@link io.vertx.rxjava.core.http.HttpClientRequest} or null.
   * 
   *   - when null is returned, the original response is processed by the original request response handler
 
   *   - when a new 
Future is returned, the client will send this new request 
   * 
   * The new request will get a copy of the previous request headers unless headers are set. In this case,
   * the client assumes that the redirect handler exclusively managers the headers of the new request.
   * 
   * The handler must return a Future unsent so the client can further configure it and send it.
   * @param handler the new redirect handler
   * @return a reference to this, so the API can be used fluently
   */
  public io.vertx.rxjava.core.http.HttpClient redirectHandler(java.util.function.Function> handler) { 
    delegate.redirectHandler(new Function>() {
      public io.vertx.core.Future apply(io.vertx.core.http.HttpClientResponse arg) {
        io.vertx.core.Future ret = handler.apply(io.vertx.rxjava.core.http.HttpClientResponse.newInstance((io.vertx.core.http.HttpClientResponse)arg));
        return ret.map(val -> val);
      }
    });
    return this;
  }
  /**
   * Close the client. Closing will close down any pooled connections.
   * Clients should always be closed after use.
   * @param handler 
   */
  public void close(io.vertx.core.Handler> handler) { 
    delegate.close(handler);
  }
  /**
   * Close the client. Closing will close down any pooled connections.
   * Clients should always be closed after use.
   */
  public void close() {
    close(ar -> { });
  }
    /**
   * Close the client. Closing will close down any pooled connections.
   * Clients should always be closed after use.
   * @return 
   */
  public rx.Single rxClose() { 
    return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
      close(fut);
    }));
  }
  public static HttpClient newInstance(io.vertx.core.http.HttpClient arg) {
    return arg != null ? new HttpClient(arg) : null;
  }
}