forked from Hakky54/mutual-tls-ssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VertxWebClientService.java
45 lines (33 loc) 路 1.3 KB
/
VertxWebClientService.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
package nl.altindag.client.service;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import nl.altindag.client.ClientType;
import nl.altindag.client.model.ClientResponse;
import org.springframework.stereotype.Service;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import static nl.altindag.client.Constants.HEADER_KEY_CLIENT_TYPE;
@Service
public class VertxWebClientService implements RequestService {
private static final int TIMEOUT_AMOUNT_IN_SECONDS = 5;
private final WebClient client;
public VertxWebClientService(WebClient client) {
this.client = client;
}
@Override
public ClientResponse executeRequest(String url) throws Exception {
URI uri = URI.create(url);
HttpResponse<Buffer> response = client.get(uri.getPort(), uri.getHost(), uri.getPath())
.putHeader(HEADER_KEY_CLIENT_TYPE, getClientType().getValue())
.send()
.toCompletionStage()
.toCompletableFuture()
.get(TIMEOUT_AMOUNT_IN_SECONDS, TimeUnit.SECONDS);
return new ClientResponse(response.bodyAsString(), response.statusCode());
}
@Override
public ClientType getClientType() {
return ClientType.VERTX;
}
}