-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientWSImpl.java
More file actions
204 lines (167 loc) · 7.03 KB
/
ClientWSImpl.java
File metadata and controls
204 lines (167 loc) · 7.03 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.switcherapi.client.remote;
import com.switcherapi.client.SwitcherProperties;
import com.switcherapi.client.exception.SwitcherRemoteException;
import com.switcherapi.client.model.ContextKey;
import com.switcherapi.client.model.SwitcherRequest;
import com.switcherapi.client.model.criteria.Snapshot;
import com.switcherapi.client.remote.dto.*;
import com.google.gson.Gson;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import static com.switcherapi.client.remote.Constants.*;
/**
* @author Roger Floriano (petruki)
* @since 2019-12-24
*/
public class ClientWSImpl implements ClientWS {
private final SwitcherProperties switcherProperties;
private final HttpClient client;
private final int timeoutMs;
private final Gson gson = new Gson();
public ClientWSImpl(SwitcherProperties switcherProperties, HttpClient client, int timeoutMs) {
this.switcherProperties = switcherProperties;
this.timeoutMs = timeoutMs;
this.client = client;
}
public static ClientWS build(SwitcherProperties switcherProperties, ExecutorService executorService, int timeoutMs) {
final HttpClient httpClient = ClientWSBuilder.builder(executorService, switcherProperties)
.connectTimeout(Duration.ofMillis(timeoutMs))
.build();
return new ClientWSImpl(switcherProperties, httpClient, timeoutMs);
}
@Override
public CriteriaResponse executeCriteria(final CriteriaRequest criteriaRequest, final String token) {
final String url = switcherProperties.getValue(ContextKey.URL);
try {
final URI uri = new URI(url)
.resolve(String.format(CRITERIA_URL, url,
SwitcherRequest.KEY, criteriaRequest.getSwitcherKey(),
SwitcherRequest.SHOW_REASON, Boolean.TRUE,
SwitcherRequest.BYPASS_METRIC, criteriaRequest.isBypassMetric()));
final HttpResponse<String> response = client.send(HttpRequest.newBuilder()
.uri(uri)
.headers(HEADER_AUTHORIZATION, String.format(TOKEN_TEXT, token),
HEADER_CONTENT_TYPE, HEADER_JSON)
.timeout(Duration.ofMillis(timeoutMs))
.POST(HttpRequest.BodyPublishers.ofString(gson.toJson(criteriaRequest.getInputRequest())))
.build(), HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new SwitcherRemoteException(url, response.statusCode());
}
final CriteriaResponse criteriaResponse = gson.fromJson(response.body(), CriteriaResponse.class);
criteriaResponse.setSwitcherKey(criteriaRequest.getSwitcherKey());
return criteriaResponse;
} catch (Exception e) {
return exceptionHandler(e, url);
}
}
@Override
public Optional<AuthResponse> auth() {
final AuthRequest authRequest = new AuthRequest();
authRequest.setDomain(switcherProperties.getValue(ContextKey.DOMAIN));
authRequest.setComponent(switcherProperties.getValue(ContextKey.COMPONENT));
authRequest.setEnvironment(switcherProperties.getValue(ContextKey.ENVIRONMENT));
final String url = switcherProperties.getValue(ContextKey.URL);
try {
final HttpResponse<String> response = client.send(HttpRequest.newBuilder()
.uri(URI.create(String.format(AUTH_URL, url)))
.headers(HEADER_APIKEY, switcherProperties.getValue(ContextKey.APIKEY),
HEADER_CONTENT_TYPE, HEADER_JSON)
.timeout(Duration.ofMillis(timeoutMs))
.POST(HttpRequest.BodyPublishers.ofString(gson.toJson(authRequest))
).build(), HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new SwitcherRemoteException(url, response.statusCode());
}
return Optional.of(gson.fromJson(response.body(), AuthResponse.class));
} catch (Exception e) {
return exceptionHandler(e, url);
}
}
@Override
public Snapshot resolveSnapshot(final String token) {
final String url = switcherProperties.getValue(ContextKey.URL);
try {
final HttpResponse<String> response = client.send(HttpRequest.newBuilder()
.uri(URI.create(String.format(SNAPSHOT_URL, url)))
.headers(HEADER_AUTHORIZATION, String.format(TOKEN_TEXT, token),
HEADER_CONTENT_TYPE, HEADER_JSON)
.timeout(Duration.ofMillis(timeoutMs))
.POST(HttpRequest.BodyPublishers.ofString(String.format(QUERY,
switcherProperties.getValue(ContextKey.DOMAIN),
switcherProperties.getValue(ContextKey.ENVIRONMENT),
switcherProperties.getValue(ContextKey.COMPONENT)))
).build(), HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new SwitcherRemoteException(url, response.statusCode());
}
return gson.fromJson(response.body(), SnapshotDataResponse.class).getData();
} catch (Exception e) {
return exceptionHandler(e, url);
}
}
@Override
public SnapshotVersionResponse checkSnapshotVersion(final long version, final String token) {
final String url = switcherProperties.getValue(ContextKey.URL);
try {
final HttpResponse<String> response = client.send(HttpRequest.newBuilder()
.uri(URI.create(String.format(SNAPSHOT_VERSION_CHECK, url, version)))
.headers(HEADER_AUTHORIZATION, String.format(TOKEN_TEXT, token),
HEADER_CONTENT_TYPE, HEADER_JSON)
.timeout(Duration.ofMillis(timeoutMs))
.GET().build(), HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new SwitcherRemoteException(url, response.statusCode());
}
return gson.fromJson(response.body(), SnapshotVersionResponse.class);
} catch (Exception e) {
return exceptionHandler(e, url);
}
}
@Override
public boolean isAlive() {
try {
HttpResponse<String> response = client.send(HttpRequest.newBuilder()
.uri(URI.create(String.format(CHECK_URL, switcherProperties.getValue(ContextKey.URL))))
.timeout(Duration.ofMillis(timeoutMs))
.GET().build(), HttpResponse.BodyHandlers.ofString());
return response.statusCode() == 200;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
} catch (Exception e) {
return false;
}
}
@Override
public SwitchersCheck checkSwitchers(Set<String> switchers, final String token) {
final String url = switcherProperties.getValue(ContextKey.URL);
try {
final HttpResponse<String> response = client.send(HttpRequest.newBuilder()
.uri(URI.create(String.format(CHECK_SWITCHERS, url)))
.timeout(Duration.ofMillis(timeoutMs))
.headers(HEADER_AUTHORIZATION, String.format(TOKEN_TEXT, token),
HEADER_CONTENT_TYPE, HEADER_JSON)
.POST(HttpRequest.BodyPublishers.ofString(gson.toJson(new SwitchersCheck(switchers)))
).build(), HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new SwitcherRemoteException(url, response.statusCode());
}
return gson.fromJson(response.body(), SwitchersCheck.class);
} catch (Exception e) {
return exceptionHandler(e, url);
}
}
private <T> T exceptionHandler(Exception e, String url) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new SwitcherRemoteException(url, e);
}
}