Skip to content

Commit 41fdf64

Browse files
authored
Merge pull request #43 from smartystreets/eric/add-secondary-endpoint
Added Secondary Endpoint and Secondary Count Endpoint
2 parents 3d69ea3 + 5ee1f13 commit 41fdf64

File tree

10 files changed

+340
-0
lines changed

10 files changed

+340
-0
lines changed

src/examples/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ private static void Main()
1616
USReverseGeoExample.Run();
1717
USEnrichmentPropertyExample.Run();
1818
USEnrichmentGeoReferenceExample.Run();
19+
USEnrichmentSecondaryExample.Run();
1920
USEnrichmentUniversalExample.Run();
2021
}
2122
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
namespace Examples
2+
{
3+
using System;
4+
using System.Net;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using SmartyStreets;
9+
using SmartyStreets.USEnrichmentApi;
10+
using System.Reflection;
11+
using System.Text;
12+
13+
internal static class USEnrichmentSecondaryExample
14+
{
15+
public static void Run()
16+
{
17+
// specifies the TLS protocoll to use - this is TLS 1.2
18+
const SecurityProtocolType tlsProtocol1_2 = (SecurityProtocolType)3072;
19+
20+
// var authId = "Your SmartyStreets Auth ID here";
21+
// var authToken = "Your SmartyStreets Auth Token here";
22+
23+
// We recommend storing your keys in environment variables instead---it's safer!
24+
var authId = Environment.GetEnvironmentVariable("SMARTY_AUTH_ID");
25+
var authToken = Environment.GetEnvironmentVariable("SMARTY_AUTH_TOKEN");
26+
ServicePointManager.SecurityProtocol = tlsProtocol1_2;
27+
28+
var client = new ClientBuilder(authId, authToken).BuildUsEnrichmentApiClient();
29+
30+
// 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
31+
SmartyStreets.USEnrichmentApi.Secondary.Result[] results = null;
32+
var lookup = new SmartyStreets.USEnrichmentApi.Secondary.Lookup("325023201");
33+
// Options available for Lookup
34+
// lookup.SetEtag("HAYDKMJXHA4DKNA");
35+
36+
try {
37+
results = client.SendSecondaryLookup(lookup);
38+
}
39+
catch (NotModifiedException ex) {
40+
Console.WriteLine(ex.Message); // The Etag value provided represents the latest version of the requested record
41+
}
42+
catch (Exception ex) {
43+
Console.WriteLine(ex.Message + ex.StackTrace);
44+
}
45+
46+
if (results is not null) {
47+
foreach (SmartyStreets.USEnrichmentApi.Secondary.Result result in results) {
48+
PrintResult(result);
49+
if (result.Aliases is not null) {
50+
Console.WriteLine("Aliases: {");
51+
foreach (SmartyStreets.USEnrichmentApi.Secondary.Aliases alias in result.Aliases) {
52+
PrintResult(alias);
53+
Console.WriteLine();
54+
}
55+
Console.WriteLine("}\n");
56+
}
57+
Console.WriteLine("Secondaries: {");
58+
foreach (SmartyStreets.USEnrichmentApi.Secondary.Secondaries secondary in result.Secondaries) {
59+
PrintResult(secondary);
60+
Console.WriteLine();
61+
}
62+
Console.WriteLine("}\n");
63+
}
64+
}
65+
else {
66+
Console.WriteLine("Result was null");
67+
}
68+
69+
SmartyStreets.USEnrichmentApi.Secondary.Count.Result[] countResults = null;
70+
var countLookup = new SmartyStreets.USEnrichmentApi.Secondary.Count.Lookup("325023201");
71+
// Options available for Lookup
72+
// lookup.SetEtag("HAYDKMJXHA4DKNA");
73+
74+
try {
75+
countResults = client.SendSecondaryCountLookup(countLookup);
76+
}
77+
catch (NotModifiedException ex) {
78+
Console.WriteLine(ex.Message); // The Etag value provided represents the latest version of the requested record
79+
}
80+
catch (Exception ex) {
81+
Console.WriteLine(ex.Message + ex.StackTrace);
82+
}
83+
84+
if (countResults is not null) {
85+
Console.WriteLine("Count: {");
86+
foreach (SmartyStreets.USEnrichmentApi.Secondary.Count.Result result in countResults) {
87+
PrintResult(result);
88+
}
89+
Console.WriteLine("}");
90+
}
91+
else {
92+
Console.WriteLine("Result was null");
93+
}
94+
}
95+
96+
97+
private static void PrintResult(object obj){
98+
Type type = obj.GetType();
99+
100+
foreach (PropertyInfo property in type.GetProperties()) {
101+
if (property.Name == "RootAddress") {
102+
Console.WriteLine("Root Address: {");
103+
PrintResult(property.GetValue(obj, null));
104+
Console.WriteLine("}\n");
105+
}
106+
else if (property.GetValue(obj, null) is not null && property.Name != "Aliases" && property.Name != "Secondaries") {
107+
Console.WriteLine($"{property.Name}: {property.GetValue(obj, null)}");
108+
}
109+
}
110+
}
111+
}
112+
}

src/sdk/USEnrichmentApi/Client.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@ public GeoReference.Result[] SendGeoReferenceLookup(GeoReference.Lookup lookup)
5353
return lookup.GetResults();
5454
}
5555

56+
public Secondary.Result[] SendSecondaryLookup(string smartyKey)
57+
{
58+
Secondary.Lookup lookup = new Secondary.Lookup(smartyKey);
59+
Send(lookup);
60+
return lookup.GetResults();
61+
}
62+
63+
public Secondary.Result[] SendSecondaryLookup(Secondary.Lookup lookup)
64+
{
65+
Send(lookup);
66+
return lookup.GetResults();
67+
}
68+
69+
public Secondary.Count.Result[] SendSecondaryCountLookup(string smartyKey)
70+
{
71+
Secondary.Count.Lookup lookup = new Secondary.Count.Lookup(smartyKey);
72+
Send(lookup);
73+
return lookup.GetResults();
74+
}
75+
76+
public Secondary.Count.Result[] SendSecondaryCountLookup(Secondary.Count.Lookup lookup)
77+
{
78+
Send(lookup);
79+
return lookup.GetResults();
80+
}
81+
5682
public byte[] SendUniversalLookup(Universal.Lookup lookup)
5783
{
5884
Send(lookup);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace SmartyStreets.USEnrichmentApi.Secondary
2+
{
3+
using System.Runtime.Serialization;
4+
5+
[DataContract]
6+
public class Aliases
7+
{
8+
[DataMember(Name = "smarty_key")]
9+
public string SmartyKey { get; set; }
10+
11+
[DataMember(Name = "primary_number")]
12+
public string PrimaryNumber { get; set; }
13+
14+
[DataMember(Name = "street_predirection")]
15+
public string StreetPredirection { get; set; }
16+
17+
[DataMember(Name = "street_name")]
18+
public string StreetName { get; set; }
19+
20+
[DataMember(Name = "street_suffix")]
21+
public string StreetSuffix { get; set; }
22+
23+
[DataMember(Name = "street_postdirection")]
24+
public string StreetPostdirection { get; set; }
25+
26+
[DataMember(Name = "city_name")]
27+
public string CityName { get; set; }
28+
29+
[DataMember(Name = "state_abbreviation")]
30+
public string StateAbbreviation { get; set; }
31+
32+
[DataMember(Name = "zipcode")]
33+
public string Zipcode { get; set; }
34+
35+
[DataMember(Name = "plus4_code")]
36+
public string Plus4Code { get; set; }
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace SmartyStreets.USEnrichmentApi.Secondary.Count
2+
{
3+
using System;
4+
using System.IO;
5+
using SmartyStreets.USEnrichmentApi.Secondary;
6+
7+
public class Lookup : SmartyStreets.USEnrichmentApi.Lookup
8+
{
9+
private Result[] results;
10+
11+
public Lookup(string smartyKey) : base(smartyKey, "secondary", "count")
12+
{
13+
}
14+
15+
public Result[] GetResults()
16+
{
17+
return results;
18+
}
19+
20+
public void SetResults(Result[] results)
21+
{
22+
this.results = results;
23+
}
24+
25+
public override void DeserializeAndSetResults(SmartyStreets.ISerializer serializer, Stream payload)
26+
{
27+
this.results = serializer.Deserialize<Result[]>(payload);
28+
this.results[0].Etag = this.GetEtag();
29+
}
30+
}
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace SmartyStreets.USEnrichmentApi.Secondary.Count
2+
{
3+
using System.Runtime.Serialization;
4+
5+
[DataContract]
6+
public class Result
7+
{
8+
[DataMember(Name = "smarty_key")]
9+
public string SmartyKey { get; set; }
10+
11+
[DataMember(Name = "etag")]
12+
public string Etag { get; set; }
13+
14+
[DataMember(Name = "count")]
15+
public string count { get; set; }
16+
}
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace SmartyStreets.USEnrichmentApi.Secondary
2+
{
3+
using System;
4+
using System.IO;
5+
using SmartyStreets.USEnrichmentApi.Secondary;
6+
7+
public class Lookup : SmartyStreets.USEnrichmentApi.Lookup
8+
{
9+
private Result[] results;
10+
11+
public Lookup(string smartyKey) : base(smartyKey, "secondary", "")
12+
{
13+
}
14+
15+
public Result[] GetResults()
16+
{
17+
return results;
18+
}
19+
20+
public void SetResults(Result[] results)
21+
{
22+
this.results = results;
23+
}
24+
25+
public override void DeserializeAndSetResults(SmartyStreets.ISerializer serializer, Stream payload)
26+
{
27+
this.results = serializer.Deserialize<Result[]>(payload);
28+
this.results[0].Etag = this.GetEtag();
29+
}
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace SmartyStreets.USEnrichmentApi.Secondary
2+
{
3+
using System.Runtime.Serialization;
4+
5+
[DataContract]
6+
public class Result
7+
{
8+
[DataMember(Name = "smarty_key")]
9+
public string SmartyKey { get; set; }
10+
11+
[DataMember(Name = "etag")]
12+
public string Etag { get; set; }
13+
14+
[DataMember(Name = "root_address")]
15+
public RootAddress RootAddress { get; set; }
16+
17+
[DataMember(Name = "aliases")]
18+
public Aliases[] Aliases { get; set; }
19+
20+
[DataMember(Name = "secondaries")]
21+
public Secondaries[] Secondaries { get; set; }
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace SmartyStreets.USEnrichmentApi.Secondary
2+
{
3+
using System.Runtime.Serialization;
4+
5+
[DataContract]
6+
public class RootAddress
7+
{
8+
[DataMember(Name = "secondary_count")]
9+
public string SecondaryCount { get; set; }
10+
11+
[DataMember(Name = "smarty_key")]
12+
public string SmartyKey { get; set; }
13+
14+
[DataMember(Name = "primary_number")]
15+
public string PrimaryNumber { get; set; }
16+
17+
[DataMember(Name = "street_predirection")]
18+
public string StreetPredirection { get; set; }
19+
20+
[DataMember(Name = "street_name")]
21+
public string StreetName { get; set; }
22+
23+
[DataMember(Name = "street_suffix")]
24+
public string StreetSuffix { get; set; }
25+
26+
[DataMember(Name = "street_postdirection")]
27+
public string StreetPostdirection { get; set; }
28+
29+
[DataMember(Name = "city_name")]
30+
public string CityName { get; set; }
31+
32+
[DataMember(Name = "state_abbreviation")]
33+
public string StateAbbreviation { get; set; }
34+
35+
[DataMember(Name = "zipcode")]
36+
public string Zipcode { get; set; }
37+
38+
[DataMember(Name = "plus4_code")]
39+
public string Plus4Code { get; set; }
40+
}
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace SmartyStreets.USEnrichmentApi.Secondary
2+
{
3+
using System.Runtime.Serialization;
4+
5+
[DataContract]
6+
public class Secondaries
7+
{
8+
[DataMember(Name = "smarty_key")]
9+
public string SmartyKey { get; set; }
10+
11+
[DataMember(Name = "secondary_designator")]
12+
public string SecondaryDesignator { get; set; }
13+
14+
[DataMember(Name = "secondary_number")]
15+
public string SecondaryNumber { get; set; }
16+
17+
[DataMember(Name = "plus4_code")]
18+
public string Plus4Code { get; set; }
19+
}
20+
}

0 commit comments

Comments
 (0)