Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions examples/us_enrichment_business_name_search_example.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to update the Makefile to include this file

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os

from smartystreets_python_sdk import BasicAuthCredentials, ClientBuilder
from smartystreets_python_sdk.us_enrichment import BusinessLookup


def run():
auth_id = os.environ['SMARTY_AUTH_ID']
auth_token = os.environ['SMARTY_AUTH_TOKEN']

credentials = BasicAuthCredentials(auth_id, auth_token)
client = ClientBuilder(credentials).build_us_enrichment_api_client()

lookup = BusinessLookup()
lookup.business_name = "delta air"
lookup.city = "atlanta"

try:
summary_results = client.send_business_lookup(lookup)
except Exception as err:
print(err)
return

if not summary_results:
print("No response returned for business name {}".format(lookup.business_name))
return

summary = summary_results[0]
if not summary.businesses:
print("Business name {} has no business tenants".format(lookup.business_name))
return

print("Matching businesses for business_name '{}':".format(lookup.business_name))
for biz in summary.businesses:
print(" - {} (ID: {})".format(biz.company_name, biz.business_id))

first = summary.businesses[0]
print("\nFetching details for business: {} (ID: {})".format(first.company_name, first.business_id))

try:
detail_result = client.send_business_detail_lookup(first.business_id)
except Exception as err:
print(err)
return

if detail_result is None:
print("\nNo detail result returned")
return

print("\nDetail result:")
print_result(detail_result)


def print_result(obj):
for key, value in vars(obj).items():
if value is None:
continue
if key == 'attributes':
print_result(value)
continue
print("{}: {}".format(key, value))
print()


if __name__ == "__main__":
run()
5 changes: 3 additions & 2 deletions smartystreets_python_sdk/us_enrichment/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ def send_lookup(client: Client, lookup, response_class=Response):
_is_blank(getattr(lookup, 'smartykey', None))
and _is_blank(getattr(lookup, 'street', None))
and _is_blank(getattr(lookup, 'freeform', None))
and _is_blank(getattr(lookup, 'business_name', None))
):
raise SmartyException("Lookup requires one of 'smartykey', 'street', or 'freeform' to be set")
raise SmartyException("Lookup requires one of 'smartykey', 'street', 'freeform', or 'business_name' to be set")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make a change according to my Lookup comment, I would change this too to either be smarty_key or businessName


request = build_request(lookup)
raw = _dispatch(client, request, lookup)
Expand Down Expand Up @@ -158,7 +159,7 @@ def _url_components(lookup):

def _address_parameters(lookup):
params = {}
for key in ('freeform', 'street', 'city', 'state', 'zipcode', 'features'):
for key in ('freeform', 'business_name', 'street', 'city', 'state', 'zipcode', 'features'):
value = getattr(lookup, key, None)
if value:
params[key] = value
Expand Down
3 changes: 2 additions & 1 deletion smartystreets_python_sdk/us_enrichment/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ def add_exclude_attribute(self, attribute):


class Lookup(LookupBase):
def __init__(self, smartykey=None, dataset=None, dataSubset=None, features=None, freeform=None, street=None, city=None, state=None, zipcode=None):
def __init__(self, smartykey=None, dataset=None, dataSubset=None, features=None, freeform=None, business_name=None, street=None, city=None, state=None, zipcode=None):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure how customers are using the Python SDK, but we should probably move string businessName = null to the end of the constructor, in the case that someone was doing this previously:

Lookup(null, null, null, null, "123 main st", "123 main st");

In this case, 123 main st was originally the street parameter, but now it is the businessName parameter. It's an edge case, but I would do this instead:

def __init__(self, smartykey=None, dataset=None, dataSubset=None, features=None, freeform=None, street=None, city=None, state=None, zipcode=None, business_name=None):

super().__init__()
self.smartykey = smartykey
self.dataset = dataset
self.dataSubset = dataSubset
self.features = features
self.freeform = freeform
self.business_name = business_name

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to change this because I don't think there's a right answer in this repo, but in this file, two word variables are either camel case case (dataSubset) or lowercase (smartykey). In other lookup files, they do follow snake case. So anyways I just had to point that out.

self.street = street
self.city = city
self.state = state
Expand Down
21 changes: 21 additions & 0 deletions test/us_enrichment/business_summary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ def test_business_lookup_with_freeform_builds_url(self):
capturing.request.parameters['freeform'],
)

def test_business_lookup_with_business_name_builds_search_param(self):
capturing = RequestCapturingSender()
client, _ = self._client(capturing)

lookup = BusinessLookup()
lookup.business_name = "Style Studio"
client.send_business_lookup(lookup)

self.assertEqual("search/business", capturing.request.url_components)
self.assertEqual("Style Studio", capturing.request.parameters['business_name'])

def test_business_lookup_omits_empty_business_name(self):
capturing = RequestCapturingSender()
client, _ = self._client(capturing)

lookup = BusinessLookup()
lookup.freeform = "1600 amphitheatre pkwy mountain view ca"
client.send_business_lookup(lookup)

self.assertNotIn('business_name', capturing.request.parameters)

def test_rejects_whitespace_smartykey_on_summary_lookup(self):
capturing = RequestCapturingSender()
client, _ = self._client(capturing)
Expand Down