Skip to content

Commit

Permalink
Add deprecated version of API.
Browse files Browse the repository at this point in the history
  • Loading branch information
alrwarner committed Oct 23, 2023
1 parent 35ac01c commit e9cfd87
Show file tree
Hide file tree
Showing 6 changed files with 363 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 @@ -14,6 +14,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/v2/lookup";
private final static String INTERNATIONAL_AUTOCOMPLETE_API_URL_DEPRECATED = "https://international-autocomplete.api.smarty.com/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 Expand Up @@ -143,6 +144,11 @@ public com.smartystreets.api.international_autocomplete.Client buildInternationa
return new com.smartystreets.api.international_autocomplete.Client(this.buildSender(), this.serializer);
}

public com.smartystreets.api.international_autocomplete_deprecated.Client buildInternationalAutocompleteApiClientDeprecated() {
this.ensureURLPrefixNotNull(this.INTERNATIONAL_AUTOCOMPLETE_API_URL_DEPRECATED);
return new com.smartystreets.api.international_autocomplete_deprecated.Client(this.buildSender(), this.serializer);
}

public com.smartystreets.api.us_autocomplete.Client buildUsAutocompleteApiClient() {
this.ensureURLPrefixNotNull(this.US_AUTOCOMPLETE_API_URL);
return new com.smartystreets.api.us_autocomplete.Client(this.buildSender(), this.serializer);
Expand Down
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
}
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;
}
}
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
}
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];
}
}
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());
}
}
}

0 comments on commit e9cfd87

Please sign in to comment.