Skip to content

Commit

Permalink
Landon/enrichment sdk (#41)
Browse files Browse the repository at this point in the history
Add Enrichment SDK
  • Loading branch information
LandonSmarty authored Jan 30, 2024
1 parent a573b6c commit 126c008
Show file tree
Hide file tree
Showing 15 changed files with 1,902 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ private static void Main()
USExtractExample.Run();
USAutocompleteProExample.Run();
USReverseGeoExample.Run();
USEnrichmentExample.Run();
}
}
}
78 changes: 78 additions & 0 deletions src/examples/USEnrichmentExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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;

internal static class USEnrichmentExample
{
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();

SmartyStreets.USEnrichmentApi.Property.Principal.Result[] results = null;
try {
results = client.SendPropertyPrincipalLookup("1682393594");
}
catch (Exception ex) {
Console.WriteLine(ex.Message + ex.StackTrace);
}

if (results != null) {
foreach (SmartyStreets.USEnrichmentApi.Property.Principal.Result result in results) {
PrintResult(result);
}
}
else {
Console.WriteLine("Result was null");
}


SmartyStreets.USEnrichmentApi.Property.Financial.Result[] financialResults = null;
try {
financialResults = client.SendPropertyFinancialLookup("1682393594");
}
catch (Exception ex) {
Console.WriteLine(ex.Message + ex.StackTrace);
}

if (financialResults != null) {
foreach (SmartyStreets.USEnrichmentApi.Property.Financial.Result result in financialResults) {
PrintResult(result);
}
}
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 == "Attributes" ){
PrintResult(property.GetValue(obj, null));
}
if (property.GetValue(obj, null) != null) {
Console.WriteLine($"{property.Name}: {property.GetValue(obj, null)}");
}
}
}
}
}
33 changes: 33 additions & 0 deletions src/integration/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public static void RunAllApiIntegrationTests()
TestUSStreetRequestReturnsWithCorrectNumberOfResults(credentials);
TestUSZIPCodeRequestReturnsWithCorrectNumberOfResults(credentials);
TestReturnsCorrectNumberOfResultsViaProxy(credentials);
TestUsEnrichmentPropertyPrincipalRequestReturnsCorrectNumberOfResults(credentials);
TestUsEnrichmentPropertyFinancialRequestReturnsCorrectNumberOfResults(credentials);
}

private static void TestInternationalStreetRequestReturnsWithCorrectNumberOfResults(ICredentials credentials)
Expand Down Expand Up @@ -180,6 +182,37 @@ private static void TestReturnsCorrectNumberOfResultsViaProxy(ICredentials crede
AssertResults("VIA_PROXY", citiesAmount, 7);
}

private static void TestUsEnrichmentPropertyPrincipalRequestReturnsCorrectNumberOfResults(ICredentials credentials){
var client = new ClientBuilder(credentials).BuildUsEnrichmentApiClient();

SmartyStreets.USEnrichmentApi.Property.Principal.Result[] results = null;
try
{
results = client.SendPropertyPrincipalLookup("1682393594");
}
catch (Exception)
{
Console.WriteLine("");
}
AssertResults("ENRICHMENT_PROPERTY_PRINCIPAL", results.Length, 1);
}

private static void TestUsEnrichmentPropertyFinancialRequestReturnsCorrectNumberOfResults(ICredentials credentials){
var client = new ClientBuilder(credentials).BuildUsEnrichmentApiClient();

SmartyStreets.USEnrichmentApi.Property.Financial.Result[] results = null;
try
{
results = client.SendPropertyFinancialLookup("1682393594");
}
catch (Exception)
{
Console.WriteLine("");
}

AssertResults("ENRICHMENT_FINANCIAL_PRINCIPAL", results.Length, 1);
}

private static void AssertResults(string apiType, int actualResultCount, int expectedResultCount)
{
if (actualResultCount == expectedResultCount)
Expand Down
7 changes: 7 additions & 0 deletions src/sdk/ClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ClientBuilder
private const string UsStreetApiUrl = "https://us-street.api.smarty.com/street-address";
private const string UsZipCodeApiUrl = "https://us-zipcode.api.smarty.com/lookup";
private const string UsReverseGeoApiUrl = "https://us-reverse-geo.api.smarty.com/lookup";
private const string USEnrichmentApiUrl = "https://us-enrichment.api.smarty.com/lookup";

public ClientBuilder()
{
Expand Down Expand Up @@ -178,6 +179,12 @@ public USReverseGeoApi.Client BuildUsReverseGeoApiClient()
return new USReverseGeoApi.Client(this.BuildSender(), this.serializer);
}

public USEnrichmentApi.Client BuildUsEnrichmentApiClient()
{
this.EnsureURLPrefixNotNull(USEnrichmentApiUrl);
return new USEnrichmentApi.Client(this.BuildSender(), this.serializer);
}

private ISender BuildSender()
{
if (this.httpSender != null)
Expand Down
51 changes: 51 additions & 0 deletions src/sdk/USEnrichmentApi/Client.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace SmartyStreets.USEnrichmentApi
{
using System;
using System.IO;

public class Client //: IUSEnrichmentClient
{
private readonly ISender sender;
private readonly ISerializer serializer;

public Client(ISender sender, ISerializer serializer)
{
this.sender = sender;
this.serializer = serializer;
}

public Property.Financial.Result[] SendPropertyFinancialLookup(string smartyKey)
{
Property.Financial.Lookup lookup = new Property.Financial.Lookup(smartyKey);
Send(lookup);
return lookup.GetResults();
}

public Property.Principal.Result[] SendPropertyPrincipalLookup(string smartyKey)
{
Property.Principal.Lookup lookup = new Property.Principal.Lookup(smartyKey);
Send(lookup);
return lookup.GetResults();
}

private void Send(Lookup lookup)
{
if (lookup == null || string.IsNullOrEmpty(lookup.GetSmartyKey()))
throw new SmartyStreets.SmartyException("Client.Send() requires a Lookup with the 'smartyKey' field set");
Request request = BuildRequest(lookup);
Response response = this.sender.Send(request);
if (response.Payload != null){
using (var payloadStream = new MemoryStream(response.Payload)){
lookup.DeserializeAndSetResults(serializer, payloadStream);
}
}
}

private SmartyStreets.Request BuildRequest(Lookup lookup)
{
SmartyStreets.Request request = new SmartyStreets.Request();
request.SetUrlPrefix("/" + lookup.GetSmartyKey() + "/" + lookup.GetDatasetName() + "/" + lookup.GetDataSubsetName());
return request;
}
}
}
9 changes: 9 additions & 0 deletions src/sdk/USEnrichmentApi/IUSEnrichmentClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SmartyStreets.USEnrichmentApi
{
// marker interface for easy dependency injection and unit test mocking
public interface IUSEnrichmentClient
{
SmartyStreets.USEnrichmentApi.Property.Principal.Result[] sendPropertyPrincipalLookup(string smartyKey);
SmartyStreets.USEnrichmentApi.Property.Financial.Result[] sendPropertyFinancialLookup(string smartyKey);
}
}
36 changes: 36 additions & 0 deletions src/sdk/USEnrichmentApi/Lookup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace SmartyStreets.USEnrichmentApi
{
using System;
using System.IO;

public abstract class Lookup
{
private readonly string smartyKey;
private readonly string datasetName;
private readonly string dataSubsetName;

public Lookup(string smartyKey, string datasetName, string dataSubsetName)
{
this.smartyKey = smartyKey;
this.datasetName = datasetName;
this.dataSubsetName = dataSubsetName;
}

public string GetSmartyKey()
{
return smartyKey;
}

public string GetDatasetName()
{
return datasetName;
}

public string GetDataSubsetName()
{
return dataSubsetName;
}

public abstract void DeserializeAndSetResults(SmartyStreets.ISerializer serializer, Stream payload);
}
}
Loading

0 comments on commit 126c008

Please sign in to comment.