Skip to content

Commit

Permalink
Merge pull request #56 from smartystreets/eric/add-enrichment-secondary
Browse files Browse the repository at this point in the history
Eric/add enrichment secondary
  • Loading branch information
RyanLCox1 authored Aug 2, 2024
2 parents dd2bbf1 + 0410922 commit 7a3093e
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 10 deletions.
12 changes: 11 additions & 1 deletion smartystreets_python_sdk/us_enrichment/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from smartystreets_python_sdk import Request
from smartystreets_python_sdk.exceptions import SmartyException
from .lookup import FinancialLookup, PrincipalLookup, GeoReferenceLookup, Lookup
from .lookup import FinancialLookup, PrincipalLookup, GeoReferenceLookup, SecondaryLookup, SecondaryCountLookup, Lookup
from .response import Response


Expand All @@ -27,6 +27,16 @@ def send_geo_reference_lookup(self, smartykey):
send_lookup(self, l)
return l.result

def send_secondary_lookup(self, smartykey):
l = SecondaryLookup(smartykey)
send_lookup(self, l)
return l.result

def send_secondary_count_lookup(self, smartykey):
l = SecondaryCountLookup(smartykey)
send_lookup(self, l)
return l.result

def send_generic_lookup(self, smartykey, dataset, dataSubset):
l = Lookup(smartykey, dataset, dataSubset)
send_lookup(self, l)
Expand Down
12 changes: 11 additions & 1 deletion smartystreets_python_sdk/us_enrichment/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
financialDataSubset = "financial"
principalDataSubset = "principal"
geoReferenceDataset = "geo-reference"
secondaryDataset = "secondary"
countDataSubset = "count"
noneDataSubset = None

class Lookup:
Expand All @@ -21,4 +23,12 @@ def __init__(self, smartykey):

class GeoReferenceLookup(Lookup):
def __init__(self, smartykey):
super().__init__(smartykey, geoReferenceDataset, noneDataSubset)
super().__init__(smartykey, geoReferenceDataset, noneDataSubset)

class SecondaryLookup(Lookup):
def __init__(self, smartykey):
super().__init__(smartykey, secondaryDataset, noneDataSubset)

class SecondaryCountLookup(Lookup):
def __init__(self, smartykey):
super().__init__(smartykey, secondaryDataset, countDataSubset)
158 changes: 151 additions & 7 deletions smartystreets_python_sdk/us_enrichment/response.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,56 @@
class Response:
def __init__(self, obj):
self.smarty_key = obj.get('smarty_key', None)
self.data_set_name = obj.get('data_set_name', None)
self.data_subset_name = obj.get('data_subset_name', None)
self.attributes = get_attributes(self.data_set_name, self.data_subset_name, obj.get('attributes', None))
data_set_name = None
data_subset_name = None
if 'data_set_name' in obj:
self.data_set_name = obj.get('data_set_name')
data_set_name = obj.get('data_set_name')
elif 'secondaries' in obj:
data_set_name = 'secondary'

if 'data_subset_name' in obj:
self.data_subset_name = obj.get('data_subset_name', None)
data_subset_name = obj.get('data_subset_name', None)
elif 'count' in obj:
data_set_name = 'secondary'
data_subset_name = 'count'

if data_set_name == 'secondary':
if data_subset_name == 'count':
self.count = obj.get('count', None)
else:
self.root_address = get_secondary_root_address(obj.get('root_address', None))
if 'aliases' in obj:
self.aliases = get_secondary_aliases(obj.get('aliases', None))
self.secondaries = get_secondary_secondaries(obj.get('secondaries', None))
else:
self.attributes = get_attributes(data_set_name, data_subset_name, obj.get('attributes', None))

def __str__(self):
lines = [self.__class__.__name__ + ':']
for key, val in vars(self).items():
lines += '{}: {}'.format(key, val).split('\n')
lines += get_lines(key, val)
return '\n '.join(lines)

def __eq__(self, __value: object) -> bool:
return isinstance(__value, type(self)) and __value.smarty_key == self.smarty_key

def get_lines(key, val):
lines = ['']
if type(val) is list:
if len(val) > 1:
for item in val:
if val.index(item) == 0:
lines += ['secondaries:']
lines += get_lines(val.index(item), val[val.index(item)])
return lines
else:
return get_lines(key, val[0])
else:
if type(key) == int:
return ' {}: {}'.format(key, val).split('\n')
return '{}: {}'.format(key, val).split('\n')

def get_attributes(dataset, data_subset, attribute_obj):
if dataset == "property":
Expand All @@ -24,7 +61,6 @@ def get_attributes(dataset, data_subset, attribute_obj):
if dataset == "geo-reference":
return GeoReferenceOutputCategories(attribute_obj)


class PrincipalAttributes:
def __init__(self, obj):
self.first_floor_sqft = obj.get('1st_floor_sqft', None)
Expand Down Expand Up @@ -575,7 +611,7 @@ def __str__(self):

class GeoReferenceOutputCategories:
def __init__(self, obj):

self.census_block = get_geo_reference_census_block(obj.get('census_block', None))
self.census_county_division = get_geo_reference_census_county_division(obj.get('census_county_division', None))
self.census_tract = get_geo_reference_census_tract(obj.get('census_tract', None))
Expand Down Expand Up @@ -669,4 +705,112 @@ def get_geo_reference_place(geo_reference_place_obj):
return None
output = []
output.append(GeoReferencePlace(geo_reference_place_obj))
return output
return output

class SecondaryRootAddress:
def __init__(self, obj):
self.secondary_count = obj.get('secondary_count', None)
self.smarty_key = obj.get('smarty_key', None)
self.primary_number = obj.get('primary_number', None)
self.street_predirection = obj.get('street_predirection', None)
self.street_name = obj.get('street_name', None)
self.street_suffix = obj.get('street_suffix', None)
self.street_postdirection = obj.get('street_postdirection', None)
self.city_name = obj.get('city_name', None)
self.state_abbreviation = obj.get('state_abbreviation', None)
self.zipcode = obj.get('zipcode', None)
self.plus4_code = obj.get('plus4_code', None)

def __str__(self):
lines = ['']
for key, val in vars(self).items():
if type(val) is list:
lines.append(key + ': ')
for item in val:
for subkey, subval in vars(item).items():
lines += ' {}: {}'.format(subkey, subval).split('\n')
else:
lines.append(key + ': ' + str(val))
return '\n '.join(lines)

def get_secondary_root_address(secondary_root_address_obj):
if secondary_root_address_obj is None:
return None
output = []
output.append(SecondaryRootAddress(secondary_root_address_obj))

return output

class SecondaryAliases:
def __init__(self, obj):
self.smarty_key = obj.get('smarty_key', None)
self.primary_number = obj.get('primary_number', None)
self.street_predirection = obj.get('street_predirection', None)
self.street_name = obj.get('street_name', None)
self.street_suffix = obj.get('street_suffix', None)
self.street_postdirection = obj.get('street_postdirection', None)
self.city_name = obj.get('city_name', None)
self.state_abbreviation = obj.get('state_abbreviation', None)
self.zipcode = obj.get('zipcode', None)
self.plus4_code = obj.get('plus4_code', None)

def __str__(self):
lines = ['']
for key, val in vars(self).items():
if type(val) is list:
lines.append(key + ': ')
for item in val:
for subkey, subval in vars(item).items():
lines += ' {}: {}'.format(subkey, subval).split('\n')
else:
lines.append(key + ': ' + str(val))
return '\n '.join(lines)

def get_secondary_aliases(secondary_aliases_obj):
if secondary_aliases_obj is None:
return None
output = []
output_list = []
for item in secondary_aliases_obj:
output.append(SecondaryAliases(item))
output_list.append(output)
output = []
return output_list

class SecondarySecondaries:
def __init__(self, obj):
self.smarty_key = obj.get('smarty_key', None)
self.secondary_designator = obj.get('secondary_designator', None)
self.secondary_number = obj.get('secondary_number', None)
self.plus4_code = obj.get('plus4_code', None)

def __str__(self):
lines = ['']
for key, val in vars(self).items():
if type(val) is list:
lines.append(key + ': ')
for item in val:
for subkey, subval in vars(item).items():
lines += ' {}: {}'.format(subkey, subval).split('\n')
else:
lines.append(key + ': ' + str(val))
return '\n '.join(lines)

def get_secondary_secondaries(secondary_secondaries_obj):
if secondary_secondaries_obj is None:
return None
output = []
output_list = []
for item in secondary_secondaries_obj:
output.append(SecondarySecondaries(item))
output_list.append(output)
output = []
return output_list

class SecondaryCountAttributes:
def __init__(self, obj):
self.smarty_key = obj.get('smarty_key', None)
self.count = obj.get('count', None)

def __str__(self):
return self.__dict__.__str__()
50 changes: 49 additions & 1 deletion test/us_enrichment/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from smartystreets_python_sdk import URLPrefixSender
from smartystreets_python_sdk.us_enrichment.client import Client, send_lookup
from smartystreets_python_sdk.us_enrichment.lookup import FinancialLookup, PrincipalLookup
from smartystreets_python_sdk.us_enrichment.lookup import FinancialLookup, PrincipalLookup, GeoReferenceLookup, SecondaryLookup, SecondaryCountLookup
from smartystreets_python_sdk.us_enrichment.response import Response
from test.mocks import *

Expand Down Expand Up @@ -38,3 +38,51 @@ def test_sending_principal_lookup(self):

function_result = client.send_property_principal_lookup("xxx")
self.assertEqual(result, function_result)

def test_sending_geo_reference_lookup(self):
capturing_sender = RequestCapturingSender()
sender = URLPrefixSender('http://localhost/', capturing_sender)
serializer = FakeSerializer(None)
client = Client(sender, serializer)

lookup = GeoReferenceLookup("xxx")
result = send_lookup(client, lookup)

self.assertEqual("geo-reference", lookup.dataset)
self.assertEqual(None, lookup.dataSubset)
self.assertEqual(lookup.result, result)

function_result = client.send_geo_reference_lookup("xxx")
self.assertEqual(result, function_result)

def test_sending_secondary_lookup(self):
capturing_sender = RequestCapturingSender()
sender = URLPrefixSender('http://localhost/', capturing_sender)
serializer = FakeSerializer(None)
client = Client(sender, serializer)

lookup = SecondaryLookup("xxx")
result = send_lookup(client, lookup)

self.assertEqual("secondary", lookup.dataset)
self.assertEqual(None, lookup.dataSubset)
self.assertEqual(lookup.result, result)

function_result = client.send_secondary_lookup("xxx")
self.assertEqual(result, function_result)

def test_sending_secondary_count_lookup(self):
capturing_sender = RequestCapturingSender()
sender = URLPrefixSender('http://localhost/', capturing_sender)
serializer = FakeSerializer(None)
client = Client(sender, serializer)

lookup = SecondaryCountLookup("xxx")
result = send_lookup(client, lookup)

self.assertEqual("secondary", lookup.dataset)
self.assertEqual("count", lookup.dataSubset)
self.assertEqual(lookup.result, result)

function_result = client.send_secondary_count_lookup("xxx")
self.assertEqual(result, function_result)

0 comments on commit 7a3093e

Please sign in to comment.