-
Notifications
You must be signed in to change notification settings - Fork 20
/
USStreetSingleAddressExample.cs
91 lines (79 loc) · 3.28 KB
/
USStreetSingleAddressExample.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
namespace Examples
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using SmartyStreets;
using SmartyStreets.USStreetApi;
internal static class USStreetSingleAddressExample
{
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;
// The appropriate license values to be used for your subscriptions
// can be found on the Subscriptions page the account dashboard.
// https://www.smartystreets.com/docs/cloud/licensing
var client = new ClientBuilder(authId, authToken).WithLicense(new List<string>{"us-core-cloud"})
//.WithCustomBaseUrl("us-street.api.smarty.com")
//.ViaProxy("http://localhost:8080", "username", "password") // uncomment this line to point to the specified proxy.
.BuildUsStreetApiClient();
// Documentation for input fields can be found at:
// https://smartystreets.com/docs/us-street-api#input-fields
var lookup = new Lookup
{
InputId = "24601", // Optional ID from your system
Addressee = "John Doe",
Street = "1600 Amphitheatre Pkwy",
Street2 = "closet under the stairs",
Secondary = "APT 2",
Urbanization = "", // Only applies to Puerto Rico addresses
City = "Mountain View",
State = "CA",
ZipCode = "21229",
CountySource = Lookup.GEOGRAPHIC,
MaxCandidates = 3,
MatchStrategy = Lookup.ENHANCED // "invalid" is the most permissive match,
// this will always return at least one result even if the address is invalid.
// Refer to the documentation for additional MatchStrategy options.
};
//uncomment the line below to add a custom parameter
//lookup.AddCustomParameter("county_source", "geographic");
try
{
client.Send(lookup);
}
catch (SmartyException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
return;
}
catch (IOException ex)
{
Console.WriteLine(ex.StackTrace);
return;
}
var candidates = lookup.Result;
if (candidates.Count == 0)
{
Console.WriteLine("No candidates. This means the address is not valid.");
return;
}
var firstCandidate = candidates[0];
Console.WriteLine("Input ID: " + firstCandidate.InputId);
Console.WriteLine("There is at least one candidate.\n If the match parameter is set to STRICT, the address is valid.\n Otherwise, check the Analysis output fields to see if the address is valid.\n");
Console.WriteLine("ZIP Code: " + firstCandidate.Components.ZipCode);
Console.WriteLine("County: " + firstCandidate.Metadata.CountyName);
Console.WriteLine("Latitude: " + firstCandidate.Metadata.Latitude);
Console.WriteLine("Longitude: " + firstCandidate.Metadata.Longitude);
}
}
}