Skip to content

Commit 685498f

Browse files
committed
Added async methods and interface for SmartyStreetsClient
1 parent 29daa3f commit 685498f

File tree

4 files changed

+146
-73
lines changed

4 files changed

+146
-73
lines changed

src/Rentler.SmartyStreets/ApiClient.cs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,41 @@ public string CreateAddress(string endpoint, Dictionary<string, string> args)
4646
return url + parameters;
4747
}
4848

49-
public async Task<Stream> Get(string url)
49+
#region Depricated
50+
[Obsolete("Use Async method instead")]
51+
public Task<Stream> Get(string url)
5052
{
51-
var result = await client.GetAsync(url);
52-
return await result.Content.ReadAsStreamAsync();
53+
return GetAsync(url);
5354
}
5455

55-
public async Task<Stream> Post(string url)
56-
{
57-
var result = await client.PostAsync(url, null);
58-
return await result.Content.ReadAsStreamAsync();
59-
}
56+
[Obsolete("Use Async method instead")]
57+
public Task<Stream> Post(string url)
58+
{
59+
return this.PostAsync(url);
60+
}
61+
[Obsolete("Use Async method instead")]
62+
public Task<string> PostString(string url)
63+
{
64+
return PostStringAsync(url);
65+
}
66+
#endregion
6067

61-
public async Task<string> PostString(string url)
62-
{
63-
var result = await client.PostAsync(url, null);
64-
return await result.Content.ReadAsStringAsync();
65-
}
68+
public async Task<Stream> GetAsync(string url)
69+
{
70+
var result = await client.GetAsync(url);
71+
return await result.Content.ReadAsStreamAsync();
72+
}
73+
74+
public async Task<Stream> PostAsync(string url)
75+
{
76+
var result = await client.PostAsync(url, null);
77+
return await result.Content.ReadAsStreamAsync();
78+
}
79+
80+
public async Task<string> PostStringAsync(string url)
81+
{
82+
var result = await client.PostAsync(url, null);
83+
return await result.Content.ReadAsStringAsync();
84+
}
6685
}
6786
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Rentler.SmartyStreets
8+
{
9+
public interface ISmartyStreetsClient
10+
{
11+
Task<IEnumerable<SmartyStreetsAddress>> GetStreetAddressAsync(string street = null, string city = null, string state = null, string zipcode = null);
12+
Task<IEnumerable<SmartyStreetsCityStateZipLookup>> GetLookupAsync(string city = null, string state = null, string zip = null);
13+
}
14+
}

src/Rentler.SmartyStreets/Rentler.SmartyStreets.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Compile Include="App.cs" />
5151
<Compile Include="Extensions.cs" />
5252
<Compile Include="Hashing.cs" />
53+
<Compile Include="ISmartyStreetsClient.cs" />
5354
<Compile Include="Properties\AssemblyInfo.cs" />
5455
<Compile Include="SmartyStreetsAddress.cs" />
5556
<Compile Include="SmartyStreetsCityStateZipLookup.cs" />

src/Rentler.SmartyStreets/SmartyStreetsClient.cs

Lines changed: 99 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace Rentler.SmartyStreets
1111
/// Handles requests to SmartyStreets for street address
1212
/// and city/state/zip lookups.
1313
/// </summary>
14-
public class SmartyStreetsClient
14+
public class SmartyStreetsClient : ISmartyStreetsClient
1515
{
16-
ApiClient client;
17-
string authId;
18-
string authToken;
16+
private ApiClient _client;
17+
private string _authId;
18+
private string _authToken;
1919

2020
/// <summary>
2121
/// Initializes a new instance of the SmartyStreetsClient.
@@ -29,21 +29,21 @@ public class SmartyStreetsClient
2929
/// </summary>
3030
/// <param name="authId">Unique "auth-id" value provided by SmartyStreets.</param>
3131
/// <param name="authToken">Unique "auth-token" value.</param>
32-
public SmartyStreetsClient(
33-
string authId = null,
34-
string authToken = null)
32+
public SmartyStreetsClient(string authId = null, string authToken = null)
3533
{
36-
client = ApiClient.Instance;
37-
this.authId = authId ?? App.SmartyStreetsAuthId;
38-
this.authToken = authToken ?? App.SmartyStreetsAuthToken;
34+
_client = ApiClient.Instance;
35+
this._authId = authId ?? App.SmartyStreetsAuthId;
36+
this._authToken = authToken ?? App.SmartyStreetsAuthToken;
3937

40-
if (string.IsNullOrWhiteSpace(this.authId) || string.IsNullOrWhiteSpace(this.authToken))
38+
if (string.IsNullOrWhiteSpace(this._authId) || string.IsNullOrWhiteSpace(this._authToken))
4139
throw new System.Configuration.ConfigurationErrorsException(
4240
"Could not find one or either of the SmartyStreets auth keys.\n " +
4341
"Set them in the constructor, or an app.config or web.config.");
4442
}
4543

46-
/// <summary>
44+
45+
#region depricated
46+
/// <summary>
4747
/// Attempts to resolve a street address to a verified one.
4848
/// Makes requests to https://api.smartystreets.com/street-address.
4949
/// See http://smartystreets.com/kb/liveaddress-api/rest-endpoint for
@@ -56,28 +56,12 @@ public SmartyStreetsClient(
5656
/// <returns>An enumerable list of possible addresses. Generally, one entry
5757
/// will be returned, but results can include up to five possibles. If none are found,
5858
/// the array will be empty.</returns>
59-
public async Task<IEnumerable<SmartyStreetsAddress>> GetStreetAddress(
59+
[Obsolete("Use async method instead")]
60+
public Task<IEnumerable<SmartyStreetsAddress>> GetStreetAddress(
6061
string street = null, string city = null,
6162
string state = null, string zipcode = null)
6263
{
63-
var args = SetAuth();
64-
args["street"] = street;
65-
args["city"] = city;
66-
args["state"] = state;
67-
args["zipcode"] = zipcode;
68-
args["candidates"] = "5";
69-
70-
//var url = client.CreateAddress("street-address", args);
71-
var url = client.CreateAddress("street-address", args);
72-
var response = await client.Post(url);
73-
74-
//special cases
75-
if (response.Length == 3)
76-
return new SmartyStreetsAddress[0];
77-
78-
return JsonSerializer.DeserializeFromStream<SmartyStreetsAddress[]>(response)
79-
??
80-
new SmartyStreetsAddress[0];
64+
return GetStreetAddressAsync(street, city, state, zipcode);
8165
}
8266

8367
/// <summary>
@@ -93,40 +77,95 @@ public async Task<IEnumerable<SmartyStreetsAddress>> GetStreetAddress(
9377
/// <returns>An object with lists of matching Cities and States, along
9478
/// with any relevant zip codes that match the area. If SmartyStreets
9579
/// cannot find anything, an empty array will be returned.</returns>
96-
public async Task<IEnumerable<SmartyStreetsCityStateZipLookup>> GetLookup(
80+
[Obsolete("Use async method instead")]
81+
public Task<IEnumerable<SmartyStreetsCityStateZipLookup>> GetLookup(
9782
string city = null, string state = null,
9883
string zip = null)
9984
{
100-
var args = SetAuth();
101-
args["city"] = city;
102-
args["state"] = state;
103-
args["zip"] = zip;
104-
105-
var url = client.CreateAddress("zipcode", args);
106-
var response = await client.Post(url);
107-
var obj = JsonSerializer.DeserializeFromStream<SmartyStreetsCityStateZipLookup[]>(response)
108-
??
109-
new SmartyStreetsCityStateZipLookup[] { };
110-
111-
return obj;
85+
return GetLookupAsync(city, state, zip);
11286
}
87+
#endregion
11388

114-
/// <summary>
115-
/// Handles assignment of the keys necessary to talk to SmartyStreets.
116-
/// </summary>
117-
/// <param name="dict">A dictionary with any arguments intended to be
118-
/// passed to SmartyStreets. If an existing dictionary is not supplied,
119-
/// it will instantiate and return a new one.</param>
120-
/// <returns>A dictionary with the necessary keys to talk to SmartyStreeets.</returns>
121-
Dictionary<string, string> SetAuth(Dictionary<string, string> dict = null)
122-
{
123-
if (dict == null)
124-
dict = new Dictionary<string, string>();
89+
/// <summary>
90+
/// Attempts to resolve a street address to a verified one.
91+
/// Makes requests to https://api.smartystreets.com/street-address.
92+
/// See http://smartystreets.com/kb/liveaddress-api/rest-endpoint for
93+
/// documentation on this endpoint.
94+
/// </summary>
95+
/// <param name="street">The street address, or a single-line (freeform) address.</param>
96+
/// <param name="city">The city name.</param>
97+
/// <param name="state">The state name.</param>
98+
/// <param name="zipcode">The ZIP code.</param>
99+
/// <returns>An enumerable list of possible addresses. Generally, one entry
100+
/// will be returned, but results can include up to five possibles. If none are found,
101+
/// the array will be empty.</returns>
102+
public async Task<IEnumerable<SmartyStreetsAddress>> GetStreetAddressAsync(string street = null, string city = null, string state = null, string zipcode = null)
103+
{
104+
var args = SetAuth();
105+
args["street"] = street;
106+
args["city"] = city;
107+
args["state"] = state;
108+
args["zipcode"] = zipcode;
109+
args["candidates"] = "5";
125110

126-
dict["auth-id"] = authId;
127-
dict["auth-token"] = authToken;
111+
//var url = client.CreateAddress("street-address", args);
112+
var url = _client.CreateAddress("street-address", args);
113+
var response = await _client.PostAsync(url);
128114

129-
return dict;
130-
}
131-
}
115+
//special cases
116+
if (response.Length == 3)
117+
return new SmartyStreetsAddress[0];
118+
119+
return JsonSerializer.DeserializeFromStream<SmartyStreetsAddress[]>(response)
120+
??
121+
new SmartyStreetsAddress[0];
122+
}
123+
124+
/// <summary>
125+
/// Allows you to identify cities with ZIP codes, and vice-versa.
126+
/// It also provides approximate geo-coordinates (latitude/longitude).
127+
/// This endpoint does not support street addresses as input. See
128+
/// http://smartystreets.com/kb/liveaddress-api/zipcode-api for documentation
129+
/// on this endpoint.
130+
/// </summary>
131+
/// <param name="city">The city name.</param>
132+
/// <param name="state">The state name.</param>
133+
/// <param name="zip">The ZIP code.</param>
134+
/// <returns>An object with lists of matching Cities and States, along
135+
/// with any relevant zip codes that match the area. If SmartyStreets
136+
/// cannot find anything, an empty array will be returned.</returns>
137+
public async Task<IEnumerable<SmartyStreetsCityStateZipLookup>> GetLookupAsync(string city = null, string state = null, string zip = null)
138+
{
139+
var args = SetAuth();
140+
args["city"] = city;
141+
args["state"] = state;
142+
args["zip"] = zip;
143+
144+
var url = _client.CreateAddress("zipcode", args);
145+
var response = await _client.PostAsync(url);
146+
var obj = JsonSerializer.DeserializeFromStream<SmartyStreetsCityStateZipLookup[]>(response)
147+
??
148+
new SmartyStreetsCityStateZipLookup[] { };
149+
150+
return obj;
151+
}
152+
153+
/// <summary>
154+
/// Handles assignment of the keys necessary to talk to SmartyStreets.
155+
/// </summary>
156+
/// <param name="dict">A dictionary with any arguments intended to be
157+
/// passed to SmartyStreets. If an existing dictionary is not supplied,
158+
/// it will instantiate and return a new one.</param>
159+
/// <returns>A dictionary with the necessary keys to talk to SmartyStreeets.</returns>
160+
private Dictionary<string, string> SetAuth(Dictionary<string, string> dict = null)
161+
{
162+
if (dict == null)
163+
dict = new Dictionary<string, string>();
164+
165+
dict["auth-id"] = _authId;
166+
dict["auth-token"] = _authToken;
167+
168+
return dict;
169+
}
170+
}
132171
}

0 commit comments

Comments
 (0)