Skip to content

Commit 3b0249b

Browse files
authored
Merge pull request #59 from smartystreets/eric/enrichment-address-search
Eric/enrichment address search
2 parents 71b70d6 + 637459e commit 3b0249b

File tree

4 files changed

+224
-40
lines changed

4 files changed

+224
-40
lines changed

examples/us_enrichment_example.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
from smartystreets_python_sdk import SharedCredentials, StaticCredentials, exceptions, ClientBuilder
4+
from smartystreets_python_sdk.us_enrichment.lookup import Lookup as EnrichmentLookup
45

56

67
# from smartystreets_python_sdk.us_enrichment import
@@ -30,17 +31,34 @@ def run():
3031
# client = ClientBuilder(credentials).with_http_proxy('localhost:8080', 'user', 'password').build_us_street_api_client()
3132
# Uncomment the line above to try it with a proxy instead
3233

33-
smarty_key = "1682393594"
34+
smarty_key = "325023201"
35+
36+
lookup = EnrichmentLookup()
37+
freeform_lookup = EnrichmentLookup()
38+
39+
lookup.street = "56 Union Ave"
40+
lookup.city = "Somerville"
41+
lookup.state = "NJ"
42+
lookup.zipcode = "08876"
43+
44+
freeform_lookup.freeform = "56 Union Ave Somerville NJ 08876"
45+
3446
try:
47+
# use the below line to send a lookup with a smarty key
3548
results = client.send_property_principal_lookup(smarty_key)
49+
# Or, uncomment the below line to send a lookup with an address in components
50+
# results = client.send_property_principal_lookup(lookup)
51+
# Or, uncomment the below line to send a lookup with an address in freeform
52+
# results = client.send_property_principal_lookup(freeform_lookup)
53+
3654
# results = client.send_generic_lookup(smarty_key, 'property', 'principal')
3755
# Uncomment the line above to try it as a generic lookup instead
3856
except Exception as err:
3957
print(err)
4058
return
4159

4260
if not results:
43-
print("No results found. This means the Smartykey is likely not valid.")
61+
print("No results found. This means the Smartykey or address is likely not valid, or does not have data in this dataset")
4462
return
4563

4664
top_result = results[0]

smartystreets_python_sdk/us_enrichment/client.py

+94-32
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,79 @@ def __init__(self, sender, serializer):
1212
self.sender = sender
1313
self.serializer = serializer
1414

15-
def send_property_financial_lookup(self, smartykey):
16-
l = FinancialLookup(smartykey)
17-
send_lookup(self, l)
18-
return l.result
19-
20-
def send_property_principal_lookup(self, smartykey):
21-
l = PrincipalLookup(smartykey)
22-
send_lookup(self, l)
23-
return l.result
15+
def send_property_financial_lookup(self, lookup):
16+
if isinstance(lookup, str):
17+
l = FinancialLookup(lookup)
18+
send_lookup(self, l)
19+
return l.result
20+
else:
21+
lookup.dataset = 'property'
22+
lookup.dataSubset = 'financial'
23+
send_lookup(self, lookup)
24+
return lookup.result
25+
26+
def send_property_principal_lookup(self, lookup):
27+
if isinstance(lookup, str):
28+
l = PrincipalLookup(lookup)
29+
send_lookup(self, l)
30+
return l.result
31+
else:
32+
lookup.dataset = 'property'
33+
lookup.dataSubset = 'principal'
34+
send_lookup(self, lookup)
35+
return lookup.result
2436

25-
def send_geo_reference_lookup(self, smartykey):
26-
l = GeoReferenceLookup(smartykey)
27-
send_lookup(self, l)
28-
return l.result
37+
def send_geo_reference_lookup(self, lookup):
38+
if isinstance(lookup, str):
39+
l = GeoReferenceLookup(lookup)
40+
send_lookup(self, l)
41+
return l.result
42+
else:
43+
lookup.dataset = 'geo-reference'
44+
lookup.dataSubset = None
45+
send_lookup(self, lookup)
46+
return lookup.result
2947

30-
def send_secondary_lookup(self, smartykey):
31-
l = SecondaryLookup(smartykey)
32-
send_lookup(self, l)
33-
return l.result
48+
def send_secondary_lookup(self, lookup):
49+
if isinstance(lookup, str):
50+
l = SecondaryLookup(lookup)
51+
send_lookup(self, l)
52+
return l.result
53+
else:
54+
lookup.dataset = 'secondary'
55+
lookup.dataSubset = None
56+
send_lookup(self, lookup)
57+
return lookup.result
3458

35-
def send_secondary_count_lookup(self, smartykey):
36-
l = SecondaryCountLookup(smartykey)
37-
send_lookup(self, l)
38-
return l.result
59+
def send_secondary_count_lookup(self, lookup):
60+
if isinstance(lookup, str):
61+
l = SecondaryCountLookup(lookup)
62+
send_lookup(self, l)
63+
return l.result
64+
else:
65+
lookup.dataset = 'secondary'
66+
lookup.dataSubset = 'count'
67+
send_lookup(self, lookup)
68+
return lookup.result
3969

40-
def send_generic_lookup(self, smartykey, dataset, dataSubset):
41-
l = Lookup(smartykey, dataset, dataSubset)
42-
send_lookup(self, l)
43-
return l.result
70+
def send_generic_lookup(self, lookup, dataset, dataSubset):
71+
if isinstance(lookup, str):
72+
l = Lookup(lookup, dataset, dataSubset)
73+
send_lookup(self, l)
74+
return l.result
75+
else:
76+
lookup.dataset = dataset
77+
lookup.dataSubset = dataSubset
78+
send_lookup(self, lookup)
79+
return lookup.result
4480

4581

4682
def send_lookup(client: Client, lookup):
4783
"""
4884
Sends a Lookup object to the US Enrichment API and stores the result in the Lookup's result field.
4985
"""
50-
if lookup is None or lookup.smartykey is None or not isinstance(lookup.smartykey, str) or len(
51-
lookup.smartykey.strip()) == 0:
52-
raise SmartyException('Client.send() requires a Lookup with the "smartykey" field set as a string')
86+
if lookup is None or (lookup.smartykey is None and lookup.street is None and lookup.freeform is None):
87+
raise SmartyException('Client.send() requires a Lookup with either the "smartykey", "street, or "freeform" field set as a string')
5388

5489
request = build_request(lookup)
5590

@@ -67,10 +102,37 @@ def send_lookup(client: Client, lookup):
67102

68103
def build_request(lookup):
69104
request = Request()
70-
if lookup.dataSubset == None:
71-
request.url_components = lookup.smartykey + "/" + lookup.dataset
105+
if lookup.smartykey != None:
106+
if lookup.dataSubset == None:
107+
request.url_components = lookup.smartykey + "/" + lookup.dataset
108+
return request
109+
110+
request.url_components = lookup.smartykey + "/" + lookup.dataset + "/" + lookup.dataSubset
111+
112+
return request
113+
else:
114+
if lookup.dataSubset == None:
115+
request.url_components = 'search/' + lookup.dataset
116+
request.parameters = remap_keys(lookup)
117+
return request
118+
119+
request.url_components = 'search/' + lookup.dataset + "/" + lookup.dataSubset
120+
121+
request.parameters = remap_keys(lookup)
72122
return request
73123

74-
request.url_components = lookup.smartykey + "/" + lookup.dataset + "/" + lookup.dataSubset
124+
def remap_keys(lookup):
125+
converted_lookup = {}
126+
127+
add_field(converted_lookup, 'freeform', lookup.freeform)
128+
add_field(converted_lookup, 'street', lookup.street)
129+
add_field(converted_lookup, 'city', lookup.city)
130+
add_field(converted_lookup, 'state', lookup.state)
131+
add_field(converted_lookup, 'zipcode', lookup.zipcode)
132+
133+
return converted_lookup
134+
75135

76-
return request
136+
def add_field(converted_lookup, key, value):
137+
if value:
138+
converted_lookup[key] = value

smartystreets_python_sdk/us_enrichment/lookup.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,33 @@
77
noneDataSubset = None
88

99
class Lookup:
10-
def __init__(self, smartykey, dataset, dataSubset):
10+
def __init__(self, smartykey = None, dataset = None, dataSubset = None, freeform = None, street = None, city = None, state = None, zipcode = None):
1111
self.smartykey = smartykey
1212
self.dataset = dataset
1313
self.dataSubset = dataSubset
14+
self.freeform = freeform
15+
self.street = street
16+
self.city = city
17+
self.state = state
18+
self.zipcode = zipcode
1419
self.result = []
1520

1621
class FinancialLookup(Lookup):
17-
def __init__(self, smartykey):
22+
def __init__(self, smartykey = None):
1823
super().__init__(smartykey, propertyDataset, financialDataSubset)
1924

2025
class PrincipalLookup(Lookup):
21-
def __init__(self, smartykey):
26+
def __init__(self, smartykey = None):
2227
super().__init__(smartykey, propertyDataset, principalDataSubset)
2328

2429
class GeoReferenceLookup(Lookup):
25-
def __init__(self, smartykey):
30+
def __init__(self, smartykey = None):
2631
super().__init__(smartykey, geoReferenceDataset, noneDataSubset)
2732

2833
class SecondaryLookup(Lookup):
29-
def __init__(self, smartykey):
34+
def __init__(self, smartykey = None):
3035
super().__init__(smartykey, secondaryDataset, noneDataSubset)
3136

3237
class SecondaryCountLookup(Lookup):
33-
def __init__(self, smartykey):
38+
def __init__(self, smartykey = None):
3439
super().__init__(smartykey, secondaryDataset, countDataSubset)

test/us_enrichment/client_test.py

+99
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ def test_sending_Financial_Lookup(self):
2323
function_result = client.send_property_financial_lookup("xxx")
2424
self.assertEqual(result, function_result)
2525

26+
def test_sending_Financial_address_Lookup(self):
27+
capturing_sender = RequestCapturingSender()
28+
sender = URLPrefixSender('http://localhost/', capturing_sender)
29+
serializer = FakeSerializer(None)
30+
client = Client(sender, serializer)
31+
lookup = FinancialLookup()
32+
lookup.street = "street"
33+
lookup.city = "city"
34+
lookup.state = "state"
35+
lookup.zipcode = "zipcode"
36+
result = send_lookup(client, lookup)
37+
38+
self.assertEqual("property", lookup.dataset)
39+
self.assertEqual("financial", lookup.dataSubset)
40+
self.assertEqual(lookup.result, result)
41+
42+
function_result = client.send_property_financial_lookup(lookup)
43+
self.assertEqual(result, function_result)
44+
2645
def test_sending_principal_lookup(self):
2746
capturing_sender = RequestCapturingSender()
2847
sender = URLPrefixSender('http://localhost/', capturing_sender)
@@ -38,6 +57,26 @@ def test_sending_principal_lookup(self):
3857

3958
function_result = client.send_property_principal_lookup("xxx")
4059
self.assertEqual(result, function_result)
60+
61+
def test_sending_principal_address_lookup(self):
62+
capturing_sender = RequestCapturingSender()
63+
sender = URLPrefixSender('http://localhost/', capturing_sender)
64+
serializer = FakeSerializer(None)
65+
client = Client(sender, serializer)
66+
67+
lookup = PrincipalLookup()
68+
lookup.street = "street"
69+
lookup.city = "city"
70+
lookup.state = "state"
71+
lookup.zipcode = "zipcode"
72+
result = send_lookup(client, lookup)
73+
74+
self.assertEqual("property", lookup.dataset)
75+
self.assertEqual("principal", lookup.dataSubset)
76+
self.assertEqual(lookup.result, result)
77+
78+
function_result = client.send_property_principal_lookup(lookup)
79+
self.assertEqual(result, function_result)
4180

4281
def test_sending_geo_reference_lookup(self):
4382
capturing_sender = RequestCapturingSender()
@@ -54,6 +93,26 @@ def test_sending_geo_reference_lookup(self):
5493

5594
function_result = client.send_geo_reference_lookup("xxx")
5695
self.assertEqual(result, function_result)
96+
97+
def test_sending_geo_reference_address_lookup(self):
98+
capturing_sender = RequestCapturingSender()
99+
sender = URLPrefixSender('http://localhost/', capturing_sender)
100+
serializer = FakeSerializer(None)
101+
client = Client(sender, serializer)
102+
103+
lookup = GeoReferenceLookup()
104+
lookup.street = "street"
105+
lookup.city = "city"
106+
lookup.state = "state"
107+
lookup.zipcode = "zipcode"
108+
result = send_lookup(client, lookup)
109+
110+
self.assertEqual("geo-reference", lookup.dataset)
111+
self.assertEqual(None, lookup.dataSubset)
112+
self.assertEqual(lookup.result, result)
113+
114+
function_result = client.send_geo_reference_lookup(lookup)
115+
self.assertEqual(result, function_result)
57116

58117
def test_sending_secondary_lookup(self):
59118
capturing_sender = RequestCapturingSender()
@@ -71,6 +130,26 @@ def test_sending_secondary_lookup(self):
71130
function_result = client.send_secondary_lookup("xxx")
72131
self.assertEqual(result, function_result)
73132

133+
def test_sending_secondary_address_lookup(self):
134+
capturing_sender = RequestCapturingSender()
135+
sender = URLPrefixSender('http://localhost/', capturing_sender)
136+
serializer = FakeSerializer(None)
137+
client = Client(sender, serializer)
138+
139+
lookup = SecondaryLookup()
140+
lookup.street = "street"
141+
lookup.city = "city"
142+
lookup.state = "state"
143+
lookup.zipcode = "zipcode"
144+
result = send_lookup(client, lookup)
145+
146+
self.assertEqual("secondary", lookup.dataset)
147+
self.assertEqual(None, lookup.dataSubset)
148+
self.assertEqual(lookup.result, result)
149+
150+
function_result = client.send_secondary_lookup(lookup)
151+
self.assertEqual(result, function_result)
152+
74153
def test_sending_secondary_count_lookup(self):
75154
capturing_sender = RequestCapturingSender()
76155
sender = URLPrefixSender('http://localhost/', capturing_sender)
@@ -86,3 +165,23 @@ def test_sending_secondary_count_lookup(self):
86165

87166
function_result = client.send_secondary_count_lookup("xxx")
88167
self.assertEqual(result, function_result)
168+
169+
def test_sending_secondary_count_address_lookup(self):
170+
capturing_sender = RequestCapturingSender()
171+
sender = URLPrefixSender('http://localhost/', capturing_sender)
172+
serializer = FakeSerializer(None)
173+
client = Client(sender, serializer)
174+
175+
lookup = SecondaryCountLookup()
176+
lookup.street = "street"
177+
lookup.city = "city"
178+
lookup.state = "state"
179+
lookup.zipcode = "zipcode"
180+
result = send_lookup(client, lookup)
181+
182+
self.assertEqual("secondary", lookup.dataset)
183+
self.assertEqual("count", lookup.dataSubset)
184+
self.assertEqual(lookup.result, result)
185+
186+
function_result = client.send_secondary_count_lookup(lookup)
187+
self.assertEqual(result, function_result)

0 commit comments

Comments
 (0)