Search in sources :

Example 1 with HttpClientResponse

use of io.vertx.reactivex.core.http.HttpClientResponse in project knotx by Cognifide.

the class SampleApplicationTest method request.

private static Observable<HttpClientResponse> request(HttpClient client, HttpMethod method, int port, String domain, String uri, Action1<HttpClientRequest> requestBuilder) {
    return Observable.unsafeCreate(subscriber -> {
        HttpClientRequest req = client.request(method, port, domain, uri);
        Observable<HttpClientResponse> resp = req.toObservable();
        resp.subscribe(subscriber);
        requestBuilder.call(req);
        req.end();
    });
}
Also used : HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse)

Example 2 with HttpClientResponse

use of io.vertx.reactivex.core.http.HttpClientResponse in project knotx by Cognifide.

the class HttpRepositoryConnectorProxyImpl method get.

private Observable<HttpClientResponse> get(HttpClient client, RequestOptions requestOptions, MultiMap headers) {
    return Observable.unsafeCreate(subscriber -> {
        HttpClientRequest req = client.get(requestOptions);
        req.headers().addAll(headers);
        if (headers.get(HttpHeaderNames.HOST.toString()) != null) {
            req.setHost(headers.get(HttpHeaderNames.HOST.toString()));
        }
        Observable<HttpClientResponse> resp = req.toObservable();
        resp.subscribe(subscriber);
        req.end();
    });
}
Also used : HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse)

Example 3 with HttpClientResponse

use of io.vertx.reactivex.core.http.HttpClientResponse in project knotx by Cognifide.

the class KnotxServerRoutingTest method testPostRequest.

private void testPostRequest(String url, Action1<HttpClientResponse> expectedResponse) {
    HttpClient client = Vertx.newInstance(vertx.vertx()).createHttpClient();
    String testBody = "a=b";
    Observable<HttpClientResponse> request = request(client, HttpMethod.POST, KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url, req -> {
        req.headers().set("content-length", String.valueOf(testBody.length()));
        req.headers().set("content-type", "application/x-www-form-urlencoded");
        req.write(testBody);
    });
    request.subscribe(expectedResponse::call);
}
Also used : HttpClient(io.vertx.reactivex.core.http.HttpClient) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse)

Example 4 with HttpClientResponse

use of io.vertx.reactivex.core.http.HttpClientResponse 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.toFlowable().flatMap(HttpClientResponse::toFlowable).map(buffer -> buffer.toJsonObject().mapTo(Data.class)).subscribe(data -> System.out.println("Got response " + data.message));
    // End request
    req.end();
}
Also used : HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpMethod(io.vertx.core.http.HttpMethod) HttpClient(io.vertx.reactivex.core.http.HttpClient) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpClient(io.vertx.reactivex.core.http.HttpClient) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse)

Example 5 with HttpClientResponse

use of io.vertx.reactivex.core.http.HttpClientResponse 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 Flowable<JsonObject>
    Flowable<JsonObject> obs1 = req1.toFlowable().flatMap(HttpClientResponse::toFlowable).map(buf -> new JsonObject(buf.toString("UTF-8")));
    Flowable<JsonObject> obs2 = req2.toFlowable().flatMap(HttpClientResponse::toFlowable).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);
    }, Throwable::printStackTrace);
    req1.end();
    req2.end();
}
Also used : HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) Flowable(io.reactivex.Flowable) HttpMethod(io.vertx.core.http.HttpMethod) JsonObject(io.vertx.core.json.JsonObject) HttpClient(io.vertx.reactivex.core.http.HttpClient) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpClient(io.vertx.reactivex.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

HttpClientResponse (io.vertx.reactivex.core.http.HttpClientResponse)7 HttpClientRequest (io.vertx.reactivex.core.http.HttpClientRequest)6 HttpClient (io.vertx.reactivex.core.http.HttpClient)4 HttpMethod (io.vertx.core.http.HttpMethod)3 Runner (io.vertx.example.util.Runner)2 AbstractVerticle (io.vertx.reactivex.core.AbstractVerticle)2 Maps (com.google.common.collect.Maps)1 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)1 Logback (io.knotx.junit.rule.Logback)1 TestVertxDeployer (io.knotx.junit.rule.TestVertxDeployer)1 FileReader (io.knotx.junit.util.FileReader)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 Flowable (io.reactivex.Flowable)1 Observable (io.reactivex.Observable)1 JsonObject (io.vertx.core.json.JsonObject)1 Async (io.vertx.ext.unit.Async)1 TestContext (io.vertx.ext.unit.TestContext)1 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)1 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)1 Vertx (io.vertx.reactivex.core.Vertx)1