Skip to content

Commit

Permalink
Merge pull request #39 from smartystreets/iac-v2
Browse files Browse the repository at this point in the history
Support iac-v2
  • Loading branch information
XanSmarty authored Nov 10, 2023
2 parents 423e842 + a7f97dd commit c9f02b1
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 85 deletions.
20 changes: 14 additions & 6 deletions src/examples/InternationalAutocompleteExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,21 @@ public static void Run()
Console.WriteLine("*** Results ***");
foreach (var candidate in candidates)
{
Console.Write(candidate.Street);
Console.Write(" ");
Console.Write(candidate.Locality);
Console.Write(", ");
Console.WriteLine(candidate.CountryISO3);
}
if (candidate.AddressText != null) {
Console.Write(candidate.Entries);
Console.Write(" ");
Console.Write(candidate.AddressText);
Console.Write(", ");
Console.WriteLine(candidate.AddressID);
} else {
Console.Write(candidate.Street);
Console.Write(" ");
Console.Write(candidate.Locality);
Console.Write(", ");
Console.WriteLine(candidate.CountryISO3);
}
}
}

}
}
4 changes: 2 additions & 2 deletions src/sdk/ClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ClientBuilder
private Dictionary<string, string> customHeaders;
private List<string> licenses;
private const string InternationalStreetApiUrl = "https://international-street.api.smarty.com/verify";
private const string InternationalAutocompleteApiUrl = "https://international-autocomplete.api.smarty.com/lookup";
private const string InternationalAutocompleteApiUrl = "https://international-autocomplete.api.smarty.com/v2/lookup";
private const string UsAutocompleteApiUrl = "https://us-autocomplete.api.smarty.com/suggest";
private const string UsAutocompleteProApiUrl = "https://us-autocomplete-pro.api.smarty.com/lookup";
private const string UsExtractApiUrl = "https://us-extract.api.smarty.com/";
Expand Down Expand Up @@ -136,7 +136,7 @@ public InternationalStreetApi.Client BuildInternationalStreetApiClient()
return new InternationalStreetApi.Client(this.BuildSender(), this.serializer);
}

public InternationalAutocompleteApi.Client BuildInternationalAutocompleteApiClient()
public InternationalAutocompleteApi.Client BuildInternationalAutocompleteApiClient()
{
this.EnsureURLPrefixNotNull(InternationalAutocompleteApiUrl);
return new InternationalAutocompleteApi.Client(this.BuildSender(), this.serializer);
Expand Down
19 changes: 12 additions & 7 deletions src/sdk/InternationalAutcompleteApi/Candidate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SmartyStreets.InternationalAutocompleteApi
{
using System.Runtime.Serialization;
using System.Runtime.Serialization;

/// <remarks>
/// See "https://smartystreets.com/docs/cloud/us-autocomplete-api#http-response"
Expand All @@ -18,16 +18,21 @@ public class Candidate
[DataMember(Name = "administrative_area")]
public string AdministrativeArea { get; set; }

[DataMember(Name = "super_administrative_area")]
public string SuperAdministrativeArea { get; set; }

[DataMember(Name = "sub_administrative_area")]
public string SubAdministrativeArea { get; set; }

[DataMember(Name = "postal_code")]
public string PostalCode { get; set; }

[DataMember(Name = "country_iso3")]
public string CountryISO3 { get; set; }

[DataMember(Name = "entries")]
public string Entries {get; set; }

[DataMember(Name = "address_text")]
public string AddressText {get; set; }

[DataMember(Name = "address_id")]
public string AddressID {get; set; }


}
}
19 changes: 12 additions & 7 deletions src/sdk/InternationalAutcompleteApi/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ public Client(ISender sender, ISerializer serializer)

public void Send(Lookup lookup)
{

if (lookup == null)
throw new ArgumentNullException("lookup");

if (string.IsNullOrEmpty(lookup?.Search))
throw new SmartyException("Send() must be passed a Lookup with the prefix field set.");
if (string.IsNullOrEmpty(lookup?.Country))
throw new SmartyException("Send() must be passed a Lookup with the country field set.");

if (string.IsNullOrEmpty(lookup?.Search) && string.IsNullOrEmpty(lookup?.AddressID))
throw new SmartyException("Send() must be passed a Lookup with the search or addressID field set.");


var request = BuildRequest(lookup);

Expand All @@ -37,22 +42,22 @@ public void Send(Lookup lookup)
var candidates = result.Candidates;
lookup.Result = candidates;
}

}

private static Request BuildRequest(Lookup lookup)
{
var request = new Request();

if (lookup.AddressID != null) {
request.SetUrlPrefix("/" + lookup.AddressID);
}

request.SetParameter("search", lookup.Search);
request.SetParameter("country", lookup.Country);
request.SetParameter("max_results", lookup.MaxSuggestionsString);
request.SetParameter("include_only_administrative_area", lookup.AdministrativeArea);
request.SetParameter("include_only_locality", lookup.Locality);
request.SetParameter("include_only_postal_code", lookup.PostalCode);
request.SetParameter("geolocation", lookup.GeolocationString);
request.SetParameter("distance", lookup.DistanceString);
request.SetParameter("latitude", lookup.Latitude);
request.SetParameter("longitude", lookup.Longitude);

return request;
}
Expand Down
11 changes: 0 additions & 11 deletions src/sdk/InternationalAutcompleteApi/GeolocateType.cs

This file was deleted.

20 changes: 1 addition & 19 deletions src/sdk/InternationalAutcompleteApi/Lookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,15 @@ public class Lookup
#region [ Fields ]

private const int MAX_RESULTS_DEFAULT = 10;
public const int DISTANCE_DEFAULT = 5;

public Candidate[] Result { get; set; }
public string Search { get; set; }

public string Country { get; set; }
public string AddressID {get; set; }
public int MaxResults { get; set; }

public string AdministrativeArea { get; set; }

public string Locality { get; set; }

public string PostalCode { get; set; }

public int Distance { get; set; }

public string Geolocation { get; set; }

public string Latitude { get; set; }

public string Longitude { get; set; }

#endregion

#region [ Constructors ]
Expand All @@ -47,8 +34,6 @@ public class Lookup
public Lookup()
{
this.MaxResults = MAX_RESULTS_DEFAULT;
this.Geolocation = GeolocateType.NONE;
this.Distance = Lookup.DISTANCE_DEFAULT;
}

/// <param name="search">The beginning of an address.</param>
Expand All @@ -61,8 +46,5 @@ public Lookup(string search) : this()

internal string MaxSuggestionsString => this.MaxResults.Equals(MAX_RESULTS_DEFAULT) ? null : this.MaxResults.ToString();

internal string GeolocationString => this.Geolocation == GeolocateType.NONE ? null : this.Geolocation;

internal string DistanceString => (this.Distance < 1) ? Lookup.DISTANCE_DEFAULT.ToString() : (this.Distance == Lookup.DISTANCE_DEFAULT) ? null : this.Distance.ToString();
}
}
5 changes: 5 additions & 0 deletions src/sdk/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public void SetUrlPrefix(string value)
this.urlPrefix = value;
}

public String GetUrlPrefix()
{
return this.urlPrefix;
}

public string GetUrl()
{
var url = this.urlPrefix;
Expand Down
6 changes: 5 additions & 1 deletion src/sdk/URLPrefixSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ public URLPrefixSender(string urlPrefix, ISender inner)

public Response Send(Request request)
{
request.SetUrlPrefix(this.urlPrefix);
if (request.GetUrlPrefix() != null) {
request.SetUrlPrefix(this.urlPrefix + request.GetUrlPrefix());
} else {
request.SetUrlPrefix(this.urlPrefix);
}
return this.inner.Send(request);
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/tests/InternationalAutcomplete/CandidateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ public void TestFullJSONDeserialization()
""country_iso3"": ""AUS""
},
{
""street"": ""TWELFTH AV"",
""locality"": ""EDEN PARK"",
""administrative_area"": ""VIC"",
""postal_code"": ""3757"",
""country_iso3"": ""AUS""
""entries"": ""3"",
""address_text"": ""my_fun_street"",
""address_id"": ""my_address_id""
}
]
}";
Expand All @@ -46,11 +45,9 @@ public void TestFullJSONDeserialization()
Assert.AreEqual("VIC", actual[0].AdministrativeArea);
Assert.AreEqual("3226", actual[0].PostalCode);
Assert.AreEqual("AUS", actual[0].CountryISO3);
Assert.AreEqual("TWELFTH AV", actual[1].Street);
Assert.AreEqual("EDEN PARK", actual[1].Locality);
Assert.AreEqual("VIC", actual[1].AdministrativeArea);
Assert.AreEqual("3757", actual[1].PostalCode);
Assert.AreEqual("AUS", actual[1].CountryISO3);
Assert.AreEqual("3", actual[1].Entries);
Assert.AreEqual("my_fun_street", actual[1].AddressText);
Assert.AreEqual("my_address_id", actual[1].AddressID);
}
}

Expand Down
38 changes: 16 additions & 22 deletions src/tests/InternationalAutcomplete/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ClientTests
public void Setup()
{
this.capturingSender = new RequestCapturingSender();
this.urlSender = new URLPrefixSender("http://localhost/", this.capturingSender);
this.urlSender = new URLPrefixSender("http://localhost/lookup", this.capturingSender);
}

#region [ Single Lookup ]
Expand All @@ -24,10 +24,14 @@ public void TestSendingSinglePrefixOnlyLookup()
{
var serializer = new FakeSerializer(new byte[0]);
var client = new Client(this.urlSender, serializer);
var lookup = new Lookup("1")
{
Country = "2"
};

client.Send(new Lookup("1"));
client.Send(lookup);

Assert.AreEqual("http://localhost/?search=1",
Assert.AreEqual("http://localhost/lookup?search=1&country=2",
this.capturingSender.Request.GetUrl());
}

Expand All @@ -37,19 +41,15 @@ public void TestSendingSingleFullyPopulatedLookup()
var serializer = new FakeSerializer(new byte[0]);
var client = new Client(this.urlSender, serializer);
const string expectedURL =
"http://localhost/?search=1&country=2&max_results=3&include_only_administrative_area=4&include_only_locality=5&include_only_postal_code=6&geolocation=7&distance=8&latitude=9&longitude=10";
"http://localhost/lookup/myID?search=1&country=2&max_results=3&include_only_locality=5&include_only_postal_code=6";
var lookup = new Lookup
{
Search = "1",
Country = "2",
MaxResults = 3,
AdministrativeArea = "4",
Locality = "5",
PostalCode = "6",
Geolocation = "7",
Distance = 8,
Latitude = "9",
Longitude = "10"
AddressID = "myID"
};

client.Send(lookup);
Expand All @@ -63,19 +63,14 @@ public void TestSendingSinglePopulatedLookupWithNoGeolocation()
var serializer = new FakeSerializer(new byte[0]);
var client = new Client(this.urlSender, serializer);
const string expectedURL =
"http://localhost/?search=1&country=2&max_results=3&include_only_administrative_area=4&include_only_locality=5&include_only_postal_code=6&distance=7&latitude=9&longitude=10";
"http://localhost/lookup?search=1&country=2&max_results=3&include_only_locality=5&include_only_postal_code=6";
var lookup = new Lookup
{
Search = "1",
Country = "2",
MaxResults = 3,
AdministrativeArea = "4",
Locality = "5",
PostalCode = "6",
Distance = 7,
Geolocation = GeolocateType.NONE,
Latitude = "9",
Longitude = "10"
};

client.Send(lookup);
Expand All @@ -89,19 +84,14 @@ public void TestSendingSinglePopulatedLookupWithEmptyGeolocation()
var serializer = new FakeSerializer(new byte[0]);
var client = new Client(this.urlSender, serializer);
const string expectedURL =
"http://localhost/?search=1&country=2&max_results=3&include_only_administrative_area=4&include_only_locality=5&include_only_postal_code=6&distance=7&latitude=9&longitude=10";
"http://localhost/lookup?search=1&country=2&max_results=3&include_only_locality=5&include_only_postal_code=6";
var lookup = new Lookup
{
Search = "1",
Country = "2",
MaxResults = 3,
AdministrativeArea = "4",
Locality = "5",
PostalCode = "6",
Distance = 7,
Geolocation = "",
Latitude = "9",
Longitude = "10"
};

client.Send(lookup);
Expand All @@ -122,7 +112,10 @@ public void TestDeserializeCalledWithResponseBody()
var deserializer = new FakeDeserializer(new Result());
var client = new Client(sender, deserializer);

client.Send(new Lookup("1"));
var lookup = new Lookup("1");
lookup.Country = "2";

client.Send(lookup);

Assert.AreEqual(response.Payload, deserializer.Payload);
}
Expand Down Expand Up @@ -159,6 +152,7 @@ public void TestRejectEmptyPrefix()
public void TestResultCorrectlyAssignedToLookup()
{
var lookup = new Lookup("1");
lookup.Country = "2";
var expectedResult = new Result();

var mockSender = new MockSender(new Response(0, Encoding.ASCII.GetBytes("{[]}")));
Expand Down
36 changes: 36 additions & 0 deletions src/tests/URLPrefixSenderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace SmartyStreets
{
using System.Collections.Generic;
using NUnit.Framework;

[TestFixture]
public class URLPrefixSenderTests
{
[Test]
public void TestRequestURLPresent()
{
var mockSender = new MockSender(null);
var urlPrefixSender = new URLPrefixSender("http://localhost/", mockSender);

var request = new Request();
request.SetUrlPrefix("jimbo");

urlPrefixSender.Send(request);

Assert.AreEqual("http://localhost/jimbo?", request.GetUrl());
}

[Test]
public void TestRequestURLAbsent()
{
var mockSender = new MockSender(null);
var urlPrefixSender = new URLPrefixSender("http://localhost/", mockSender);

var request = new Request();

urlPrefixSender.Send(request);

Assert.AreEqual("http://localhost/?", request.GetUrl());
}
}
}

0 comments on commit c9f02b1

Please sign in to comment.