-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Client.java
49 lines (38 loc) · 1.44 KB
/
Client.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package io.vertx.example.rxjava3.http.client.reduce;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.vertx.core.Launcher;
import io.vertx.core.http.HttpMethod;
import io.vertx.rxjava3.core.AbstractVerticle;
import io.vertx.rxjava3.core.buffer.Buffer;
import io.vertx.rxjava3.core.http.HttpClient;
/*
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
public class Client extends AbstractVerticle {
public static void main(String[] args) {
Launcher.executeCommand("run", Client.class.getName());
}
@Override
public void start() throws Exception {
HttpClient client = vertx.createHttpClient();
Maybe<String> maybe = client.rxRequest(HttpMethod.GET, 8080, "localhost", "/")
// Connect
.flatMapMaybe(req -> req
// Send request
.rxSend()
// Status code check and -> Observable<Buffer>
.flatMapPublisher(resp -> {
if (resp.statusCode() != 200) {
return Flowable.error(new RuntimeException("Wrong status code " + resp.statusCode()));
}
return Flowable.just(Buffer.buffer()).mergeWith(resp.toFlowable());
})
// Reduce all buffers in a single buffer
.reduce(Buffer::appendBuffer)
// Turn in to a string
.map(buffer -> buffer.toString("UTF-8"))
);
maybe.subscribe(data -> System.out.println("Server content " + data), Throwable::printStackTrace);
}
}