Skip to content

Commit

Permalink
Add new fields to the international autocomplete api.
Browse files Browse the repository at this point in the history
Update to the latest jackson-databind.
  • Loading branch information
RyanLCox1 committed Jan 13, 2023
1 parent 4b6454e commit 1b07859
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0</version>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.smartystreets.api;

public enum InternationalGeolocateType {

ADMIN_AREA("adminarea"), LOCALITY("locality"), POSTAL_CODE("postalcode"), GEOCODES("geocodes"), NONE("");
private final String name;

InternationalGeolocateType(String name){
this.name = name;
}

public String getName(){
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,20 @@ private Request buildRequest(Lookup lookup) {

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
@@ -1,28 +1,42 @@
package com.smartystreets.api.international_autocomplete;

import com.smartystreets.api.InternationalGeolocateType;

/**
* In addition to holding all of the input data for this lookup, this class also<br>
* will contain the result of the lookup after it comes back from the API.
* @see "https://smartystreets.com/docs/cloud/international-address-autocomplete-api#http-request-input-fields"
* will contain the result of the lookup after it comes back from the API.
*
* @see "https://smartystreets.com/docs/cloud/international-address-autocomplete-api#http-request-input-fields"
*/
public class Lookup {
static final int MAX_RESULTS_DEFAULT = 5;
static final int DISTANCE_DEFAULT = 5;

//region [ Fields ]

private Candidate[] result;
private String country;
private String search;
private int maxResults;
private int distance;
private InternationalGeolocateType geolocation;
private String administrativeArea;
private String locality;
private String postalCode;

//endregion
private Float latitude;
private Float longitude;
//endregion

//region [ Constructors ]

/**
* If you use this constructor, don't forget to set the <b>prefix</b>. It is required.
*/
public Lookup() {}
public Lookup() {
this.maxResults = MAX_RESULTS_DEFAULT;
this.distance = DISTANCE_DEFAULT;
this.geolocation = InternationalGeolocateType.NONE;
}

/**
* @param search The beginning of an address
Expand All @@ -44,15 +58,45 @@ public Candidate getResult(int index) {
return this.result[index];
}

public String getCountry() { return this.country; }
public String getCountry() {
return this.country;
}

public String getSearch() { return this.search; }
public String getSearch() {
return this.search;
}

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

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

public String getPostalCode() { return this.postalCode; }
public String getGeolocation() {
return this.geolocation.getName();
}

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

public String getLocality() {
return this.locality;
}

public String getPostalCode() {
return this.postalCode;
}

public Float getLatitude() {
return latitude;
}

public Float getLongitude() {
return longitude;
}

//endregion

Expand All @@ -62,15 +106,46 @@ public void setResult(Candidate[] result) {
this.result = result;
}

public void setCountry(String country) { this.country = country; }
public void setCountry(String country) {
this.country = country;
}

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 setGeolocation(InternationalGeolocateType geolocation) {
this.geolocation = geolocation;
}

public void setSearch(String search) { this.search = search; }

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

public void setLocality(String locality) { this.locality = locality; }
public void setLocality(String locality) {
this.locality = locality;
}

public void setPostalCode(String postalCode) { this.postalCode = postalCode; }
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
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.smartystreets.api.international_autocomplete;

import com.smartystreets.api.InternationalGeolocateType;
import com.smartystreets.api.Response;
import com.smartystreets.api.URLPrefixSender;
import com.smartystreets.api.mocks.FakeDeserializer;
Expand All @@ -23,7 +24,26 @@ public void testSendingSinglePrefixOnlyLookup() throws Exception {

client.send(new Lookup("1"));

assertEquals("http://localhost/?search=1", capturingSender.getRequest().getUrl());
assertEquals("http://localhost/?search=1&max_results=5&distance=5", capturingSender.getRequest().getUrl());
}

@Test
public void testSendingSinglePartiallyPopulatedLookup() throws Exception {
RequestCapturingSender capturingSender = new RequestCapturingSender();
URLPrefixSender sender = new URLPrefixSender("http://localhost/", capturingSender);
FakeSerializer serializer = new FakeSerializer(new Result());
Client client = new Client(sender, serializer);
String expectedURL = "http://localhost/?country=1&search=2&max_results=5&distance=5&include_only_administrative_area=3&include_only_locality=4&include_only_postal_code=5";
Lookup lookup = new Lookup();
lookup.setCountry("1");
lookup.setSearch("2");
lookup.setAdministrativeArea("3");
lookup.setLocality("4");
lookup.setPostalCode("5");

client.send(lookup);

assertEquals(expectedURL, capturingSender.getRequest().getUrl());
}

@Test
Expand All @@ -32,13 +52,18 @@ public void testSendingSingleFullyPopulatedLookup() throws Exception {
URLPrefixSender sender = new URLPrefixSender("http://localhost/", capturingSender);
FakeSerializer serializer = new FakeSerializer(new Result());
Client client = new Client(sender, serializer);
String expectedURL = "http://localhost/?country=1&search=2&include_only_administrative_area=3&include_only_locality=4&include_only_postal_code=5";
String expectedURL = "http://localhost/?country=1&search=2&max_results=10&distance=8&geolocation=postalcode&include_only_administrative_area=3&include_only_locality=4&include_only_postal_code=5&latitude=10.1&longitude=11.2";
Lookup lookup = new Lookup();
lookup.setCountry("1");
lookup.setSearch("2");
lookup.setMaxResults(10);
lookup.setDistance(8);
lookup.setGeolocation(InternationalGeolocateType.POSTAL_CODE);
lookup.setAdministrativeArea("3");
lookup.setLocality("4");
lookup.setPostalCode("5");
lookup.setLatitude(10.1f);
lookup.setLongitude(11.2f);

client.send(lookup);

Expand Down

0 comments on commit 1b07859

Please sign in to comment.