Skip to content

Commit

Permalink
Merge pull request #14 from smartystreets/reverse-geo
Browse files Browse the repository at this point in the history
US Reverse Geocoding API added to SDK
  • Loading branch information
Michael Whatcott authored Sep 17, 2020
2 parents 4cb7acf + 1b86849 commit 5b045ac
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/smartystreets/api/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/smartystreets/api/us_reverse_geo/Address.java
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 src/main/java/com/smartystreets/api/us_reverse_geo/Client.java
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 src/main/java/com/smartystreets/api/us_reverse_geo/Coordinate.java
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 src/main/java/com/smartystreets/api/us_reverse_geo/Lookup.java
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 src/main/java/com/smartystreets/api/us_reverse_geo/Result.java
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; }

}
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; }

}
55 changes: 55 additions & 0 deletions src/main/java/examples/UsReverseGeoExample.java
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 src/test/java/com/smartystreets/api/us_reverse_geo/ClientTest.java
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
}

0 comments on commit 5b045ac

Please sign in to comment.