-
Notifications
You must be signed in to change notification settings - Fork 20
/
USEnrichmentSecondaryExample.cs
152 lines (127 loc) · 6.57 KB
/
USEnrichmentSecondaryExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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;
// 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");
//uncomment the below line to add a custom parameter
//componentsLookup.AddCustomParameter("zipcode", "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) {
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;
// 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");
//uncomment the below line to add a custom parameter
//countComponentsLookup.AddCustomParameter("zipcode", "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) {
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)}");
}
}
}
}
}