-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminStatClient.java
More file actions
57 lines (45 loc) · 1.94 KB
/
Copy pathAdminStatClient.java
File metadata and controls
57 lines (45 loc) · 1.94 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
package apu.saerok_admin.infra.stat;
import apu.saerok_admin.infra.SaerokApiProps;
import apu.saerok_admin.infra.stat.dto.StatSeriesResponse;
import java.net.URI;
import java.util.Collection;
import java.util.Objects;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriBuilder;
@Component
public class AdminStatClient {
private static final String[] ADMIN_STATS_SEGMENTS = {"admin", "stats"};
private static final String SERIES_SEGMENT = "series";
private final RestClient saerokRestClient;
private final String[] missingPrefixSegments;
public AdminStatClient(RestClient saerokRestClient, SaerokApiProps saerokApiProps) {
this.saerokRestClient = saerokRestClient;
this.missingPrefixSegments = saerokApiProps.missingPrefixSegments().toArray(new String[0]);
}
public StatSeriesResponse fetchSeries(Collection<StatMetric> metrics) {
if (metrics == null || metrics.isEmpty()) {
throw new IllegalArgumentException("metrics must not be empty");
}
StatSeriesResponse response = saerokRestClient.get()
.uri(uriBuilder -> buildSeriesUri(uriBuilder, metrics))
.retrieve()
.body(StatSeriesResponse.class);
if (response == null) {
throw new IllegalStateException("Empty response from admin stats API");
}
return response;
}
private URI buildSeriesUri(UriBuilder builder, Collection<StatMetric> metrics) {
if (missingPrefixSegments.length > 0) {
builder.pathSegment(missingPrefixSegments);
}
builder.pathSegment(ADMIN_STATS_SEGMENTS);
builder.pathSegment(SERIES_SEGMENT);
metrics.stream()
.filter(Objects::nonNull)
.map(Enum::name)
.forEach(metric -> builder.queryParam("metric", metric));
return builder.build();
}
}