diff --git a/examples/international_autocomplete_example.py b/examples/international_autocomplete_example.py index 4ce0e94..9562186 100644 --- a/examples/international_autocomplete_example.py +++ b/examples/international_autocomplete_example.py @@ -34,8 +34,10 @@ def run(): print('*** Result with no filter ***') print() for suggestion in lookup.result: - print(suggestion.street + " " + suggestion.locality, suggestion.administrative_area, - suggestion.super_administrative_area, suggestion.sub_administrative_area, sep=", ") + if suggestion.address_text: + print(str(suggestion.entries) + " " + suggestion.address_text + " " + suggestion.address_id) + else: + print(suggestion.street + " " + suggestion.locality, suggestion.administrative_area, sep=", ") # Documentation for input fields can be found at: # https://smartystreets.com/docs/us-autocomplete-api#http-request-input-fields @@ -47,8 +49,10 @@ def run(): print() print('*** Result with some filters ***') for suggestion in suggestions: - print(suggestion.street + " " + suggestion.locality, suggestion.administrative_area, - suggestion.super_administrative_area, suggestion.sub_administrative_area, sep=", ") + if suggestion.address_text: + print(str(suggestion.entries) + " " + suggestion.address_text + " " + suggestion.address_id) + else: + print(suggestion.street + " " + suggestion.locality, suggestion.administrative_area, sep=", ") if __name__ == "__main__": diff --git a/smartystreets_python_sdk/client_builder.py b/smartystreets_python_sdk/client_builder.py index 86bb4f3..c36b2f7 100644 --- a/smartystreets_python_sdk/client_builder.py +++ b/smartystreets_python_sdk/client_builder.py @@ -27,7 +27,7 @@ def __init__(self, signer): self.header = None self.licenses = [] self.INTERNATIONAL_STREET_API_URL = "https://international-street.api.smarty.com/verify" - self.INTERNATIONAL_AUTOCOMPLETE_API_URL = "https://international-autocomplete.api.smarty.com/lookup" + self.INTERNATIONAL_AUTOCOMPLETE_API_URL = "https://international-autocomplete.api.smarty.com/v2/lookup" self.US_AUTOCOMPLETE_API_URL = "https://us-autocomplete.api.smarty.com/suggest" self.US_AUTOCOMPLETE_PRO_API_URL = "https://us-autocomplete-pro.api.smarty.com/lookup" self.US_EXTRACT_API_URL = "https://us-extract.api.smarty.com" diff --git a/smartystreets_python_sdk/international_autocomplete/candidate.py b/smartystreets_python_sdk/international_autocomplete/candidate.py index fed77f2..ff022eb 100644 --- a/smartystreets_python_sdk/international_autocomplete/candidate.py +++ b/smartystreets_python_sdk/international_autocomplete/candidate.py @@ -3,7 +3,9 @@ def __init__(self, obj=None): self.street = obj.get('street', None) self.locality = obj.get('locality', None) self.administrative_area = obj.get('administrative_area', None) - self.super_administrative_area = obj.get('super_administrative_area', None) - self.sub_administrative_area = obj.get('sub_administrative_area', None) self.postal_code = obj.get('postal_code', None) self.country_iso3 = obj.get('country_iso3', None) + # v2 fields + self.entries = obj.get('entries', None) + self.address_text = obj.get('address_text', None) + self.address_id = obj.get('address_id', None) diff --git a/smartystreets_python_sdk/international_autocomplete/client.py b/smartystreets_python_sdk/international_autocomplete/client.py index 7b06a0a..bd8ec75 100644 --- a/smartystreets_python_sdk/international_autocomplete/client.py +++ b/smartystreets_python_sdk/international_autocomplete/client.py @@ -15,8 +15,8 @@ def send(self, lookup): """ Sends a Lookup object to the International Autocomplete API and stores the result in the Lookup's result field. """ - if not lookup or not lookup.search: - raise SmartyException('Send() must be passed a Lookup with the search field set.') + if not lookup or (not lookup.search and not lookup.address_id): + raise SmartyException('Send() must be passed a Lookup with country set, and search or address_id set.') request = self.build_request(lookup) @@ -34,16 +34,14 @@ def send(self, lookup): def build_request(self, lookup): request = Request() + if lookup.address_id is not None: + request.url_prefix = "/" + lookup.address_id + self.add_parameter(request, 'country', lookup.country) self.add_parameter(request, 'search', lookup.search) self.add_parameter(request, 'max_results', lookup.max_results) - self.add_parameter(request, 'distance', lookup.distance) - self.add_parameter(request, 'geolocation', lookup.geolocation.value) - self.add_parameter(request, 'include_only_administrative_area', lookup.administrative_area) self.add_parameter(request, 'include_only_locality', lookup.locality) self.add_parameter(request, 'include_only_postal_code', lookup.postal_code) - self.add_parameter(request, 'latitude', lookup.latitude) - self.add_parameter(request, 'longitude', lookup.longitude) return request diff --git a/smartystreets_python_sdk/international_autocomplete/international_geolocate_type.py b/smartystreets_python_sdk/international_autocomplete/international_geolocate_type.py deleted file mode 100644 index 6629150..0000000 --- a/smartystreets_python_sdk/international_autocomplete/international_geolocate_type.py +++ /dev/null @@ -1,9 +0,0 @@ -from enum import Enum - - -class InternationalGeolocateType(str, Enum): - ADMIN_AREA = "admin_area" - LOCALITY = "locality" - POSTAL_CODE = "postal_code" - GEOCODES = "geocodes" - NONE = "" diff --git a/smartystreets_python_sdk/international_autocomplete/lookup.py b/smartystreets_python_sdk/international_autocomplete/lookup.py index a3ae749..7690717 100644 --- a/smartystreets_python_sdk/international_autocomplete/lookup.py +++ b/smartystreets_python_sdk/international_autocomplete/lookup.py @@ -1,10 +1,6 @@ -from smartystreets_python_sdk.international_autocomplete.international_geolocate_type import InternationalGeolocateType - - class Lookup: - def __init__(self, search=None, country=None, max_results=5, distance=5, - international_geolocate_type=InternationalGeolocateType.NONE, administrative_area=None, locality=None, - postal_code=None, latitude=None, longitude=None): + def __init__(self, search=None, address_id=None, country=None, max_results=5, locality=None, + postal_code=None): """ In addition to holding all the input data for this lookup, this class also will contain the result of the lookup after it comes back from the API. @@ -18,12 +14,8 @@ def __init__(self, search=None, country=None, max_results=5, distance=5, self.result = [] self.search = search + self.address_id = address_id self.country = country self.max_results = max_results - self.distance = distance - self.geolocation = international_geolocate_type - self.administrative_area = administrative_area self.locality = locality self.postal_code = postal_code - self.latitude = latitude - self.longitude = longitude diff --git a/smartystreets_python_sdk/url_prefix_sender.py b/smartystreets_python_sdk/url_prefix_sender.py index 0f3384b..a51d848 100644 --- a/smartystreets_python_sdk/url_prefix_sender.py +++ b/smartystreets_python_sdk/url_prefix_sender.py @@ -4,6 +4,9 @@ def __init__(self, url_prefix, inner): self.inner = inner def send(self, request): - request.url_prefix = self.url_prefix + if request.url_prefix is not None: + request.url_prefix = self.url_prefix + request.url_prefix + else: + request.url_prefix = self.url_prefix return self.inner.send(request) diff --git a/test/international_autocomplete/candidate_test.py b/test/international_autocomplete/candidate_test.py index 06d8012..f64ec97 100644 --- a/test/international_autocomplete/candidate_test.py +++ b/test/international_autocomplete/candidate_test.py @@ -6,8 +6,7 @@ class TestSuggestion(unittest.TestCase): def test_all_fields_get_filled_in_correctly(self): response_dictionary = {"street": "1", "locality": "2", "administrative_area": "3", - "super_administrative_area": "4", "sub_administrative_area": "5", "postal_code": "6", - "country_iso3": "7"} + "postal_code": "4", "country_iso3": "5", "entries": "6", "address_text": "7", "address_id": "8"} suggestion = Candidate(response_dictionary) @@ -15,7 +14,8 @@ def test_all_fields_get_filled_in_correctly(self): self.assertEqual('1', suggestion.street) self.assertEqual('2', suggestion.locality) self.assertEqual('3', suggestion.administrative_area) - self.assertEqual('4', suggestion.super_administrative_area) - self.assertEqual('5', suggestion.sub_administrative_area) - self.assertEqual('6', suggestion.postal_code) - self.assertEqual('7', suggestion.country_iso3) + self.assertEqual('4', suggestion.postal_code) + self.assertEqual('5', suggestion.country_iso3) + self.assertEqual('6', suggestion.entries) + self.assertEqual('7', suggestion.address_text) + self.assertEqual('8', suggestion.address_id) diff --git a/test/international_autocomplete/client_test.py b/test/international_autocomplete/client_test.py index f2b1786..83e7184 100644 --- a/test/international_autocomplete/client_test.py +++ b/test/international_autocomplete/client_test.py @@ -1,7 +1,6 @@ import unittest from smartystreets_python_sdk import Response, exceptions -from smartystreets_python_sdk.international_autocomplete.international_geolocate_type import InternationalGeolocateType from test.mocks import * from smartystreets_python_sdk.international_autocomplete import Client, Lookup @@ -16,7 +15,6 @@ def test_sending_prefix_only_lookup(self): self.assertEqual('1', sender.request.parameters['search']) self.assertEqual(5, sender.request.parameters['max_results']) - self.assertEqual(5, sender.request.parameters['distance']) def test_sending_fully_populated_lookup(self): sender = RequestCapturingSender() @@ -25,26 +23,18 @@ def test_sending_fully_populated_lookup(self): lookup = Lookup('1') lookup.country = '2' lookup.max_results = 7 - lookup.distance = 3 - lookup.geolocation = InternationalGeolocateType.GEOCODES - lookup.administrative_area = '3' - lookup.locality = '4' - lookup.postal_code = '5' - lookup.latitude = 40.202830239007696 - lookup.longitude = -111.62195073669359 + lookup.locality = '3' + lookup.postal_code = '4' + lookup.address_id = '5' client.send(lookup) self.assertEqual('1', sender.request.parameters['search']) self.assertEqual('2', sender.request.parameters['country']) self.assertEqual(7, sender.request.parameters['max_results']) - self.assertEqual(3, sender.request.parameters['distance']) - self.assertEqual('geocodes', sender.request.parameters['geolocation']) - self.assertEqual('3', sender.request.parameters['include_only_administrative_area']) - self.assertEqual('4', sender.request.parameters['include_only_locality']) - self.assertEqual('5', sender.request.parameters['include_only_postal_code']) - self.assertEqual(40.202830239007696, sender.request.parameters['latitude']) - self.assertEqual(-111.62195073669359, sender.request.parameters['longitude']) + self.assertEqual('3', sender.request.parameters['include_only_locality']) + self.assertEqual('4', sender.request.parameters['include_only_postal_code']) + self.assertEqual('/5', sender.request.url_prefix) def test_deserialize_called_with_response_body(self): response = Response('Hello, World!', 0) diff --git a/test/url_prefix_sender_test.py b/test/url_prefix_sender_test.py new file mode 100644 index 0000000..05e815c --- /dev/null +++ b/test/url_prefix_sender_test.py @@ -0,0 +1,26 @@ +import unittest + +from smartystreets_python_sdk import URLPrefixSender +from smartystreets_python_sdk import Request + +class TestURLPrefixSender(unittest.TestCase): + def test_request_url_present(self): + fakeSender = FakeSender() + prefix_sender = URLPrefixSender('https://mysite.com', fakeSender) + request = Request() + request.url_prefix = '/jimbo' + + new_request = prefix_sender.send(request) + self.assertEqual('https://mysite.com/jimbo', new_request.url_prefix) + + def test_request_url_not_present(self): + fakeSender = FakeSender() + prefix_sender = URLPrefixSender('https://mysite.com', fakeSender) + request = Request() + + new_request = prefix_sender.send(request) + self.assertEqual('https://mysite.com', new_request.url_prefix) + +class FakeSender: + def send(self, request): + return request \ No newline at end of file