From 71c2d59ffed372655e9127fd0c3d091751c6c1a0 Mon Sep 17 00:00:00 2001 From: Duncan Beutler Date: Wed, 16 Sep 2020 16:19:08 -0600 Subject: [PATCH 1/2] US Reverse geocoding API added to SDK. --- .../com/smartystreets/api/ClientBuilder.java | 6 ++ .../api/us_reverse_geo/Address.java | 30 ++++++++++ .../api/us_reverse_geo/Client.java | 33 +++++++++++ .../api/us_reverse_geo/Coordinate.java | 30 ++++++++++ .../api/us_reverse_geo/Lookup.java | 35 +++++++++++ .../api/us_reverse_geo/Result.java | 59 +++++++++++++++++++ .../api/us_reverse_geo/SmartyResponse.java | 11 ++++ .../java/examples/UsReverseGeoExample.java | 55 +++++++++++++++++ .../api/us_reverse_geo/ClientTest.java | 33 +++++++++++ 9 files changed, 292 insertions(+) create mode 100644 src/main/java/com/smartystreets/api/us_reverse_geo/Address.java create mode 100644 src/main/java/com/smartystreets/api/us_reverse_geo/Client.java create mode 100644 src/main/java/com/smartystreets/api/us_reverse_geo/Coordinate.java create mode 100644 src/main/java/com/smartystreets/api/us_reverse_geo/Lookup.java create mode 100644 src/main/java/com/smartystreets/api/us_reverse_geo/Result.java create mode 100644 src/main/java/com/smartystreets/api/us_reverse_geo/SmartyResponse.java create mode 100644 src/main/java/examples/UsReverseGeoExample.java create mode 100644 src/test/java/com/smartystreets/api/us_reverse_geo/ClientTest.java diff --git a/src/main/java/com/smartystreets/api/ClientBuilder.java b/src/main/java/com/smartystreets/api/ClientBuilder.java index b640d17..d6646c0 100644 --- a/src/main/java/com/smartystreets/api/ClientBuilder.java +++ b/src/main/java/com/smartystreets/api/ClientBuilder.java @@ -26,6 +26,7 @@ public class ClientBuilder { private final String US_EXTRACT_API_URL = "https://us-extract.api.smartystreets.com/"; private final String US_STREET_API_URL = "https://us-street.api.smartystreets.com/street-address"; private final String US_ZIP_CODE_API_URL = "https://us-zipcode.api.smartystreets.com/lookup"; + private final String US_REVERSE_GEO_API_URL = "https://us-reverse-geo.api.smartystreets.com/lookup"; private ClientBuilder() { this.serializer = new GoogleSerializer(); @@ -160,6 +161,11 @@ public com.smartystreets.api.us_zipcode.Client buildUsZipCodeApiClient() { return new com.smartystreets.api.us_zipcode.Client(this.buildSender(), this.serializer); } + public com.smartystreets.api.us_reverse_geo.Client buildUsReverseGeoClient() { + this.ensureURLPrefixNotNull(this.US_REVERSE_GEO_API_URL); + return new com.smartystreets.api.us_reverse_geo.Client(this.buildSender(), this.serializer); + } + private Sender buildSender() { if (this.httpSender != null) return this.httpSender; diff --git a/src/main/java/com/smartystreets/api/us_reverse_geo/Address.java b/src/main/java/com/smartystreets/api/us_reverse_geo/Address.java new file mode 100644 index 0000000..2d7cecd --- /dev/null +++ b/src/main/java/com/smartystreets/api/us_reverse_geo/Address.java @@ -0,0 +1,30 @@ +package com.smartystreets.api.us_reverse_geo; + +import com.google.api.client.util.Key; + +public class Address { + //region [ Fields ] + + @Key("street") + private String street; + + @Key("city") + private String city; + + @Key("state_abbreviation") + private String stateAbbreviation; + + @Key("zip_code") + private String zipCode; + + //endregion + + public String getStreet() { return this.street; } + + public String getCity() { return this.city; } + + public String getStateAbbreviation() { return this.stateAbbreviation; } + + public String getZipCode() { return this.zipCode; } + +} diff --git a/src/main/java/com/smartystreets/api/us_reverse_geo/Client.java b/src/main/java/com/smartystreets/api/us_reverse_geo/Client.java new file mode 100644 index 0000000..b323dc1 --- /dev/null +++ b/src/main/java/com/smartystreets/api/us_reverse_geo/Client.java @@ -0,0 +1,33 @@ +package com.smartystreets.api.us_reverse_geo; + +import com.smartystreets.api.Request; +import com.smartystreets.api.Response; +import com.smartystreets.api.Sender; +import com.smartystreets.api.Serializer; +import com.smartystreets.api.exceptions.SmartyException; + +import java.io.IOException; +import java.text.DecimalFormat; + +public class Client { + private final Sender sender; + private final Serializer serializer; + + public Client(Sender sender, Serializer serializer) { + this.sender = sender; + this.serializer = serializer; + } + + public void send(Lookup lookup) throws SmartyException, IOException { + Request request = new Request(); + + DecimalFormat decimalFormat = new DecimalFormat("#.########"); + request.putParameter("latitude", decimalFormat.format(lookup.getLatitude())); + request.putParameter("longitude", decimalFormat.format(lookup.getLongitude())); + + Response httpResponse = this.sender.send(request); + + SmartyResponse response = this.serializer.deserialize(httpResponse.getPayload(), SmartyResponse.class); + lookup.setResponse(response); + } +} diff --git a/src/main/java/com/smartystreets/api/us_reverse_geo/Coordinate.java b/src/main/java/com/smartystreets/api/us_reverse_geo/Coordinate.java new file mode 100644 index 0000000..0dc63ff --- /dev/null +++ b/src/main/java/com/smartystreets/api/us_reverse_geo/Coordinate.java @@ -0,0 +1,30 @@ +package com.smartystreets.api.us_reverse_geo; + +import com.google.api.client.util.Key; + +public class Coordinate { + //region [ Fields ] + + @Key("latitude") + private double latitude; + + @Key("longitude") + private double longitude; + + @Key("license") + private int license; + + @Key("zipcode") + private String zipcode; + + //endregion + + public double getLatitude() { return this.latitude; } + + public double getLongitude() { return this.longitude; } + + public int getLicense() { return this.license; } + + public String getZipCode() { return this.zipcode; } + +} diff --git a/src/main/java/com/smartystreets/api/us_reverse_geo/Lookup.java b/src/main/java/com/smartystreets/api/us_reverse_geo/Lookup.java new file mode 100644 index 0000000..e3450db --- /dev/null +++ b/src/main/java/com/smartystreets/api/us_reverse_geo/Lookup.java @@ -0,0 +1,35 @@ +package com.smartystreets.api.us_reverse_geo; + +import com.google.api.client.util.Key; + +public class Lookup { +//region [ Fields ] + + @Key("latitude") + private double latitude; + + @Key("longitude") + private double longitude; + + private SmartyResponse response; + + //endregion + + public Lookup() { this.response = new SmartyResponse(); } + + public Lookup(double latitude, double longitude) { + this(); + this.latitude = latitude; + this.longitude = longitude; + } + + public Double getLatitude() { return this.latitude; } + + public Double getLongitude() { return this.longitude; } + + public SmartyResponse getResponse() { return this.response; } + + public void setResponse(SmartyResponse response) { + this.response = response; + } +} diff --git a/src/main/java/com/smartystreets/api/us_reverse_geo/Result.java b/src/main/java/com/smartystreets/api/us_reverse_geo/Result.java new file mode 100644 index 0000000..183954d --- /dev/null +++ b/src/main/java/com/smartystreets/api/us_reverse_geo/Result.java @@ -0,0 +1,59 @@ +package com.smartystreets.api.us_reverse_geo; + +import com.google.api.client.util.Key; + +public class Result { + //region [ Fields ] + + @Key("address") + private Address address; + + @Key("coordinate") + private Coordinate coordinate; + + @Key("distance") + private double distance; + + @Key("latitude") + private double latitude; + + @Key("longitude") + private double longitude; + + @Key("street") + private String street; + + @Key("city") + private String city; + + @Key("state_abbreviation") + private String stateAbbreviation; + + @Key("zipcode") + private String zipcode; + + //endregion + + public Address getAddress() { + return this.address; + } + + public Coordinate getCoordinate() { return this.coordinate; } + + public Double getDistance() { return this.distance; } + + public Double getLatitude() { + return this.latitude; + } + + public Double getLongitude() { return this.longitude; } + + public String getStreet() { return this.street; } + + public String getCity() { return this.city; } + + public String getStateAbbreviation() { return this.stateAbbreviation; } + + public String getZipCode() { return this.zipcode; } + +} diff --git a/src/main/java/com/smartystreets/api/us_reverse_geo/SmartyResponse.java b/src/main/java/com/smartystreets/api/us_reverse_geo/SmartyResponse.java new file mode 100644 index 0000000..36949dc --- /dev/null +++ b/src/main/java/com/smartystreets/api/us_reverse_geo/SmartyResponse.java @@ -0,0 +1,11 @@ +package com.smartystreets.api.us_reverse_geo; +import com.google.api.client.util.Key; + +public class SmartyResponse { + + @Key("results") + private Result[] results; + + public Result[] getResults() { return this.results; } + +} diff --git a/src/main/java/examples/UsReverseGeoExample.java b/src/main/java/examples/UsReverseGeoExample.java new file mode 100644 index 0000000..0a781de --- /dev/null +++ b/src/main/java/examples/UsReverseGeoExample.java @@ -0,0 +1,55 @@ +package examples; + +import com.smartystreets.api.ClientBuilder; +import com.smartystreets.api.SharedCredentials; +import com.smartystreets.api.StaticCredentials; +import com.smartystreets.api.exceptions.SmartyException; +import com.smartystreets.api.us_reverse_geo.Client; +import com.smartystreets.api.us_reverse_geo.Lookup; +import com.smartystreets.api.us_reverse_geo.Result; + +import java.io.IOException; + +public class UsReverseGeoExample { + public static void main(String[] args) { +// We recommend storing your secret keys in environment variables. + // for Server-to-server requests, use this code: + String authId = System.getenv("SMARTY_AUTH_ID"); + String authToken = System.getenv("SMARTY_AUTH_TOKEN"); + StaticCredentials credentials = new StaticCredentials(authId, authToken); + + // for client-side requests (browser/mobile), use this code: +// String key = System.getenv("SMARTY_AUTH_WEB"); +// String hostname = System.getenv("SMARTY_AUTH_REFERER"); +// SharedCredentials credentials = new SharedCredentials(key, hostname); + + Client client = new ClientBuilder(credentials) +// .withProxy(Proxy.Type.HTTP, "localhost", 8080) // Uncomment this line to try it with a proxy + .buildUsReverseGeoClient(); + + Lookup lookup = new Lookup(40.27644, -111.65747); + + try { + client.send(lookup); + } catch (SmartyException ex) { + System.out.println(ex.getMessage()); + ex.printStackTrace(); + } catch (IOException ex) { + ex.printStackTrace(); + } + + Result[] results = lookup.getResponse().getResults(); + + System.out.printf("Results for input: (%f, %f)\n\n", lookup.getLatitude(), lookup.getLongitude()); + for (Result result : results) { + System.out.println("Latitude: " + result.getLatitude().toString()); + System.out.println("Longitude: " + result.getLongitude().toString()); + System.out.println("Distance: " + result.getDistance().toString()); + System.out.println("Street: " + result.getStreet()); + System.out.println("City: " + result.getCity()); + System.out.println("State Abbreviation: " + result.getStateAbbreviation()); + System.out.println("ZIP Code: " + result.getZipCode()); + System.out.println(); + } + } +} diff --git a/src/test/java/com/smartystreets/api/us_reverse_geo/ClientTest.java b/src/test/java/com/smartystreets/api/us_reverse_geo/ClientTest.java new file mode 100644 index 0000000..e3fa2f8 --- /dev/null +++ b/src/test/java/com/smartystreets/api/us_reverse_geo/ClientTest.java @@ -0,0 +1,33 @@ +package com.smartystreets.api.us_reverse_geo; + +import com.smartystreets.api.URLPrefixSender; +import com.smartystreets.api.mocks.FakeSerializer; +import com.smartystreets.api.mocks.RequestCapturingSender; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + + +import static org.junit.Assert.assertEquals; + +public class ClientTest { + @Rule + public final ExpectedException exception = ExpectedException.none(); + + //region [ Single Lookup ] + + @Test + public void testAddressLookupSerializedAndSentWithContext() throws Exception { + RequestCapturingSender capturingSender = new RequestCapturingSender(); + URLPrefixSender sender = new URLPrefixSender("http://localhost/lookup", capturingSender); + FakeSerializer serializer = new FakeSerializer(null); + Client client = new Client(sender, serializer); + + Lookup lookup = new Lookup(44.444444444, -111.111111111); + client.send(lookup); + + assertEquals("http://localhost/lookup?latitude=44.44444444&longitude=-111.11111111", capturingSender.getRequest().getUrl()); + } + + //endregion +} \ No newline at end of file From 1b86849c9eba1f471d87c9592c59fb8f5ceed05e Mon Sep 17 00:00:00 2001 From: Duncan Beutler Date: Thu, 17 Sep 2020 11:13:32 -0600 Subject: [PATCH 2/2] Redundant fields removed from result. --- .../api/us_reverse_geo/Result.java | 32 ------------------- .../java/examples/UsReverseGeoExample.java | 18 +++++------ .../api/us_reverse_geo/ClientTest.java | 4 +-- 3 files changed, 11 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/smartystreets/api/us_reverse_geo/Result.java b/src/main/java/com/smartystreets/api/us_reverse_geo/Result.java index 183954d..74e9ef0 100644 --- a/src/main/java/com/smartystreets/api/us_reverse_geo/Result.java +++ b/src/main/java/com/smartystreets/api/us_reverse_geo/Result.java @@ -14,24 +14,6 @@ public class Result { @Key("distance") private double distance; - @Key("latitude") - private double latitude; - - @Key("longitude") - private double longitude; - - @Key("street") - private String street; - - @Key("city") - private String city; - - @Key("state_abbreviation") - private String stateAbbreviation; - - @Key("zipcode") - private String zipcode; - //endregion public Address getAddress() { @@ -42,18 +24,4 @@ public Address getAddress() { public Double getDistance() { return this.distance; } - public Double getLatitude() { - return this.latitude; - } - - public Double getLongitude() { return this.longitude; } - - public String getStreet() { return this.street; } - - public String getCity() { return this.city; } - - public String getStateAbbreviation() { return this.stateAbbreviation; } - - public String getZipCode() { return this.zipcode; } - } diff --git a/src/main/java/examples/UsReverseGeoExample.java b/src/main/java/examples/UsReverseGeoExample.java index 0a781de..e978b06 100644 --- a/src/main/java/examples/UsReverseGeoExample.java +++ b/src/main/java/examples/UsReverseGeoExample.java @@ -4,9 +4,7 @@ import com.smartystreets.api.SharedCredentials; import com.smartystreets.api.StaticCredentials; import com.smartystreets.api.exceptions.SmartyException; -import com.smartystreets.api.us_reverse_geo.Client; -import com.smartystreets.api.us_reverse_geo.Lookup; -import com.smartystreets.api.us_reverse_geo.Result; +import com.smartystreets.api.us_reverse_geo.*; import java.io.IOException; @@ -42,13 +40,15 @@ public static void main(String[] args) { System.out.printf("Results for input: (%f, %f)\n\n", lookup.getLatitude(), lookup.getLongitude()); for (Result result : results) { - System.out.println("Latitude: " + result.getLatitude().toString()); - System.out.println("Longitude: " + result.getLongitude().toString()); + Coordinate coordinate = result.getCoordinate(); + Address address = result.getAddress(); + System.out.println("Latitude: " + coordinate.getLatitude()); + System.out.println("Longitude: " + coordinate.getLongitude()); System.out.println("Distance: " + result.getDistance().toString()); - System.out.println("Street: " + result.getStreet()); - System.out.println("City: " + result.getCity()); - System.out.println("State Abbreviation: " + result.getStateAbbreviation()); - System.out.println("ZIP Code: " + result.getZipCode()); + System.out.println("Street: " + address.getStreet()); + System.out.println("City: " + address.getCity()); + System.out.println("State Abbreviation: " + address.getStateAbbreviation()); + System.out.println("ZIP Code: " + address.getZipCode()); System.out.println(); } } diff --git a/src/test/java/com/smartystreets/api/us_reverse_geo/ClientTest.java b/src/test/java/com/smartystreets/api/us_reverse_geo/ClientTest.java index e3fa2f8..f5bcca8 100644 --- a/src/test/java/com/smartystreets/api/us_reverse_geo/ClientTest.java +++ b/src/test/java/com/smartystreets/api/us_reverse_geo/ClientTest.java @@ -23,10 +23,10 @@ public void testAddressLookupSerializedAndSentWithContext() throws Exception { FakeSerializer serializer = new FakeSerializer(null); Client client = new Client(sender, serializer); - Lookup lookup = new Lookup(44.444444444, -111.111111111); + Lookup lookup = new Lookup(44.888888888, -111.111111111); client.send(lookup); - assertEquals("http://localhost/lookup?latitude=44.44444444&longitude=-111.11111111", capturingSender.getRequest().getUrl()); + assertEquals("http://localhost/lookup?latitude=44.88888889&longitude=-111.11111111", capturingSender.getRequest().getUrl()); } //endregion