Skip to content

Commit

Permalink
Added Address Search Feature for the US Address Enrichment API
Browse files Browse the repository at this point in the history
Modified Enrichment Tests to include all datasets and the Address Search Feature
  • Loading branch information
Eric Devenport authored and Eric Devenport committed Sep 27, 2024
1 parent 41fdf64 commit d5aa054
Show file tree
Hide file tree
Showing 13 changed files with 452 additions and 35 deletions.
24 changes: 21 additions & 3 deletions src/examples/USEnrichmentGeoReferenceExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,29 @@ public static void Run()
var client = new ClientBuilder(authId, authToken).BuildUsEnrichmentApiClient();

SmartyStreets.USEnrichmentApi.GeoReference.Result[] results = null;
var lookup = new SmartyStreets.USEnrichmentApi.GeoReference.Lookup("1682393594");


// Create a lookup with a smarty key using the line below
var lookup = new SmartyStreets.USEnrichmentApi.GeoReference.Lookup("325023201");

// Create a lookup with address components using the lines below
var componentsLookup = new SmartyStreets.USEnrichmentApi.GeoReference.Lookup();
componentsLookup.SetStreet("56 Union Ave");
componentsLookup.SetCity("Somerville");
componentsLookup.SetState("NJ");
componentsLookup.SetZipcode("08876");

// Create a lookup with a single line address using the line below
var freeformLookup = new SmartyStreets.USEnrichmentApi.GeoReference.Lookup();
freeformLookup.SetFreeform("56 Union Ave Somerville NJ 08876");

// Options available for the GeoReference Lookup
// lookup.SetEtag("GEZTSMZYHE3DMNA");
// lookup.SetEtag("GEZDSNBUHE3DEMQ");

try {
// results = client.SendGeoReferenceLookup("1682393594"); // simple call with just a SmartyKey
// results = client.SendGeoReferenceLookup("325023201"); // simple call with just a SmartyKey

// Send a lookup using the line below
results = client.SendGeoReferenceLookup(lookup); // more flexible call to set other lookup options
}
catch (NotModifiedException ex) {
Expand Down
45 changes: 39 additions & 6 deletions src/examples/USEnrichmentPropertyExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,31 @@ public static void Run()


SmartyStreets.USEnrichmentApi.Property.Principal.Result[] results = null;
var lookup = new SmartyStreets.USEnrichmentApi.Property.Principal.Lookup("1682393594");

// Create a lookup with a smarty key using the line below
var lookup = new SmartyStreets.USEnrichmentApi.Property.Principal.Lookup("325023201");

// Create a lookup with address components using the lines below
var componentsLookup = new SmartyStreets.USEnrichmentApi.Property.Principal.Lookup();
componentsLookup.SetStreet("56 Union Ave");
componentsLookup.SetCity("Somerville");
componentsLookup.SetState("NJ");
componentsLookup.SetZipcode("08876");

// Create a lookup with a single line address using the line below
var freeformLookup = new SmartyStreets.USEnrichmentApi.Property.Principal.Lookup();
freeformLookup.SetFreeform("56 Union Ave Somerville NJ 08876");

// See the US Enrichment API documenation for available lookup properties https://www.smarty.com/docs/cloud/us-address-enrichment-api#http-request-input-fields
// Options available for the Property Lookup
// lookup.SetEtag("GU4TINZRHA4TQMY");
// lookup.SetEtag("AIDAIAQCAIEQKAIC");
// lookup.SetIncludeFields("assessed_value,assessor_last_update");
// lookup.SetExcludeFields("tax_fiscal_year,tax_jurisdiction");

try {
// results = client.SendPropertyPrincipalLookup("1682393594"); // simple call with just a SmartyKey
// results = client.SendPropertyPrincipalLookup("325023201"); // simple call with just a SmartyKey

// Send a lookup using the line below
results = client.SendPropertyPrincipalLookup(lookup); // more flexible call to set other lookup options
}
catch (NotModifiedException ex) {
Expand All @@ -55,13 +72,29 @@ public static void Run()
}

SmartyStreets.USEnrichmentApi.Property.Financial.Result[] financialResults = null;
var financialLookup = new SmartyStreets.USEnrichmentApi.Property.Financial.Lookup("1682393594");

// Create a lookup with a smarty key using the line below
var financialLookup = new SmartyStreets.USEnrichmentApi.Property.Financial.Lookup("325023201");

// Create a lookup with address components using the lines below
var financialComponentsLookup = new SmartyStreets.USEnrichmentApi.Property.Financial.Lookup();
financialComponentsLookup.SetStreet("56 Union Ave");
financialComponentsLookup.SetCity("Somerville");
financialComponentsLookup.SetState("NJ");
financialComponentsLookup.SetZipcode("08876");

// Create a lookup with a single line address using the line below
var financialFreeformLookup = new SmartyStreets.USEnrichmentApi.Property.Financial.Lookup();
financialFreeformLookup.SetFreeform("56 Union Ave Somerville NJ 08876");

// Options available for the Property Lookup
// financialLookup.SetEtag("GU4TINZRHA4TQMY");
// financialLookup.SetEtag("AIDAIAQCAIEQKAIC");
// financialLookup.SetIncludeFields("assessed_value,assessor_last_update");
// financialLookup.SetExcludeFields("tax_fiscal_year,tax_jurisdiction");
try {
// financialResults = client.SendPropertyFinancialLookup("1682393594"); // simple call with just a SmartyKey
// financialResults = client.SendPropertyFinancialLookup("325023201"); // simple call with just a SmartyKey

// Send a lookup using the line below
financialResults = client.SendPropertyFinancialLookup(financialLookup); // more flexible call to set other lookup options
}
catch (NotModifiedException ex) {
Expand Down
34 changes: 34 additions & 0 deletions src/examples/USEnrichmentSecondaryExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,28 @@ public static void Run()

// See the US Enrichment API documenation for all available datasets and data subsets https://www.smarty.com/docs/cloud/us-address-enrichment-api#data-sets
SmartyStreets.USEnrichmentApi.Secondary.Result[] results = null;

// Create a lookup with a smarty key using the line below
var lookup = new SmartyStreets.USEnrichmentApi.Secondary.Lookup("325023201");

// Create a lookup with address components using the lines below
var componentsLookup = new SmartyStreets.USEnrichmentApi.Secondary.Lookup();
componentsLookup.SetStreet("56 Union Ave");
componentsLookup.SetCity("Somerville");
componentsLookup.SetState("NJ");
componentsLookup.SetZipcode("08876");

// Create a lookup with a single line address using the line below
var freeformLookup = new SmartyStreets.USEnrichmentApi.Secondary.Lookup();
freeformLookup.SetFreeform("56 Union Ave Somerville NJ 08876");

// Options available for Lookup
// lookup.SetEtag("HAYDKMJXHA4DKNA");

try {
// results = client.SendSecondaryLookup("325023201"); // simple call with just a SmartyKey

// Send a lookup using the line below
results = client.SendSecondaryLookup(lookup);
}
catch (NotModifiedException ex) {
Expand Down Expand Up @@ -67,11 +84,28 @@ public static void Run()
}

SmartyStreets.USEnrichmentApi.Secondary.Count.Result[] countResults = null;

// Create a lookup with a smarty key using the line below
var countLookup = new SmartyStreets.USEnrichmentApi.Secondary.Count.Lookup("325023201");

// Create a lookup with address components using the lines below
var countComponentsLookup = new SmartyStreets.USEnrichmentApi.Secondary.Lookup();
countComponentsLookup.SetStreet("56 Union Ave");
countComponentsLookup.SetCity("Somerville");
countComponentsLookup.SetState("NJ");
countComponentsLookup.SetZipcode("08876");

// Create a lookup with a single line address using the line below
var countFreeformLookup = new SmartyStreets.USEnrichmentApi.Secondary.Count.Lookup();
countFreeformLookup.SetFreeform("56 Union Ave Somerville NJ 08876");

// Options available for Lookup
// lookup.SetEtag("HAYDKMJXHA4DKNA");

try {
// results = client.SendSecondaryCountLookup("325023201"); // simple call with just a SmartyKey

// Send a lookup using the line below
countResults = client.SendSecondaryCountLookup(countLookup);
}
catch (NotModifiedException ex) {
Expand Down
27 changes: 24 additions & 3 deletions src/examples/USEnrichmentUniversalExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,37 @@ public static void Run()
ServicePointManager.SecurityProtocol = tlsProtocol1_2;

var client = new ClientBuilder(authId, authToken).BuildUsEnrichmentApiClient();

byte[] results = null;
// See the US Enrichment API documenation for all available datasets and data subsets https://www.smarty.com/docs/cloud/us-address-enrichment-api#data-sets
var lookup = new SmartyStreets.USEnrichmentApi.Universal.Lookup("1682393594","property","principal");

// Create a lookup with a smarty key using the line below
var lookup = new SmartyStreets.USEnrichmentApi.Universal.Lookup("325023201","property","principal");

// Create a lookup with address components using the lines below
var componentsLookup = new SmartyStreets.USEnrichmentApi.Universal.Lookup();
componentsLookup.SetDatasetName("property");
componentsLookup.SetDataSubsetName("principal");
componentsLookup.SetStreet("56 Union Ave");
componentsLookup.SetCity("Somerville");
componentsLookup.SetState("NJ");
componentsLookup.SetZipcode("08876");

// Create a lookup with a single line address using the lines below
var freeformLookup = new SmartyStreets.USEnrichmentApi.Universal.Lookup();
freeformLookup.SetDatasetName("property");
freeformLookup.SetDataSubsetName("principal");
freeformLookup.SetFreeform("56 Union Ave Somerville NJ 08876");

// Options available for Lookup
// lookup.SetEtag("GU4TINZRHA4TQMY");
// lookup.SetEtag("AIDAIAQCAIEQKAIC");
// lookup.SetIncludeFields("assessed_value,assessor_last_update"); // applicable to Property lookups only
// lookup.SetExcludeFields("tax_fiscal_year,tax_jurisdiction"); // applicable to Property lookups only

try {
// results = client.SendUniversalLookup("325023201", "property", "principal"); // simple call with just a SmartyKey and Dataset info

// Send a lookup using the line below
results = client.SendUniversalLookup(lookup);
}
catch (NotModifiedException ex) {
Expand Down
35 changes: 30 additions & 5 deletions src/sdk/USEnrichmentApi/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public byte[] SendUniversalLookup(Universal.Lookup lookup)

private void Send(Lookup lookup)
{
if (lookup == null || string.IsNullOrEmpty(lookup.GetSmartyKey()))
throw new SmartyStreets.SmartyException("Client.Send() requires a Lookup with the 'smartyKey' field set");
if (lookup == null || (string.IsNullOrEmpty(lookup.GetSmartyKey()) && string.IsNullOrEmpty(lookup.GetStreet()) && string.IsNullOrEmpty(lookup.GetFreeform())))
throw new SmartyStreets.SmartyException("Client.Send() requires a Lookup with the 'smartyKey', 'street', or 'freeform' field set");
Request request = BuildRequest(lookup);
Response response = this.sender.Send(request);
foreach(var entry in response.HeaderInfo) {
Expand All @@ -98,6 +98,9 @@ private void Send(Lookup lookup)
}
if (response.Payload != null){
using (var payloadStream = new MemoryStream(response.Payload)){
if (serializer == null) {
Console.WriteLine("true");
}
lookup.DeserializeAndSetResults(serializer, payloadStream);
}
}
Expand All @@ -108,10 +111,18 @@ private SmartyStreets.Request BuildRequest(Lookup lookup)
SmartyStreets.Request request = new SmartyStreets.Request();

// some datasets have no data subset
if (lookup.GetDataSubsetName() == "") {
request.SetUrlComponents("/" + lookup.GetSmartyKey() + "/" + lookup.GetDatasetName());
if (string.IsNullOrEmpty(lookup.GetSmartyKey())) {
if (lookup.GetDataSubsetName() == "") {
request.SetUrlComponents("/search/" + lookup.GetDatasetName());
} else {
request.SetUrlComponents("/search/" + lookup.GetDatasetName() + "/" + lookup.GetDataSubsetName());
}
} else {
if (lookup.GetDataSubsetName() == "") {
request.SetUrlComponents("/" + lookup.GetSmartyKey() + "/" + lookup.GetDatasetName());
} else {
request.SetUrlComponents("/" + lookup.GetSmartyKey() + "/" + lookup.GetDatasetName() + "/" + lookup.GetDataSubsetName());
}
}

if (lookup.GetIncludeFields() != null) {
Expand All @@ -120,7 +131,21 @@ private SmartyStreets.Request BuildRequest(Lookup lookup)
if (lookup.GetExcludeFields() != null) {
request.SetParameter("exclude", lookup.GetExcludeFields());
}

if (lookup.GetFreeform() != null) {
request.SetParameter("freeform", lookup.GetFreeform());
}
if (lookup.GetStreet() != null) {
request.SetParameter("street", lookup.GetStreet());
}
if (lookup.GetCity() != null) {
request.SetParameter("city", lookup.GetCity());
}
if (lookup.GetState() != null) {
request.SetParameter("state", lookup.GetState());
}
if (lookup.GetZipcode() != null) {
request.SetParameter("zipcode", lookup.GetZipcode());
}
if (lookup.GetEtag() != null) {
request.SetHeader("Etag", lookup.GetEtag());
}
Expand Down
6 changes: 4 additions & 2 deletions src/sdk/USEnrichmentApi/GeoReference/Lookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Lookup : SmartyStreets.USEnrichmentApi.Lookup
{
private Result[] results;

public Lookup(string smartyKey) : base(smartyKey, "geo-reference", "")
public Lookup(string smartyKey = null) : base(smartyKey, "geo-reference", "")
{
}

Expand All @@ -25,7 +25,9 @@ public void SetResults(Result[] results)
public override void DeserializeAndSetResults(SmartyStreets.ISerializer serializer, Stream payload)
{
this.results = serializer.Deserialize<Result[]>(payload);
this.results[0].Etag = this.GetEtag();
if (this.results != null) {
this.results[0].Etag = this.GetEtag();
}
}
}
}
84 changes: 80 additions & 4 deletions src/sdk/USEnrichmentApi/Lookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ namespace SmartyStreets.USEnrichmentApi

public abstract class Lookup
{
private readonly string smartyKey;
private readonly string datasetName;
private readonly string dataSubsetName;
private string smartyKey;
private string datasetName;
private string dataSubsetName;
private string freeform;
private string street;
private string city;
private string state;
private string zipcode;
private string includeFields;
private string excludeFields;
private string eTag;

public Lookup(string smartyKey, string datasetName, string dataSubsetName)
public Lookup(string smartyKey = null, string datasetName = null, string dataSubsetName = null, string freeform = null, string street = null,
string city = null, string state = null, string zipcode = null)
{
this.smartyKey = smartyKey;
this.datasetName = datasetName;
this.dataSubsetName = dataSubsetName;
this.freeform = freeform;
this.street = street;
this.city = city;
this.state = state;
this.zipcode = zipcode;
}

public string GetSmartyKey()
Expand All @@ -34,6 +45,31 @@ public string GetDataSubsetName()
return dataSubsetName;
}

public string GetFreeform()
{
return freeform;
}

public string GetStreet()
{
return street;
}

public string GetCity()
{
return city;
}

public string GetState()
{
return state;
}

public string GetZipcode()
{
return zipcode;
}

public string GetIncludeFields()
{
return includeFields;
Expand Down Expand Up @@ -64,6 +100,46 @@ public void SetEtag(string eTag)
this.eTag = eTag;
}

public void SetSmartyKey(string smartyKey)
{
this.smartyKey = smartyKey;
}

public void SetDatasetName(string datasetName)
{
this.datasetName = datasetName;
}

public void SetDataSubsetName(string dataSubsetName)
{
this.dataSubsetName = dataSubsetName;
}

public void SetFreeform(string freeform)
{
this.freeform = freeform;
}

public void SetStreet(string street)
{
this.street = street;
}

public void SetCity(string city)
{
this.city = city;
}

public void SetState(string state)
{
this.state = state;
}

public void SetZipcode(string zipcode)
{
this.zipcode = zipcode;
}

public abstract void DeserializeAndSetResults(SmartyStreets.ISerializer serializer, Stream payload);
}

Expand Down
Loading

0 comments on commit d5aa054

Please sign in to comment.