Search in sources :

Example 1 with HttpClientRequest

use of io.vertx.rxjava.core.http.HttpClientRequest in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/");
    req.toObservable().flatMap(resp -> {
        if (resp.statusCode() != 200) {
            throw new RuntimeException("Wrong status code " + resp.statusCode());
        }
        return Observable.just(Buffer.buffer()).mergeWith(resp.toObservable());
    }).reduce(Buffer::appendBuffer).map(buffer -> buffer.toString("UTF-8")).subscribe(data -> System.out.println("Server content " + data));
    // End request
    req.end();
}
Also used : Buffer(io.vertx.rxjava.core.buffer.Buffer) HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpMethod(io.vertx.core.http.HttpMethod) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) Observable(rx.Observable) Buffer(io.vertx.rxjava.core.buffer.Buffer) HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient)

Example 2 with HttpClientRequest

use of io.vertx.rxjava.core.http.HttpClientRequest in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/");
    req.toObservable().flatMap(HttpClientResponse::toObservable).lift(io.vertx.rxjava.core.RxHelper.unmarshaller(Data.class)).subscribe(data -> {
        System.out.println("Got response " + data.message);
    });
    // End request
    req.end();
}
Also used : HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpClientResponse(io.vertx.rxjava.core.http.HttpClientResponse)

Example 3 with HttpClientRequest

use of io.vertx.rxjava.core.http.HttpClientRequest in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/");
    req.toObservable().flatMap(resp -> {
        if (resp.statusCode() != 200) {
            throw new RuntimeException("Wrong status code " + resp.statusCode());
        }
        return resp.toObservable();
    }).subscribe(data -> System.out.println("Server content " + data.toString("UTF-8")));
    // End request
    req.end();
}
Also used : HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpMethod(io.vertx.core.http.HttpMethod) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient)

Example 4 with HttpClientRequest

use of io.vertx.rxjava.core.http.HttpClientRequest in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    // Create two requests
    HttpClientRequest req1 = client.request(HttpMethod.GET, 8080, "localhost", "/");
    HttpClientRequest req2 = client.request(HttpMethod.GET, 8080, "localhost", "/");
    // Turn the requests responses into Observable<JsonObject>
    Observable<JsonObject> obs1 = req1.toObservable().flatMap(HttpClientResponse::toObservable).map(buf -> new JsonObject(buf.toString("UTF-8")));
    Observable<JsonObject> obs2 = req2.toObservable().flatMap(HttpClientResponse::toObservable).map(buf -> new JsonObject(buf.toString("UTF-8")));
    // Combine the responses with the zip into a single response
    obs1.zipWith(obs2, (b1, b2) -> new JsonObject().put("req1", b1).put("req2", b2)).subscribe(json -> {
        System.out.println("Got combined result " + json);
    }, err -> {
        err.printStackTrace();
    });
    req1.end();
    req2.end();
}
Also used : HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) HttpClientResponse(io.vertx.rxjava.core.http.HttpClientResponse) Observable(rx.Observable) HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject)

Example 5 with HttpClientRequest

use of io.vertx.rxjava.core.http.HttpClientRequest in project georocket by georocket.

the class ElasticsearchInstaller method doDownload.

/**
 * Download a file
 * @param downloadUrl the URL to download from
 * @param dest the destination file
 * @return a single emitting exactly one item once the file has been
 * downloaded
 */
private Single<Void> doDownload(String downloadUrl, AsyncFile dest) {
    ObservableFuture<Void> observable = RxHelper.observableFuture();
    Handler<AsyncResult<Void>> handler = observable.toHandler();
    HttpClientOptions options = new HttpClientOptions();
    if (downloadUrl.startsWith("https")) {
        options.setSsl(true);
    }
    HttpClient client = vertx.createHttpClient(options);
    HttpClientRequest req = client.getAbs(downloadUrl);
    req.exceptionHandler(t -> handler.handle(Future.failedFuture(t)));
    req.handler(res -> {
        if (res.statusCode() != 200) {
            handler.handle(Future.failedFuture(new HttpException(res.statusCode(), res.statusMessage())));
            return;
        }
        // get content-length
        int length;
        int[] read = { 0 };
        int[] lastOutput = { 0 };
        String contentLength = res.getHeader("Content-Length");
        if (contentLength != null) {
            length = Integer.parseInt(contentLength);
        } else {
            length = 0;
        }
        res.exceptionHandler(t -> handler.handle(Future.failedFuture(t)));
        // download file contents, log progress
        res.handler(buf -> {
            read[0] += buf.length();
            if (lastOutput[0] == 0 || read[0] - lastOutput[0] > 1024 * 2048) {
                logProgress(length, read[0]);
                lastOutput[0] = read[0];
            }
            dest.write(buf);
        });
        res.endHandler(v -> {
            logProgress(length, read[0]);
            handler.handle(Future.succeededFuture());
        });
    });
    req.end();
    return observable.toSingle();
}
Also used : HttpClientRequest(io.vertx.rxjava.core.http.HttpClientRequest) HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpException(io.georocket.util.HttpException) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Aggregations

HttpClient (io.vertx.rxjava.core.http.HttpClient)5 HttpClientRequest (io.vertx.rxjava.core.http.HttpClientRequest)5 HttpMethod (io.vertx.core.http.HttpMethod)3 Runner (io.vertx.example.util.Runner)3 AbstractVerticle (io.vertx.rxjava.core.AbstractVerticle)3 HttpClientResponse (io.vertx.rxjava.core.http.HttpClientResponse)2 Observable (rx.Observable)2 HttpException (io.georocket.util.HttpException)1 AsyncResult (io.vertx.core.AsyncResult)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 JsonObject (io.vertx.core.json.JsonObject)1 Buffer (io.vertx.rxjava.core.buffer.Buffer)1