-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Resolve addresses by three word addresses
- Loading branch information
Sandra Thieme
committed
Feb 26, 2018
1 parent
079a45f
commit 230c380
Showing
14 changed files
with
396 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/net/contargo/iris/address/w3w/ForwardW3wResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package net.contargo.iris.address.w3w; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import net.contargo.iris.GeoLocation; | ||
|
||
import java.math.BigDecimal; | ||
|
||
|
||
/** | ||
* @author Sandra Thieme - thieme@synyx.de | ||
*/ | ||
class ForwardW3wResponse { | ||
|
||
private final W3wResponseGeometry geometry; | ||
private final W3wResponseStatus status; | ||
|
||
@JsonCreator | ||
ForwardW3wResponse(@JsonProperty("geometry") W3wResponseGeometry geometry, | ||
@JsonProperty("status") W3wResponseStatus status) { | ||
|
||
this.geometry = geometry; | ||
this.status = status; | ||
} | ||
|
||
GeoLocation toGeolocation() { | ||
|
||
return new GeoLocation(geometry.lat, geometry.lon); | ||
} | ||
|
||
|
||
boolean error() { | ||
|
||
return status.code != null; | ||
} | ||
|
||
|
||
Integer errorCode() { | ||
|
||
return status.code; | ||
} | ||
|
||
|
||
String errorMessage() { | ||
|
||
return status.message; | ||
} | ||
|
||
private static class W3wResponseGeometry { | ||
|
||
private final BigDecimal lat; | ||
private final BigDecimal lon; | ||
|
||
@JsonCreator | ||
private W3wResponseGeometry(@JsonProperty("lat") BigDecimal lat, | ||
@JsonProperty("lng") BigDecimal lon) { | ||
|
||
this.lat = lat; | ||
this.lon = lon; | ||
} | ||
} | ||
|
||
private static class W3wResponseStatus { | ||
|
||
private final Integer code; | ||
private final String message; | ||
|
||
@JsonCreator | ||
private W3wResponseStatus(@JsonProperty("code") Integer code, | ||
@JsonProperty("message") String message) { | ||
|
||
this.code = code; | ||
this.message = message; | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/net/contargo/iris/address/w3w/ThreeWordClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/net/contargo/iris/address/w3w/ThreeWordClientException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.contargo.iris.address.w3w; | ||
|
||
/** | ||
* @author Sandra Thieme - thieme@synyx.de | ||
*/ | ||
public class ThreeWordClientException extends RuntimeException { | ||
|
||
ThreeWordClientException(Integer code, String message, String threeWords) { | ||
|
||
super("API of w3w returned error code " + code + " with message '" + message | ||
+ "' for three word address '" + threeWords + "'"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/net/contargo/iris/address/w3w/ThreeWordMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.contargo.iris.address.w3w; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
|
||
/** | ||
* @author Sandra Thieme - thieme@synyx.de | ||
*/ | ||
public class ThreeWordMatcher { | ||
|
||
private static final Pattern THREE_WORD_PATTERN = Pattern.compile("^\\p{L}+\\.\\p{L}+\\.\\p{L}+$"); | ||
|
||
public static boolean isThreeWordAddress(String input) { | ||
|
||
return input != null && THREE_WORD_PATTERN.matcher(input.trim()).matches(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.