|
1 | 1 | package info.jab.ms.client; |
2 | 2 |
|
| 3 | +import info.jab.ms.algorithm.UnicodeAggregator; |
| 4 | +import info.jab.ms.api.model.PantheonSource; |
3 | 5 | import info.jab.ms.config.GodOutboundProperties; |
| 6 | +import info.jab.ms.service.GodData; |
| 7 | +import info.jab.ms.service.PantheonDataSource; |
4 | 8 | import java.util.List; |
5 | | -import org.slf4j.Logger; |
6 | | -import org.slf4j.LoggerFactory; |
7 | 9 | import org.springframework.beans.factory.annotation.Qualifier; |
| 10 | +import org.springframework.http.MediaType; |
8 | 11 | import org.springframework.core.ParameterizedTypeReference; |
9 | 12 | import org.springframework.stereotype.Component; |
10 | 13 | import org.springframework.web.client.RestClient; |
11 | 14 | import org.springframework.web.client.RestClientException; |
12 | 15 |
|
13 | 16 | @Component |
14 | | -public class GodDataClient { |
15 | | - |
16 | | - private static final Logger log = LoggerFactory.getLogger(GodDataClient.class); |
17 | | - |
18 | | - private static final ParameterizedTypeReference<List<String>> STRING_LIST = |
19 | | - new ParameterizedTypeReference<>() {}; |
20 | | - |
21 | | - private final RestClient restClient; |
22 | | - private final GodOutboundProperties properties; |
23 | | - |
24 | | - public GodDataClient( |
25 | | - @Qualifier("godRestClient") RestClient restClient, GodOutboundProperties properties) { |
26 | | - this.restClient = restClient; |
27 | | - this.properties = properties; |
28 | | - } |
29 | | - |
30 | | - /** |
31 | | - * Single GET to the configured URL for the pantheon. On connect/read failure or non-2xx, returns |
32 | | - * an empty list (no retries). |
33 | | - */ |
34 | | - public List<String> fetchNames(String pantheonKey) { |
35 | | - String url = urlFor(pantheonKey); |
36 | | - if (url == null || url.isBlank()) { |
37 | | - log.warn("god.outbound.fetch.skipped source={} reason=no_url", pantheonKey); |
38 | | - return List.of(); |
39 | | - } |
40 | | - log.debug("god.outbound.fetch.start source={} url={}", pantheonKey, url); |
41 | | - try { |
42 | | - List<String> names = |
43 | | - restClient.get().uri(url).retrieve().body(STRING_LIST); |
44 | | - if (names == null) { |
45 | | - log.warn("god.outbound.fetch.empty_body source={}", pantheonKey); |
46 | | - return List.of(); |
47 | | - } |
48 | | - log.info( |
49 | | - "god.outbound.fetch.ok source={} url={} nameCount={}", |
50 | | - pantheonKey, |
51 | | - url, |
52 | | - names.size()); |
53 | | - return names; |
54 | | - } catch (RestClientException ex) { |
55 | | - log.warn( |
56 | | - "god.outbound.fetch.failed source={} url={} error={}", |
57 | | - pantheonKey, |
58 | | - url, |
59 | | - ex.toString()); |
60 | | - return List.of(); |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - private String urlFor(String pantheonKey) { |
65 | | - if (properties.urls() == null) { |
66 | | - return null; |
67 | | - } |
68 | | - return switch (pantheonKey) { |
69 | | - case "greek" -> properties.urls().greek(); |
70 | | - case "roman" -> properties.urls().roman(); |
71 | | - case "nordic" -> properties.urls().nordic(); |
72 | | - default -> null; |
73 | | - }; |
74 | | - } |
| 17 | +public class GodDataClient implements PantheonDataSource { |
| 18 | + |
| 19 | + private static final ParameterizedTypeReference<List<String>> GOD_NAMES_TYPE = new ParameterizedTypeReference<>() { |
| 20 | + }; |
| 21 | + |
| 22 | + private final RestClient restClient; |
| 23 | + private final GodOutboundProperties properties; |
| 24 | + private final UnicodeAggregator unicodeAggregator; |
| 25 | + private final OutboundCallObserver outboundCallObserver; |
| 26 | + |
| 27 | + public GodDataClient( |
| 28 | + @Qualifier("godOutboundRestClient") RestClient restClient, |
| 29 | + GodOutboundProperties properties, |
| 30 | + UnicodeAggregator unicodeAggregator, |
| 31 | + OutboundCallObserver outboundCallObserver |
| 32 | + ) { |
| 33 | + this.restClient = restClient; |
| 34 | + this.properties = properties; |
| 35 | + this.unicodeAggregator = unicodeAggregator; |
| 36 | + this.outboundCallObserver = outboundCallObserver; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public List<GodData> fetch(PantheonSource source) { |
| 41 | + outboundCallObserver.onStart(source); |
| 42 | + try { |
| 43 | + var names = restClient.get() |
| 44 | + .uri(properties.getUrlFor(source)) |
| 45 | + .accept(MediaType.APPLICATION_JSON) |
| 46 | + .retrieve() |
| 47 | + .body(GOD_NAMES_TYPE); |
| 48 | + var result = names.stream() |
| 49 | + .map(name -> new GodData(name, unicodeAggregator.toBigInteger(name))) |
| 50 | + .toList(); |
| 51 | + outboundCallObserver.onSuccess(source, result.size()); |
| 52 | + return result; |
| 53 | + } catch (RestClientException exception) { |
| 54 | + outboundCallObserver.onFailure(source, exception); |
| 55 | + throw new OutboundSourceException(source, exception); |
| 56 | + } |
| 57 | + } |
75 | 58 | } |
0 commit comments