Skip to content

Commit

Permalink
Merge pull request #43 from smartystreets/eric/add-secondary-endpoint
Browse files Browse the repository at this point in the history
Added Secondary Endpoint and Secondary Count Endpoint
  • Loading branch information
RyanLCox1 authored Aug 16, 2024
2 parents 3d69ea3 + 5ee1f13 commit 41fdf64
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private static void Main()
USReverseGeoExample.Run();
USEnrichmentPropertyExample.Run();
USEnrichmentGeoReferenceExample.Run();
USEnrichmentSecondaryExample.Run();
USEnrichmentUniversalExample.Run();
}
}
Expand Down
112 changes: 112 additions & 0 deletions src/examples/USEnrichmentSecondaryExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
namespace Examples
{
using System;
using System.Net;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SmartyStreets;
using SmartyStreets.USEnrichmentApi;
using System.Reflection;
using System.Text;

internal static class USEnrichmentSecondaryExample
{
public static void Run()
{
// specifies the TLS protocoll to use - this is TLS 1.2
const SecurityProtocolType tlsProtocol1_2 = (SecurityProtocolType)3072;

// var authId = "Your SmartyStreets Auth ID here";
// var authToken = "Your SmartyStreets Auth Token here";

// We recommend storing your keys in environment variables instead---it's safer!
var authId = Environment.GetEnvironmentVariable("SMARTY_AUTH_ID");
var authToken = Environment.GetEnvironmentVariable("SMARTY_AUTH_TOKEN");
ServicePointManager.SecurityProtocol = tlsProtocol1_2;

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

// 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;
var lookup = new SmartyStreets.USEnrichmentApi.Secondary.Lookup("325023201");
// Options available for Lookup
// lookup.SetEtag("HAYDKMJXHA4DKNA");

try {
results = client.SendSecondaryLookup(lookup);
}
catch (NotModifiedException ex) {
Console.WriteLine(ex.Message); // The Etag value provided represents the latest version of the requested record
}
catch (Exception ex) {
Console.WriteLine(ex.Message + ex.StackTrace);
}

if (results is not null) {
foreach (SmartyStreets.USEnrichmentApi.Secondary.Result result in results) {
PrintResult(result);
if (result.Aliases is not null) {
Console.WriteLine("Aliases: {");
foreach (SmartyStreets.USEnrichmentApi.Secondary.Aliases alias in result.Aliases) {
PrintResult(alias);
Console.WriteLine();
}
Console.WriteLine("}\n");
}
Console.WriteLine("Secondaries: {");
foreach (SmartyStreets.USEnrichmentApi.Secondary.Secondaries secondary in result.Secondaries) {
PrintResult(secondary);
Console.WriteLine();
}
Console.WriteLine("}\n");
}
}
else {
Console.WriteLine("Result was null");
}

SmartyStreets.USEnrichmentApi.Secondary.Count.Result[] countResults = null;
var countLookup = new SmartyStreets.USEnrichmentApi.Secondary.Count.Lookup("325023201");
// Options available for Lookup
// lookup.SetEtag("HAYDKMJXHA4DKNA");

try {
countResults = client.SendSecondaryCountLookup(countLookup);
}
catch (NotModifiedException ex) {
Console.WriteLine(ex.Message); // The Etag value provided represents the latest version of the requested record
}
catch (Exception ex) {
Console.WriteLine(ex.Message + ex.StackTrace);
}

if (countResults is not null) {
Console.WriteLine("Count: {");
foreach (SmartyStreets.USEnrichmentApi.Secondary.Count.Result result in countResults) {
PrintResult(result);
}
Console.WriteLine("}");
}
else {
Console.WriteLine("Result was null");
}
}


private static void PrintResult(object obj){
Type type = obj.GetType();

foreach (PropertyInfo property in type.GetProperties()) {
if (property.Name == "RootAddress") {
Console.WriteLine("Root Address: {");
PrintResult(property.GetValue(obj, null));
Console.WriteLine("}\n");
}
else if (property.GetValue(obj, null) is not null && property.Name != "Aliases" && property.Name != "Secondaries") {
Console.WriteLine($"{property.Name}: {property.GetValue(obj, null)}");
}
}
}
}
}
26 changes: 26 additions & 0 deletions src/sdk/USEnrichmentApi/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ public GeoReference.Result[] SendGeoReferenceLookup(GeoReference.Lookup lookup)
return lookup.GetResults();
}

public Secondary.Result[] SendSecondaryLookup(string smartyKey)
{
Secondary.Lookup lookup = new Secondary.Lookup(smartyKey);
Send(lookup);
return lookup.GetResults();
}

public Secondary.Result[] SendSecondaryLookup(Secondary.Lookup lookup)
{
Send(lookup);
return lookup.GetResults();
}

public Secondary.Count.Result[] SendSecondaryCountLookup(string smartyKey)
{
Secondary.Count.Lookup lookup = new Secondary.Count.Lookup(smartyKey);
Send(lookup);
return lookup.GetResults();
}

public Secondary.Count.Result[] SendSecondaryCountLookup(Secondary.Count.Lookup lookup)
{
Send(lookup);
return lookup.GetResults();
}

public byte[] SendUniversalLookup(Universal.Lookup lookup)
{
Send(lookup);
Expand Down
38 changes: 38 additions & 0 deletions src/sdk/USEnrichmentApi/Secondary/Aliases.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace SmartyStreets.USEnrichmentApi.Secondary
{
using System.Runtime.Serialization;

[DataContract]
public class Aliases
{
[DataMember(Name = "smarty_key")]
public string SmartyKey { get; set; }

[DataMember(Name = "primary_number")]
public string PrimaryNumber { get; set; }

[DataMember(Name = "street_predirection")]
public string StreetPredirection { get; set; }

[DataMember(Name = "street_name")]
public string StreetName { get; set; }

[DataMember(Name = "street_suffix")]
public string StreetSuffix { get; set; }

[DataMember(Name = "street_postdirection")]
public string StreetPostdirection { get; set; }

[DataMember(Name = "city_name")]
public string CityName { get; set; }

[DataMember(Name = "state_abbreviation")]
public string StateAbbreviation { get; set; }

[DataMember(Name = "zipcode")]
public string Zipcode { get; set; }

[DataMember(Name = "plus4_code")]
public string Plus4Code { get; set; }
}
}
31 changes: 31 additions & 0 deletions src/sdk/USEnrichmentApi/Secondary/Count/Lookup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace SmartyStreets.USEnrichmentApi.Secondary.Count
{
using System;
using System.IO;
using SmartyStreets.USEnrichmentApi.Secondary;

public class Lookup : SmartyStreets.USEnrichmentApi.Lookup
{
private Result[] results;

public Lookup(string smartyKey) : base(smartyKey, "secondary", "count")
{
}

public Result[] GetResults()
{
return results;
}

public void SetResults(Result[] results)
{
this.results = results;
}

public override void DeserializeAndSetResults(SmartyStreets.ISerializer serializer, Stream payload)
{
this.results = serializer.Deserialize<Result[]>(payload);
this.results[0].Etag = this.GetEtag();
}
}
}
17 changes: 17 additions & 0 deletions src/sdk/USEnrichmentApi/Secondary/Count/Result.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SmartyStreets.USEnrichmentApi.Secondary.Count
{
using System.Runtime.Serialization;

[DataContract]
public class Result
{
[DataMember(Name = "smarty_key")]
public string SmartyKey { get; set; }

[DataMember(Name = "etag")]
public string Etag { get; set; }

[DataMember(Name = "count")]
public string count { get; set; }
}
}
31 changes: 31 additions & 0 deletions src/sdk/USEnrichmentApi/Secondary/Lookup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace SmartyStreets.USEnrichmentApi.Secondary
{
using System;
using System.IO;
using SmartyStreets.USEnrichmentApi.Secondary;

public class Lookup : SmartyStreets.USEnrichmentApi.Lookup
{
private Result[] results;

public Lookup(string smartyKey) : base(smartyKey, "secondary", "")
{
}

public Result[] GetResults()
{
return results;
}

public void SetResults(Result[] results)
{
this.results = results;
}

public override void DeserializeAndSetResults(SmartyStreets.ISerializer serializer, Stream payload)
{
this.results = serializer.Deserialize<Result[]>(payload);
this.results[0].Etag = this.GetEtag();
}
}
}
23 changes: 23 additions & 0 deletions src/sdk/USEnrichmentApi/Secondary/Result.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace SmartyStreets.USEnrichmentApi.Secondary
{
using System.Runtime.Serialization;

[DataContract]
public class Result
{
[DataMember(Name = "smarty_key")]
public string SmartyKey { get; set; }

[DataMember(Name = "etag")]
public string Etag { get; set; }

[DataMember(Name = "root_address")]
public RootAddress RootAddress { get; set; }

[DataMember(Name = "aliases")]
public Aliases[] Aliases { get; set; }

[DataMember(Name = "secondaries")]
public Secondaries[] Secondaries { get; set; }
}
}
41 changes: 41 additions & 0 deletions src/sdk/USEnrichmentApi/Secondary/RootAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace SmartyStreets.USEnrichmentApi.Secondary
{
using System.Runtime.Serialization;

[DataContract]
public class RootAddress
{
[DataMember(Name = "secondary_count")]
public string SecondaryCount { get; set; }

[DataMember(Name = "smarty_key")]
public string SmartyKey { get; set; }

[DataMember(Name = "primary_number")]
public string PrimaryNumber { get; set; }

[DataMember(Name = "street_predirection")]
public string StreetPredirection { get; set; }

[DataMember(Name = "street_name")]
public string StreetName { get; set; }

[DataMember(Name = "street_suffix")]
public string StreetSuffix { get; set; }

[DataMember(Name = "street_postdirection")]
public string StreetPostdirection { get; set; }

[DataMember(Name = "city_name")]
public string CityName { get; set; }

[DataMember(Name = "state_abbreviation")]
public string StateAbbreviation { get; set; }

[DataMember(Name = "zipcode")]
public string Zipcode { get; set; }

[DataMember(Name = "plus4_code")]
public string Plus4Code { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/sdk/USEnrichmentApi/Secondary/Secondaries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace SmartyStreets.USEnrichmentApi.Secondary
{
using System.Runtime.Serialization;

[DataContract]
public class Secondaries
{
[DataMember(Name = "smarty_key")]
public string SmartyKey { get; set; }

[DataMember(Name = "secondary_designator")]
public string SecondaryDesignator { get; set; }

[DataMember(Name = "secondary_number")]
public string SecondaryNumber { get; set; }

[DataMember(Name = "plus4_code")]
public string Plus4Code { get; set; }
}
}

0 comments on commit 41fdf64

Please sign in to comment.