Skip to content

Commit

Permalink
Merge pull request #41 from smartystreets/acacia/intl-autocomplete-v2
Browse files Browse the repository at this point in the history
Acacia/intl autocomplete v2
  • Loading branch information
XanSmarty authored Nov 10, 2023
2 parents 9f8a851 + 5c2e6b0 commit 2baec37
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 127 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/smartystreets/api/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public class ClientBuilder {
private final static String INTERNATIONAL_STREET_API_URL = "https://international-street.api.smarty.com/verify";
private final static String INTERNATIONAL_AUTOCOMPLETE_API_URL = "https://international-autocomplete.api.smarty.com/lookup";
private final static String INTERNATIONAL_AUTOCOMPLETE_API_URL = "https://international-autocomplete.api.smarty.com/v2/lookup";
private final static String US_AUTOCOMPLETE_API_URL = "https://us-autocomplete.api.smarty.com/suggest";
private final static String US_AUTOCOMPLETE_API_PRO_URL = "https://us-autocomplete-pro.api.smarty.com/lookup";
private final static String US_EXTRACT_API_URL = "https://us-extract.api.smarty.com/";
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/smartystreets/api/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public byte[] getPayload() {
return this.payload;
}

public String getUrlPrefix() {
return this.urlPrefix;
}

public String getContentType() {
return contentType;
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/smartystreets/api/URLPrefixSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public URLPrefixSender(String urlPrefix, Sender inner) {
}

public Response send(Request request) throws IOException, SmartyException, InterruptedException {
request.setUrlPrefix(this.urlPrefix);
if(request.getUrlPrefix() != null && !request.getUrlPrefix().isEmpty()) {
request.setUrlPrefix(this.urlPrefix + request.getUrlPrefix());
} else {
request.setUrlPrefix(this.urlPrefix);
}

return this.inner.send(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public class Candidate implements Serializable {
private String street;
private String locality;
private String administrativeArea;
private String superAdministrativeArea;
private String subAdministrativeArea;
private String postalCode;
private String countryISO3;

private int entries;
private String addressText;
private String addressID;

//region [ Fields ]

//region [ Getters ]
Expand All @@ -35,16 +37,6 @@ public String getAdministrativeArea() {
return administrativeArea;
}

@JsonProperty("super_administrative_area")
public String getSuperAdministrativeArea() {
return superAdministrativeArea;
}

@JsonProperty("sub_administrative_area")
public String getSubAdministrativeArea() {
return subAdministrativeArea;
}

@JsonProperty("postal_code")
public String getPostalCode() {
return postalCode;
Expand All @@ -55,5 +47,20 @@ public String getCountryISO3() {
return countryISO3;
}

@JsonProperty("entries")
public int getEntries() {
return entries;
}

@JsonProperty("address_text")
public String getAddressText() {
return addressText;
}

@JsonProperty("address_id")
public String getAddressID() {
return addressID;
}

//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.smartystreets.api.exceptions.SmartyException;

import java.io.IOException;
import java.util.Objects;

/**
* This client sends lookups to the SmartyStreets US Autocomplete API, <br>
Expand All @@ -19,7 +20,7 @@ public Client(Sender sender, Serializer serializer) {
}

public Candidate[] send(Lookup lookup) throws SmartyException, IOException, InterruptedException {
if (lookup == null || lookup.getSearch() == null || lookup.getSearch().length() == 0)
if (lookup == null || lookup.getSearch() == null || (lookup.getSearch().isEmpty() && lookup.getAddressID().isEmpty()))
throw new SmartyException("Send() must be passed a Lookup with the prefix field set.");

Request request = this.buildRequest(lookup);
Expand All @@ -36,22 +37,15 @@ public Candidate[] send(Lookup lookup) throws SmartyException, IOException, Inte
private Request buildRequest(Lookup lookup) {
Request request = new Request();

if(lookup.getAddressID() != null && !lookup.getAddressID().isEmpty()) {
request.setUrlPrefix("/" + lookup.getAddressID());
}

request.putParameter("country", lookup.getCountry());
request.putParameter("search", lookup.getSearch());
request.putParameter("max_results", String.valueOf(lookup.getMaxResults()));
request.putParameter("distance", String.valueOf(lookup.getDistance()));
if (lookup.getGeolocation() != InternationalGeolocateType.NONE.getName()) {
request.putParameter("geolocation", lookup.getGeolocation());
}
request.putParameter("include_only_administrative_area", lookup.getAdministrativeArea());
request.putParameter("include_only_locality", lookup.getLocality());
request.putParameter("include_only_postal_code", lookup.getPostalCode());
if (lookup.getLatitude() != null) {
request.putParameter("latitude", String.valueOf(lookup.getLatitude()));
}
if (lookup.getLongitude() != null) {
request.putParameter("longitude", String.valueOf(lookup.getLongitude()));
}

return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ public class Lookup {
private Candidate[] result;
private String country;
private String search;
private String addressID;
private int maxResults;
private int distance;
private InternationalGeolocateType geolocation;
private String administrativeArea;
private String locality;
private String postalCode;
private Float latitude;
private Float longitude;

//endregion

//region [ Constructors ]
Expand All @@ -34,8 +31,6 @@ public class Lookup {
*/
public Lookup() {
this.maxResults = MAX_RESULTS_DEFAULT;
this.distance = DISTANCE_DEFAULT;
this.geolocation = InternationalGeolocateType.NONE;
}

/**
Expand Down Expand Up @@ -66,20 +61,12 @@ public String getSearch() {
return this.search;
}

public int getMaxResults() {
return this.maxResults;
}

public int getDistance() {
return this.distance;
}

public String getGeolocation() {
return this.geolocation.getName();
public String getAddressID() {
return addressID;
}

public String getAdministrativeArea() {
return this.administrativeArea;
public int getMaxResults() {
return this.maxResults;
}

public String getLocality() {
Expand All @@ -90,18 +77,9 @@ public String getPostalCode() {
return this.postalCode;
}

public Float getLatitude() {
return latitude;
}

public Float getLongitude() {
return longitude;
}

//endregion

//region [ Setters ]

public void setResult(Candidate[] result) {
this.result = result;
}
Expand All @@ -114,21 +92,12 @@ public void setSearch(String search) {
this.search = search;
}

public void setMaxResults(int maxResults) {
this.maxResults = maxResults;
}

public void setDistance(int distance) {
this.distance = distance;
public void setAddressID(String addressID) {
this.addressID = addressID;
}

public void setGeolocation(InternationalGeolocateType geolocation) {
this.geolocation = geolocation;
}


public void setAdministrativeArea(String administrativeArea) {
this.administrativeArea = administrativeArea;
public void setMaxResults(int maxResults) {
this.maxResults = maxResults;
}

public void setLocality(String locality) {
Expand All @@ -139,13 +108,5 @@ public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}

public void setLatitude(Float latitude) {
this.latitude = latitude;
}

public void setLongitude(Float longitude) {
this.longitude = longitude;
}

//endregion
}
30 changes: 12 additions & 18 deletions src/main/java/examples/InternationalAutocompleteExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class InternationalAutocompleteExample {
public static void main(String[] args) {
// We recommend storing your authentication credentials in environment variables.
// for server-to-server requests, use this code:
//StaticCredentials credentials = new StaticCredentials(System.getenv("SMARTY_AUTH_ID"), System.getenv("SMARTY_AUTH_TOKEN"));
StaticCredentials credentials = new StaticCredentials(System.getenv("SMARTY_AUTH_ID"), System.getenv("SMARTY_AUTH_TOKEN"));

// for client-side requests (browser/mobile), use this code:
SharedCredentials credentials = new SharedCredentials(System.getenv("SMARTY_AUTH_WEB"), System.getenv("SMARTY_AUTH_REFERER"));
//SharedCredentials credentials = new SharedCredentials(System.getenv("SMARTY_AUTH_WEB"), System.getenv("SMARTY_AUTH_REFERER"));


// The appropriate license values to be used for your subscriptions
Expand Down Expand Up @@ -50,23 +50,17 @@ public static void main(String[] args) {

private static void printResult(Lookup lookup) {
for (Candidate candidate : lookup.getResult()) {
System.out.print(candidate.getStreet());
System.out.print(" ");
System.out.print(candidate.getLocality());
System.out.print(" ");
System.out.print(candidate.getAdministrativeArea());
System.out.print(", ");
if (candidate.getSuperAdministrativeArea() != null && !candidate.getSuperAdministrativeArea().isEmpty()) {
System.out.print(candidate.getSuperAdministrativeArea());
System.out.print(", ");
if(candidate.getAddressText() != null) {
System.out.println("Entries: " + candidate.getEntries());
System.out.println("Address Text: " + candidate.getAddressText());
System.out.println("Address ID: " + candidate.getAddressID() + "\n");
} else {
System.out.println("Street: " + candidate.getStreet());
System.out.println("Locality: " + candidate.getLocality());
System.out.println("Administrative Area: " + candidate.getAdministrativeArea());
System.out.println("Postal Code: " + candidate.getPostalCode());
System.out.println("Country: " + candidate.getCountryISO3());
}
if (candidate.getSubAdministrativeArea() != null && !candidate.getSubAdministrativeArea().isEmpty()) {
System.out.print(candidate.getSubAdministrativeArea());
System.out.print(", ");
}
System.out.print(candidate.getPostalCode());
System.out.print(", ");
System.out.println(candidate.getCountryISO3());
}
}
}
20 changes: 15 additions & 5 deletions src/test/java/com/smartystreets/api/URLPrefixSenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@
public class URLPrefixSenderTest {

@Test
public void testProvidedURLOverridesRequestURL() throws Exception {
public void testRequestURLPresent() throws Exception {
Request request = new Request();
request.setUrlPrefix("http://www.google.com/the/path/stays");
String override = "https://smartystreets.com/the/path/is/ignored?";
request.setUrlPrefix("/jimbo");
Sender inner = new MockSender(new Response(123, null));
Sender sender = new URLPrefixSender(override, inner);
Sender sender = new URLPrefixSender("http://mysite.com/lookup", inner);

Response response = sender.send(request);

assertEquals(override, request.getUrl());
assertEquals("http://mysite.com/lookup/jimbo?", request.getUrl());
}

@Test
public void testRequestURLNotPresent() throws Exception {
Request request = new Request();
Sender inner = new MockSender(new Response(123, null));
Sender sender = new URLPrefixSender("http://mysite.com/lookup", inner);

Response response = sender.send(request);

assertEquals("http://mysite.com/lookup?", request.getUrl());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,27 @@ public class CandidateTest {
public void testFullJSONDeserialization() throws Exception {

String rawJSON = "{\n" +
"\"candidates\": [\n" +
"{\n" +
"\"street\": \"12TH AV\",\n" +
"\"locality\": \"OCEAN GROVE\",\n" +
"\"administrative_area\": \"VIC\",\n" +
"\"sub_administrative_area\": \"WHITTLESEA\"," +
"\"postal_code\": \"3226\",\n" +
"\"country_iso3\": \"AUS\"\n" +
"},\n" +
"{\n" +
"\"street\": \"MONG FAT STREET\",\n" +
"\"locality\": \"TUEN MUN\",\n" +
"\"administrative_area\": \"TUEN MUN DISTRICT\",\n" +
"\"super_administrative_area\": \"HONG KONG\",\n" +
"\"country_iso3\": \"HKG\"\n" +
"}\n" +
"]\n" +
"}";
"\"candidates\": [\n" +
"{\n" +
"\"street\": \"12TH AV\",\n" +
"\"locality\": \"OCEAN GROVE\",\n" +
"\"administrative_area\": \"VIC\",\n" +
"\"postal_code\": \"3226\",\n" +
"\"country_iso3\": \"AUS\"\n" +
"},\n" +
"{\n" +
"\"street\": \"MONG FAT STREET\",\n" +
"\"locality\": \"TUEN MUN\",\n" +
"\"administrative_area\": \"TUEN MUN DISTRICT\",\n" +
"\"country_iso3\": \"HKG\"\n" +
"},\n" +
"{\n" +
"\"entries\": 54,\n" +
"\"address_text\": \"11 Laguna Pky Brechin, ON, L0K 1B0\",\n" +
"\"address_id\": \"HB5SHA8DBQ8QKS0mED0nRykgGEctOhM2LRs\"\n" +
"}\n" +
"]\n" +
"}";

byte[] bytes = rawJSON.getBytes();

Expand All @@ -39,13 +42,14 @@ public void testFullJSONDeserialization() throws Exception {
assertEquals("12TH AV", candidates[0].getStreet());
assertEquals("OCEAN GROVE", candidates[0].getLocality());
assertEquals("VIC", candidates[0].getAdministrativeArea());
assertEquals("WHITTLESEA", candidates[0].getSubAdministrativeArea());
assertEquals("3226", candidates[0].getPostalCode());
assertEquals("AUS", candidates[0].getCountryISO3());
assertEquals("MONG FAT STREET", candidates[1].getStreet());
assertEquals("TUEN MUN", candidates[1].getLocality());
assertEquals("TUEN MUN DISTRICT", candidates[1].getAdministrativeArea());
assertEquals("HONG KONG", candidates[1].getSuperAdministrativeArea());
assertEquals("HKG", candidates[1].getCountryISO3());
assertEquals(54, candidates[2].getEntries());
assertEquals("11 Laguna Pky Brechin, ON, L0K 1B0", candidates[2].getAddressText());
assertEquals("HB5SHA8DBQ8QKS0mED0nRykgGEctOhM2LRs", candidates[2].getAddressID());
}
}
Loading

0 comments on commit 2baec37

Please sign in to comment.