-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathThreeWordClient.java
More file actions
47 lines (30 loc) · 1.25 KB
/
ThreeWordClient.java
File metadata and controls
47 lines (30 loc) · 1.25 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
package net.contargo.iris.address.w3w;
import net.contargo.iris.GeoLocation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
/**
* @author Sandra Thieme - thieme@synyx.de
*/
public class ThreeWordClient {
private static final String FORWARD_URL =
"https://api.what3words.com/v2/forward?addr={w3wAddress}&key={apiKey}&lang=de";
private final RestTemplate restTemplate;
private final String apiKey;
public ThreeWordClient(RestTemplate restTemplate, String apiKey) {
this.restTemplate = restTemplate;
this.apiKey = apiKey;
}
public GeoLocation resolve(String threeWords) {
ResponseEntity<ForwardW3wResponse> response = restTemplate.getForEntity(FORWARD_URL, ForwardW3wResponse.class,
threeWords, apiKey);
checkErrorStatus(response.getBody(), threeWords);
return response.getBody().toGeolocation();
}
private static void checkErrorStatus(ForwardW3wResponse response, String threeWords) {
if (response.error()) {
Integer code = response.errorCode();
String message = response.errorMessage();
throw new ThreeWordClientException(code, message, threeWords);
}
}
}