Skip to content

Commit

Permalink
Support iac-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
XanSmarty committed Nov 2, 2023
1 parent d83ffc2 commit 7a1d07c
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 57 deletions.
12 changes: 8 additions & 4 deletions examples/international_autocomplete_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__":
Expand Down
2 changes: 1 addition & 1 deletion smartystreets_python_sdk/client_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 5 additions & 7 deletions smartystreets_python_sdk/international_autocomplete/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down

This file was deleted.

14 changes: 3 additions & 11 deletions smartystreets_python_sdk/international_autocomplete/lookup.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
5 changes: 4 additions & 1 deletion smartystreets_python_sdk/url_prefix_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 6 additions & 6 deletions test/international_autocomplete/candidate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
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)

self.assertIsNotNone(suggestion)
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)
22 changes: 6 additions & 16 deletions test/international_autocomplete/client_test.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
Expand All @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions test/url_prefix_sender_test.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7a1d07c

Please sign in to comment.