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..74e9ef0 --- /dev/null +++ b/src/main/java/com/smartystreets/api/us_reverse_geo/Result.java @@ -0,0 +1,27 @@ +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; + + //endregion + + public Address getAddress() { + return this.address; + } + + public Coordinate getCoordinate() { return this.coordinate; } + + public Double getDistance() { return this.distance; } + +} 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..e978b06 --- /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.*; + +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) { + 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: " + 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 new file mode 100644 index 0000000..f5bcca8 --- /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.888888888, -111.111111111); + client.send(lookup); + + assertEquals("http://localhost/lookup?latitude=44.88888889&longitude=-111.11111111", capturingSender.getRequest().getUrl()); + } + + //endregion +} \ No newline at end of file