def run(): auth_id = os.environ[ 'SMARTY_AUTH_ID'] # We recommend storing your keys in environment variables auth_token = os.environ['SMARTY_AUTH_TOKEN'] credentials = StaticCredentials(auth_id, auth_token) client = ClientBuilder(credentials).build_us_zipcode_api_client() lookup = Lookup() lookup.city = "Mountain View" lookup.state = "California" try: client.send_lookup(lookup) except exceptions.SmartyException as err: print(err) return result = lookup.result zipcodes = result.zipcodes cities = result.cities for city in cities: print("\nCity: " + city.city) print("State: " + city.state) print("Mailable City: {}".format(city.mailable_city)) for zipcode in zipcodes: print("\nZIP Code: " + zipcode.zipcode) print("Latitude: {}".format(zipcode.latitude)) print("Longitude: {}".format(zipcode.longitude))
def getZipCode(cityName, stateName): auth_id = "64c1b167-5f64-f1ab-0898-5a13c92e2359" auth_token = "WfTDEo4wyzYEaOeoe3FV" # We recommend storing your secret keys in environment variables instead---it's safer! # auth_id = os.environ['SMARTY_AUTH_ID'] # auth_token = os.environ['SMARTY_AUTH_TOKEN'] credentials = StaticCredentials(auth_id, auth_token) client = ClientBuilder(credentials).build_us_zipcode_api_client() # Documentation for input fields can be found at: # https://smartystreet.com/docs/us-zipcode-api#input-fields lookup = ZIPCodeLookup() lookup.input_id = "dfc33cb6-829e-4fea-aa1b-b6d6580f0817" # Optional ID from your system lookup.city = cityName lookup.state = stateName try: client.send_lookup(lookup) except exceptions.SmartyException as err: print(err) return result = lookup.result zipcodes = result.zipcodes cities = result.cities # for zipcode in zipcodes: # print("\nZIP Code: " + zipcode.zipcode) return zipcodes
def run(): auth_id = os.environ[ 'SMARTY_AUTH_ID'] # We recommend storing your keys in environment variables auth_token = os.environ['SMARTY_AUTH_TOKEN'] credentials = StaticCredentials(auth_id, auth_token) client = ClientBuilder(credentials).build_us_zipcode_api_client() batch = Batch() batch.add(Lookup()) batch[ 0].zipcode = "12345" # A Lookup may have a ZIP Code, city and state, or city, state, and ZIP Code batch.add(Lookup()) batch[1].city = "Phoenix" batch[1].state = "Arizona" batch.add(Lookup("cupertino", "CA", "95014")) # You can also set these with arguments assert len(batch) == 3 try: client.send_batch(batch) except exceptions.SmartyException as err: print(err) return for i, lookup in enumerate(batch): result = lookup.result print("Lookup {}:\n".format(i)) if result.status is not None: print("Status: " + result.status) print("Reason: " + result.reason) continue cities = result.cities print("{} City and State match(es):".format(len(cities))) for city in cities: print("City: " + city.city) print("State: " + city.state) print("Mailable City: {}".format(city.mailable_city)) print("") zipcodes = result.zipcodes print("{} ZIP Code match(es):".format(len(zipcodes))) for zipcode in zipcodes: print("ZIP Code: " + zipcode.zipcode) print("County: " + zipcode.county_name) print("Latitude: {}".format(zipcode.latitude)) print("Longitude: {}".format(zipcode.longitude)) print("") print("***********************************")
def test_Successfully_Sends_Batch(self): expected_payload = "Hello, World!" sender = RequestCapturingSender() serializer = FakeSerializer(expected_payload) client = Client(sender, serializer) batch = Batch() batch.add(Lookup()) batch.add(Lookup()) client.send_batch(batch) self.assertEqual(expected_payload, sender.request.payload)
def test_single_lookup_values_correctly_assigned_to_parameters(self): sender = RequestCapturingSender() serializer = FakeDeserializer({}) client = Client(sender, serializer) lookup = Lookup() lookup.city = '0' lookup.state = '1' lookup.zipcode = '2' client.send_lookup(lookup) self.assertEqual('0', sender.request.parameters['city']) self.assertEqual('1', sender.request.parameters['state']) self.assertEqual('2', sender.request.parameters['zipcode'])
def test_results_correctly_assigned_to_corresponding_lookup(self): raw_results = [{"input_index": 0}, {"input_index": 1}] expected_results = [Result(raw_results[0]), Result(raw_results[1])] batch = Batch() batch.add(Lookup()) batch.add(Lookup()) sender = MockSender(Response("[]", 0)) deserializer = FakeDeserializer(raw_results) client = Client(sender, deserializer) client.send_batch(batch) self.assertEqual(expected_results[0].input_index, batch[0].result.input_index) self.assertEqual(expected_results[1].input_index, batch[1].result.input_index)
def get_zip_code(self, town_info): lookup = Lookup() lookup.input_id = "dfc33cb6-829e-4fea-aa1b-b6d6580f0817" lookup.city = town_info["city"] lookup.state = town_info["state"] try: self._zip_client.send_lookup(lookup) except exceptions.SmartyException as err: print(err) return result_json = self._zip_lookup_to_json(lookup) result = result_json return result
def test_deserialize_called_with_response_body(self): response = Response("Hello, World!", 0) sender = MockSender(response) deserializer = FakeDeserializer(None) client = Client(sender, deserializer) client.send_lookup(Lookup()) self.assertEqual(response.payload, deserializer.input)
def run(): # auth_id = "Your SmartyStreets Auth ID here" # auth_token = "Your SmartyStreets Auth Token here" # We recommend storing your secret keys in environment variables instead---it's safer! auth_id = os.environ['SMARTY_AUTH_ID'] auth_token = os.environ['SMARTY_AUTH_TOKEN'] credentials = StaticCredentials(auth_id, auth_token) client = ClientBuilder(credentials).build_us_zipcode_api_client() # Documentation for input fields can be found at: # https://smartystreet.com/docs/us-zipcode-api#input-fields lookup = ZIPCodeLookup() lookup.input_id = "dfc33cb6-829e-4fea-aa1b-b6d6580f0817" # Optional ID from your system lookup.city = "Mountain View" lookup.state = "California" lookup.zipcode = "94043" try: client.send_lookup(lookup) except exceptions.SmartyException as err: print(err) return result = lookup.result zipcodes = result.zipcodes cities = result.cities for city in cities: print("\nCity: " + city.city) print("State: " + city.state) print("Mailable City: {}".format(city.mailable_city)) for zipcode in zipcodes: print("\nZIP Code: " + zipcode.zipcode) print("Latitude: {}".format(zipcode.latitude)) print("Longitude: {}".format(zipcode.longitude))
def test_raises_exception_when_response_has_error(self): exception = exceptions.BadCredentialsError client = Client(MockExceptionSender(exception), FakeSerializer(None)) self.assertRaises(exception, client.send_lookup, Lookup())
def run(): auth_id = "Your SmartyStreets Auth ID here" auth_token = "Your SmartyStreets Auth Token here" # We recommend storing your secret keys in environment variables instead---it's safer! # auth_id = os.environ['SMARTY_AUTH_ID'] # auth_token = os.environ['SMARTY_AUTH_TOKEN'] credentials = StaticCredentials(auth_id, auth_token) client = ClientBuilder(credentials).build_us_zipcode_api_client() batch = Batch() # Documentation for input fields can be found at: # https://smartystreets.com/docs/us-zipcode-api#input-fields batch.add(Lookup()) batch[0].input_id = "011889998819991197253" # Optional ID from your system batch[ 0].zipcode = "12345" # A Lookup may have a ZIP Code, city and state, or city, state, and ZIP Code batch.add(Lookup()) batch[1].city = "Phoenix" batch[1].state = "Arizona" batch.add(Lookup("cupertino", "CA", "95014")) # You can also set these with arguments assert len(batch) == 3 try: client.send_batch(batch) except exceptions.SmartyException as err: print(err) return for i, lookup in enumerate(batch): result = lookup.result print("Lookup {}:\n".format(i)) if result.status is not None: print("Status: " + result.status) print("Reason: " + result.reason) continue cities = result.cities print("{} City and State match(es):".format(len(cities))) for city in cities: print("City: " + city.city) print("State: " + city.state) print("Mailable City: {}".format(city.mailable_city)) print("") zipcodes = result.zipcodes print("{} ZIP Code match(es):".format(len(zipcodes))) for zipcode in zipcodes: print("ZIP Code: " + zipcode.zipcode) print("County: " + zipcode.county_name) print("Latitude: {}".format(zipcode.latitude)) print("Longitude: {}".format(zipcode.longitude)) print("") print("***********************************")