-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from smartystreets/reverse-geo
US Reverse Geocoding API added to SDK
- Loading branch information
Showing
9 changed files
with
260 additions
and
0 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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/smartystreets/api/us_reverse_geo/Address.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,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; } | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/smartystreets/api/us_reverse_geo/Client.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,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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/smartystreets/api/us_reverse_geo/Coordinate.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,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; } | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/smartystreets/api/us_reverse_geo/Lookup.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,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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/smartystreets/api/us_reverse_geo/Result.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,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; } | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/smartystreets/api/us_reverse_geo/SmartyResponse.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,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; } | ||
|
||
} |
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,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(); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/com/smartystreets/api/us_reverse_geo/ClientTest.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,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 | ||
} |