-
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.
- Loading branch information
Showing
6 changed files
with
363 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
59 changes: 59 additions & 0 deletions
59
src/main/java/com/smartystreets/api/international_autocomplete_deprecated/Candidate.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,59 @@ | ||
package com.smartystreets.api.international_autocomplete_deprecated; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* @see "https://smartystreets.com/docs/cloud/international-address-autocomplete-api#http-response" | ||
*/ | ||
public class Candidate implements Serializable { | ||
//region [ Fields ] | ||
|
||
private String street; | ||
private String locality; | ||
private String administrativeArea; | ||
private String superAdministrativeArea; | ||
private String subAdministrativeArea; | ||
private String postalCode; | ||
private String countryISO3; | ||
|
||
//region [ Fields ] | ||
|
||
//region [ Getters ] | ||
|
||
public String getStreet() { | ||
return street; | ||
} | ||
|
||
public String getLocality() { | ||
return locality; | ||
} | ||
|
||
@JsonProperty("administrative_area") | ||
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; | ||
} | ||
|
||
@JsonProperty("country_iso3") | ||
public String getCountryISO3() { | ||
return countryISO3; | ||
} | ||
|
||
//endregion | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/smartystreets/api/international_autocomplete_deprecated/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,58 @@ | ||
package com.smartystreets.api.international_autocomplete_deprecated; | ||
|
||
import com.smartystreets.api.*; | ||
import com.smartystreets.api.exceptions.SmartyException; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* This client sends lookups to the SmartyStreets US Autocomplete API, <br> | ||
* and attaches the results to the appropriate Lookup objects. | ||
*/ | ||
public class Client { | ||
private final Sender sender; | ||
private final Serializer serializer; | ||
|
||
public Client(Sender sender, Serializer serializer) { | ||
this.sender = sender; | ||
this.serializer = serializer; | ||
} | ||
|
||
public Candidate[] send(Lookup lookup) throws SmartyException, IOException, InterruptedException { | ||
if (lookup == null || lookup.getSearch() == null || lookup.getSearch().length() == 0) | ||
throw new SmartyException("Send() must be passed a Lookup with the prefix field set."); | ||
|
||
Request request = this.buildRequest(lookup); | ||
|
||
Response response = this.sender.send(request); | ||
|
||
Result result = this.serializer.deserialize(response.getPayload(), Result.class); | ||
Candidate[] candidates = result.getCandidates(); | ||
lookup.setResult(candidates); | ||
|
||
return candidates; | ||
} | ||
|
||
private Request buildRequest(Lookup lookup) { | ||
Request request = new Request(); | ||
|
||
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; | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
src/main/java/com/smartystreets/api/international_autocomplete_deprecated/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,151 @@ | ||
package com.smartystreets.api.international_autocomplete_deprecated; | ||
|
||
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" | ||
*/ | ||
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; | ||
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() { | ||
this.maxResults = MAX_RESULTS_DEFAULT; | ||
this.distance = DISTANCE_DEFAULT; | ||
this.geolocation = InternationalGeolocateType.NONE; | ||
} | ||
|
||
/** | ||
* @param search The beginning of an address | ||
*/ | ||
public Lookup(String search) { | ||
this(); | ||
this.search = search; | ||
} | ||
|
||
//endregion | ||
|
||
//region [ Getters ] | ||
|
||
public Candidate[] getResult() { | ||
return this.result; | ||
} | ||
|
||
public Candidate getResult(int index) { | ||
return this.result[index]; | ||
} | ||
|
||
public String getCountry() { | ||
return this.country; | ||
} | ||
|
||
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 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 | ||
|
||
//region [ Setters ] | ||
|
||
public void setResult(Candidate[] result) { | ||
this.result = result; | ||
} | ||
|
||
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 setAdministrativeArea(String administrativeArea) { | ||
this.administrativeArea = administrativeArea; | ||
} | ||
|
||
public void setLocality(String locality) { | ||
this.locality = locality; | ||
} | ||
|
||
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 | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/smartystreets/api/international_autocomplete_deprecated/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,15 @@ | ||
package com.smartystreets.api.international_autocomplete_deprecated; | ||
|
||
import java.io.Serializable; | ||
|
||
public class Result implements Serializable { | ||
private Candidate[] candidates; | ||
|
||
public Candidate[] getCandidates() { | ||
return this.candidates; | ||
} | ||
|
||
public Candidate getCandidate(int index) { | ||
return this.candidates[index]; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/examples/InternationalAutocompleteExampleDeprecated.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,74 @@ | ||
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.international_autocomplete_deprecated.Candidate; | ||
import com.smartystreets.api.international_autocomplete_deprecated.Lookup; | ||
import com.smartystreets.api.international_autocomplete_deprecated.Client; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
||
public class InternationalAutocompleteExampleDeprecated { | ||
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")); | ||
|
||
// for client-side requests (browser/mobile), use this code: | ||
SharedCredentials credentials = new SharedCredentials(System.getenv("SMARTY_AUTH_WEB"), System.getenv("SMARTY_AUTH_REFERER")); | ||
|
||
|
||
// The appropriate license values to be used for your subscriptions | ||
// can be found on the Subscriptions page of the account dashboard. | ||
// https://www.smartystreets.com/docs/cloud/licensing | ||
ArrayList<String> licenses = new ArrayList<>(); | ||
licenses.add("international-autocomplete-cloud"); | ||
Client client = new ClientBuilder(credentials).withLicenses(licenses).buildInternationalAutocompleteApiClientDeprecated(); | ||
Lookup lookup = new Lookup("Louis"); | ||
|
||
// Documentation for input fields can be found at: | ||
// https://smartystreets.com/docs/cloud/international-address-autocomplete-api#pro-http-request-url | ||
|
||
|
||
lookup.setCountry("FRA"); | ||
lookup.setLocality("Paris"); | ||
|
||
try { | ||
client.send(lookup); | ||
|
||
System.out.println("*** Result ***"); | ||
System.out.println(); | ||
printResult(lookup); | ||
} catch (SmartyException | IOException | InterruptedException ex) { | ||
System.out.println(ex.getMessage()); | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
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.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()); | ||
} | ||
} | ||
} | ||
|