diff --git a/Makefile b/Makefile index 651d103..f39ad88 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ us_autocomplete_pro_api: PYTHONPATH=. python3 examples/us_autocomplete_pro_example.py us_enrichment_api: - PYTHONPATH=. python3 examples/us_enrichment_example.py + PYTHONPATH=. python3 examples/us_enrichment_example.py && PYTHONPATH=. python3 examples/us_enrichment_etag_example.py && PYTHONPATH=. python3 examples/us_enrichment_business_example.py && PYTHONPATH=. python3 examples/us_enrichment_business_name_search_example.py && PYTHONPATH=. python3 examples/us_enrichment_generic_example.py && PYTHONPATH=. python3 examples/us_enrichment_geo_reference_example.py && PYTHONPATH=. python3 examples/us_enrichment_secondary_example.py && PYTHONPATH=. python3 examples/us_enrichment_secondary_count_example.py us_extract_api: PYTHONPATH=. python3 examples/us_extract_example.py diff --git a/examples/us_enrichment_business_name_search_example.py b/examples/us_enrichment_business_name_search_example.py new file mode 100644 index 0000000..7140f79 --- /dev/null +++ b/examples/us_enrichment_business_name_search_example.py @@ -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() diff --git a/examples/us_enrichment_etag_example.py b/examples/us_enrichment_etag_example.py index 7c38ceb..95515da 100644 --- a/examples/us_enrichment_etag_example.py +++ b/examples/us_enrichment_etag_example.py @@ -69,7 +69,7 @@ def exercise_summary_etag(client, smarty_key): def exercise_detail_etag(client, business_id): print() - print("=== Business.Detail ETag round trip (businessId: {}) ===".format(business_id)) + print("=== Business.Detail ETag round trip (business_id: {}) ===".format(business_id)) first = BusinessDetailLookup(business_id) try: @@ -81,7 +81,7 @@ def exercise_detail_etag(client, business_id): initial = first.result captured_etag = first.response_etag initial_id = initial.business_id if initial is not None else "" - print(" Call 1 (no Etag): captured Etag={}, businessId={}".format( + print(" Call 1 (no Etag): captured Etag={}, business_id={}".format( display(captured_etag), initial_id)) if not captured_etag: @@ -93,7 +93,7 @@ def exercise_detail_etag(client, business_id): try: client.send_business_detail_lookup(second) second_id = second.result.business_id if second.result is not None else "" - print(" Call 2 (matching Etag): 200 — server did NOT honor the conditional. businessId={}, Etag={}".format( + print(" Call 2 (matching Etag): 200 — server did NOT honor the conditional. business_id={}, Etag={}".format( second_id, display(second.response_etag))) except NotModifiedError as ex: print(" Call 2 (matching Etag): 304 NotModifiedError — caller treats this as cache-valid. Refreshed Etag={}".format( @@ -107,7 +107,7 @@ def exercise_detail_etag(client, business_id): try: client.send_business_detail_lookup(third) third_id = third.result.business_id if third.result is not None else "" - print(" Call 3 (mutated Etag): 200 as expected. businessId={}, Etag={}".format( + print(" Call 3 (mutated Etag): 200 as expected. business_id={}, Etag={}".format( third_id, display(third.response_etag))) except NotModifiedError: print(" Call 3 (mutated Etag): 304 — UNEXPECTED. Server treated a different Etag as matching.") diff --git a/examples/us_enrichment_generic_example.py b/examples/us_enrichment_generic_example.py index 5c66b38..248cca2 100644 --- a/examples/us_enrichment_generic_example.py +++ b/examples/us_enrichment_generic_example.py @@ -12,7 +12,7 @@ def run(): credentials = BasicAuthCredentials(auth_id, auth_token) client = ClientBuilder(credentials).build_us_enrichment_api_client() - # send_generic_lookup routes a Lookup to any dataset/dataSubset that the + # send_generic_lookup routes a Lookup to any dataset/data_subset that the # Enrichment API exposes, including new endpoints that may not yet have a # dedicated helper on the client. smarty_key = "87844267" diff --git a/smartystreets_python_sdk/us_enrichment/client.py b/smartystreets_python_sdk/us_enrichment/client.py index 38b079e..0ac011b 100644 --- a/smartystreets_python_sdk/us_enrichment/client.py +++ b/smartystreets_python_sdk/us_enrichment/client.py @@ -30,7 +30,7 @@ def send_property_principal_lookup(self, lookup): return l.result else: lookup.dataset = 'property' - lookup.dataSubset = 'principal' + lookup.data_subset = 'principal' send_lookup(self, lookup) return lookup.result @@ -41,7 +41,7 @@ def send_geo_reference_lookup(self, lookup): return l.result else: lookup.dataset = 'geo-reference' - lookup.dataSubset = None + lookup.data_subset = None send_lookup(self, lookup) return lookup.result @@ -52,7 +52,7 @@ def send_secondary_lookup(self, lookup): return l.result else: lookup.dataset = 'secondary' - lookup.dataSubset = None + lookup.data_subset = None send_lookup(self, lookup) return lookup.result @@ -63,18 +63,18 @@ def send_secondary_count_lookup(self, lookup): return l.result else: lookup.dataset = 'secondary' - lookup.dataSubset = 'count' + lookup.data_subset = 'count' send_lookup(self, lookup) return lookup.result - def send_generic_lookup(self, lookup, dataset, dataSubset): + def send_generic_lookup(self, lookup, dataset, data_subset): if isinstance(lookup, str): - l = Lookup(lookup, dataset, dataSubset) + l = Lookup(lookup, dataset, data_subset) send_lookup(self, l) return l.result else: lookup.dataset = dataset - lookup.dataSubset = dataSubset + lookup.data_subset = data_subset send_lookup(self, lookup) return lookup.result @@ -85,7 +85,7 @@ def send_business_lookup(self, lookup): return l.result else: lookup.dataset = 'business' - lookup.dataSubset = None + lookup.data_subset = None send_lookup(self, lookup, BusinessSummaryResponse) return lookup.result @@ -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") request = build_request(lookup) raw = _dispatch(client, request, lookup) @@ -151,14 +152,14 @@ def _url_components(lookup): base = lookup.smartykey + "/" + lookup.dataset else: base = 'search/' + lookup.dataset - if lookup.dataSubset is None: + if lookup.data_subset is None: return base - return base + "/" + lookup.dataSubset + return base + "/" + lookup.data_subset def _address_parameters(lookup): params = {} - for key in ('freeform', 'street', 'city', 'state', 'zipcode', 'features'): + for key in ('freeform', 'street', 'city', 'state', 'zipcode', 'features', 'business_name'): value = getattr(lookup, key, None) if value: params[key] = value diff --git a/smartystreets_python_sdk/us_enrichment/lookup.py b/smartystreets_python_sdk/us_enrichment/lookup.py index 8268767..aa0f019 100644 --- a/smartystreets_python_sdk/us_enrichment/lookup.py +++ b/smartystreets_python_sdk/us_enrichment/lookup.py @@ -28,11 +28,11 @@ 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, data_subset=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.data_subset = data_subset self.features = features self.freeform = freeform self.street = street @@ -40,6 +40,7 @@ def __init__(self, smartykey=None, dataset=None, dataSubset=None, features=None, self.state = state self.zipcode = zipcode self.result = [] + self.business_name = business_name class PrincipalLookup(Lookup): diff --git a/test/us_enrichment/business_summary_test.py b/test/us_enrichment/business_summary_test.py index c141634..a454025 100644 --- a/test/us_enrichment/business_summary_test.py +++ b/test/us_enrichment/business_summary_test.py @@ -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) diff --git a/test/us_enrichment/client_test.py b/test/us_enrichment/client_test.py index 23c5eb0..091da3a 100644 --- a/test/us_enrichment/client_test.py +++ b/test/us_enrichment/client_test.py @@ -24,7 +24,7 @@ def test_sending_fully_populated_Enrichment_Lookup(self): request = capturing_sender.request self.assertEqual("property", lookup.dataset) - self.assertEqual("principal", lookup.dataSubset) + self.assertEqual("principal", lookup.data_subset) self.assertEqual('1', request.parameters['custom']) self.assertEqual('2', request.parameters['custom2']) self.assertEqual('3,4', request.parameters['include']) @@ -44,7 +44,7 @@ def test_sending_principal_lookup(self): result = send_lookup(client, lookup) self.assertEqual("property", lookup.dataset) - self.assertEqual("principal", lookup.dataSubset) + self.assertEqual("principal", lookup.data_subset) self.assertEqual(lookup.result, result) function_result = client.send_property_principal_lookup("xxx") @@ -64,7 +64,7 @@ def test_sending_principal_address_lookup(self): result = send_lookup(client, lookup) self.assertEqual("property", lookup.dataset) - self.assertEqual("principal", lookup.dataSubset) + self.assertEqual("principal", lookup.data_subset) self.assertEqual(lookup.result, result) function_result = client.send_property_principal_lookup(lookup) @@ -80,7 +80,7 @@ def test_sending_geo_reference_lookup(self): result = send_lookup(client, lookup) self.assertEqual("geo-reference", lookup.dataset) - self.assertEqual(None, lookup.dataSubset) + self.assertEqual(None, lookup.data_subset) self.assertEqual(lookup.result, result) function_result = client.send_geo_reference_lookup("xxx") @@ -100,7 +100,7 @@ def test_sending_geo_reference_address_lookup(self): result = send_lookup(client, lookup) self.assertEqual("geo-reference", lookup.dataset) - self.assertEqual(None, lookup.dataSubset) + self.assertEqual(None, lookup.data_subset) self.assertEqual(lookup.result, result) function_result = client.send_geo_reference_lookup(lookup) @@ -116,7 +116,7 @@ def test_sending_secondary_lookup(self): result = send_lookup(client, lookup) self.assertEqual("secondary", lookup.dataset) - self.assertEqual(None, lookup.dataSubset) + self.assertEqual(None, lookup.data_subset) self.assertEqual(lookup.result, result) function_result = client.send_secondary_lookup("xxx") @@ -136,7 +136,7 @@ def test_sending_secondary_address_lookup(self): result = send_lookup(client, lookup) self.assertEqual("secondary", lookup.dataset) - self.assertEqual(None, lookup.dataSubset) + self.assertEqual(None, lookup.data_subset) self.assertEqual(lookup.result, result) function_result = client.send_secondary_lookup(lookup) @@ -152,7 +152,7 @@ def test_sending_secondary_count_lookup(self): result = send_lookup(client, lookup) self.assertEqual("secondary", lookup.dataset) - self.assertEqual("count", lookup.dataSubset) + self.assertEqual("count", lookup.data_subset) self.assertEqual(lookup.result, result) function_result = client.send_secondary_count_lookup("xxx") @@ -172,7 +172,7 @@ def test_sending_secondary_count_address_lookup(self): result = send_lookup(client, lookup) self.assertEqual("secondary", lookup.dataset) - self.assertEqual("count", lookup.dataSubset) + self.assertEqual("count", lookup.data_subset) self.assertEqual(lookup.result, result) function_result = client.send_secondary_count_lookup(lookup)