Skip to content

Commit

Permalink
Add Enrichment SDK (#43)
Browse files Browse the repository at this point in the history
Add the Enrichment api to the Java SDK.
  • Loading branch information
LandonSmarty authored Nov 27, 2023
1 parent 2cd8e99 commit d4088b6
Show file tree
Hide file tree
Showing 18 changed files with 929 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/main/java/com/smartystreets/api/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ClientBuilder {
private final static String US_STREET_API_URL = "https://us-street.api.smarty.com/street-address";
private final static String US_ZIP_CODE_API_URL = "https://us-zipcode.api.smarty.com/lookup";
private final static String US_REVERSE_GEO_API_URL = "https://us-reverse-geo.api.smarty.com/lookup";
private static final String US_ENRICHMENT_API_URL = "https://us-enrichment.api.smarty.com/lookup";
private Credentials signer;
private Serializer serializer;
private Sender httpSender;
Expand Down Expand Up @@ -134,45 +135,50 @@ public ClientBuilder withLicenses(List<String> licenses) {
}

public com.smartystreets.api.international_street.Client buildInternationalStreetApiClient() {
this.ensureURLPrefixNotNull(this.INTERNATIONAL_STREET_API_URL);
this.ensureURLPrefixNotNull(INTERNATIONAL_STREET_API_URL);
return new com.smartystreets.api.international_street.Client(this.buildSender(), this.serializer);
}

public com.smartystreets.api.international_autocomplete.Client buildInternationalAutcompleteApiClient() {
this.ensureURLPrefixNotNull(this.INTERNATIONAL_AUTOCOMPLETE_API_URL);
this.ensureURLPrefixNotNull(INTERNATIONAL_AUTOCOMPLETE_API_URL);
return new com.smartystreets.api.international_autocomplete.Client(this.buildSender(), this.serializer);
}

public com.smartystreets.api.us_autocomplete.Client buildUsAutocompleteApiClient() {
this.ensureURLPrefixNotNull(this.US_AUTOCOMPLETE_API_URL);
this.ensureURLPrefixNotNull(US_AUTOCOMPLETE_API_URL);
return new com.smartystreets.api.us_autocomplete.Client(this.buildSender(), this.serializer);
}

public com.smartystreets.api.us_autocomplete_pro.Client buildUsAutocompleteProApiClient() {
this.ensureURLPrefixNotNull(this.US_AUTOCOMPLETE_API_PRO_URL);
this.ensureURLPrefixNotNull(US_AUTOCOMPLETE_API_PRO_URL);
return new com.smartystreets.api.us_autocomplete_pro.Client(this.buildSender(), this.serializer);
}

public com.smartystreets.api.us_extract.Client buildUsExtractApiClient() {
this.ensureURLPrefixNotNull(this.US_EXTRACT_API_URL);
this.ensureURLPrefixNotNull(US_EXTRACT_API_URL);
return new com.smartystreets.api.us_extract.Client(this.buildSender(), this.serializer);
}

public com.smartystreets.api.us_street.Client buildUsStreetApiClient() {
this.ensureURLPrefixNotNull(this.US_STREET_API_URL);
this.ensureURLPrefixNotNull(US_STREET_API_URL);
return new com.smartystreets.api.us_street.Client(this.buildSender(), this.serializer);
}

public com.smartystreets.api.us_zipcode.Client buildUsZipCodeApiClient() {
this.ensureURLPrefixNotNull(this.US_ZIP_CODE_API_URL);
this.ensureURLPrefixNotNull(US_ZIP_CODE_API_URL);
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);
this.ensureURLPrefixNotNull(US_REVERSE_GEO_API_URL);
return new com.smartystreets.api.us_reverse_geo.Client(this.buildSender(), this.serializer);
}

public com.smartystreets.api.us_enrichment.Client buildUsEnrichmentClient() {
this.ensureURLPrefixNotNull(US_ENRICHMENT_API_URL);
return new com.smartystreets.api.us_enrichment.Client(this.buildSender(), this.serializer);
}

private Sender buildSender() {
if (this.httpSender != null)
return this.httpSender;
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 @@ -96,6 +96,10 @@ public void setUrlPrefix(String urlPrefix) {
this.urlPrefix = urlPrefix;
}

public String getUrlPrefix() {
return urlPrefix;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/smartystreets/api/URLPrefixSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public Response send(Request request) throws IOException, SmartyException, Inter
} else {
request.setUrlPrefix(this.urlPrefix);
}

return this.inner.send(request);
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/smartystreets/api/us_enrichment/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.smartystreets.api.us_enrichment;

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 com.smartystreets.api.us_enrichment.lookup_types.Lookup;
import com.smartystreets.api.us_enrichment.lookup_types.property_financial.PropertyFinancialLookup;
import com.smartystreets.api.us_enrichment.lookup_types.property_principal.PropertyPrincipalLookup;
import com.smartystreets.api.us_enrichment.result_types.property_financial.FinancialResponse;
import com.smartystreets.api.us_enrichment.result_types.property_principal.PrincipalResponse;

import java.io.IOException;

public class Client {
private Sender sender;
private Serializer serializer;

public Client(Sender sender, Serializer serializer) {
this.sender = sender;
this.serializer = serializer;
}

public FinancialResponse[] sendPropertyFinancialLookup(String smartyKey) throws SmartyException, IOException, InterruptedException {
PropertyFinancialLookup lookup = new PropertyFinancialLookup(smartyKey);
send(lookup);
return lookup.getResults();
}

public PrincipalResponse[] sendPropertyPrincipalLookup(String smartyKey) throws SmartyException, IOException, InterruptedException {
PropertyPrincipalLookup lookup = new PropertyPrincipalLookup(smartyKey);
send(lookup);
return lookup.getResults();
}

private void send(Lookup lookup) throws IOException, SmartyException, InterruptedException {
if (lookup == null || lookup.getSmartyKey() == null || lookup.getSmartyKey().isEmpty())
throw new SmartyException("Client.send() requires a Lookup with the 'smartyKey' field set");

Request request = this.buildRequest(lookup);
Response response = this.sender.send(request);

lookup.deserializeAndSetResults(serializer, response.getPayload());
}

private Request buildRequest(Lookup lookup) {
Request request = new Request();
request.setUrlPrefix("/" + lookup.getSmartyKey() + "/" + lookup.getDatasetName() + "/" + lookup.getDataSubsetName());
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.smartystreets.api.us_enrichment.lookup_types;

import com.smartystreets.api.Serializer;

import java.io.IOException;

public abstract class Lookup {
private final String smartyKey;
private final String datasetName;
private final String dataSubsetName;

public Lookup(String smartyKey, String datasetName, String dataSubsetName) {
this.smartyKey = smartyKey;
this.datasetName = datasetName;
this.dataSubsetName = dataSubsetName;
}

public String getSmartyKey() {
return smartyKey;
}

public String getDatasetName() {
return datasetName;
}

public String getDataSubsetName() {
return dataSubsetName;
}

public abstract void deserializeAndSetResults(Serializer serializer, byte[] payload) throws IOException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.smartystreets.api.us_enrichment.lookup_types.property_financial;

import com.smartystreets.api.Serializer;
import com.smartystreets.api.us_enrichment.lookup_types.Lookup;
import com.smartystreets.api.us_enrichment.result_types.property_financial.FinancialResponse;

import java.io.IOException;

public class PropertyFinancialLookup extends Lookup {
private FinancialResponse[] results;
public PropertyFinancialLookup(String smartyKey) {
super(smartyKey, "property", "financial");
}

public FinancialResponse[] getResults() {
return results;
}

public void setResults(FinancialResponse[] results) {
this.results = results;
}

@Override
public void deserializeAndSetResults(Serializer serializer, byte[] payload) throws IOException {
this.results = serializer.deserialize(payload, FinancialResponse[].class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.smartystreets.api.us_enrichment.lookup_types.property_principal;

import com.smartystreets.api.Serializer;
import com.smartystreets.api.us_enrichment.lookup_types.Lookup;
import com.smartystreets.api.us_enrichment.result_types.property_principal.PrincipalResponse;

import java.io.IOException;

public class PropertyPrincipalLookup extends Lookup {
private PrincipalResponse[] results;
public PropertyPrincipalLookup(String smartyKey) {
super(smartyKey, "property", "principal");
}

public PrincipalResponse[] getResults() {
return results;
}

public void setResults(PrincipalResponse[] results) {
this.results = results;
}

@Override
public void deserializeAndSetResults(Serializer serializer, byte[] payload) throws IOException {
this.results = serializer.deserialize(payload, PrincipalResponse[].class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.smartystreets.api.us_enrichment.result_types;

public class Attributes extends EnrichmentToStringer {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.smartystreets.api.us_enrichment.result_types;

import java.lang.reflect.Array;
import java.lang.reflect.Field;

public class EnrichmentToStringer {
@Override
public String toString() {
Class<?> clazz = getClass();
StringBuilder sb = new StringBuilder();
sb.append(clazz.getSimpleName()).append(":\n");

while (clazz != null) {
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
try {
Object value = field.get(this);
if (value != null) {
if (value.getClass().isArray()) {
value = arrayToString(value);
}
sb.append(field.getName()).append(": ").append(value).append("\n");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
clazz = clazz.getSuperclass();
}

return sb.toString();
}

private String arrayToString(Object array) {
int length = Array.getLength(array);
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < length; i++) {
Object element = Array.get(array, i);
sb.append(element);
if (i < length - 1) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.smartystreets.api.us_enrichment.result_types;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Result extends EnrichmentToStringer {
@JsonProperty("smarty_key")
private String smartyKey;
@JsonProperty("data_set_name")
private String datasetName;
@JsonProperty("data_subset_name")
private String dataSubsetName;

public String getSmartyKey() {
return smartyKey;
}

public String getDatasetName() {
return datasetName;
}

public String getDataSubsetName() {
return dataSubsetName;
}

}

Loading

0 comments on commit d4088b6

Please sign in to comment.