def get_address_from_freeform(address): client = Client(auth_id=SMARTY_STREETS_AUTH_ID, auth_token=SMARTY_STREETS_AUTH_TOKEN) response = client.street_address(str(address)) if not response or not response.get('analysis', False) or \ response['analysis'].get('active', 'N') != 'Y': raise ValidationError("could not validate freeform address", payload={"address": address}) return response
class TestClient(unittest.TestCase): def setUp(self): self.client = Client(auth_id='blah', auth_token='blibbidy') @responses.activate def test_input_error(self): responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='', status=400, content_type='application/json') self.assertRaises(SmartyStreetsInputError, self.client.street_addresses, [{}, {}]) @responses.activate def test_auth_error(self): responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='', status=401, content_type='application/json') self.assertRaises(SmartyStreetsAuthError, self.client.street_addresses, [{}, {}]) @responses.activate def test_payment_error(self): responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='', status=402, content_type='application/json') self.assertRaises(SmartyStreetsPaymentError, self.client.street_addresses, [{}, {}]) @responses.activate def test_server_error(self): responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='', status=500, content_type='application/json') self.assertRaises(SmartyStreetsServerError, self.client.street_addresses, [{}, {}]) @responses.activate def test_one_address(self): """Ensure singluar street address method returns an Address""" responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='[{"street_address": "100 Main St"}]', status=200, content_type='application/json') response = self.client.street_address({"street": "100 Main st"}) self.assertIsInstance(response, Address) @responses.activate def test_addresses_response(self): """Ensure address return an AddressCollection""" responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='[{"street_address": "100 Main St"}, {"street_address": "200 Main St"}]', status=200, content_type='application/json') response = self.client.street_addresses([{"street": "100 Main st"}, {"street": "200 Main St"}]) self.assertIsInstance(response, AddressCollection) self.assertEqual(2, len(response))
class TestZipcode(unittest.TestCase): def setUp(self): self.client = Client(auth_id='blah', auth_token='blibbidy') @responses.activate def test_input_error(self): responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='', status=400, content_type='application/json') self.assertRaises(SmartyStreetsInputError, self.client.zipcodes, [{}, {}]) @responses.activate def test_auth_error(self): responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='', status=401, content_type='application/json') self.assertRaises(SmartyStreetsAuthError, self.client.zipcodes, [{}, {}]) @responses.activate def test_payment_error(self): responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='', status=402, content_type='application/json') self.assertRaises(SmartyStreetsPaymentError, self.client.zipcodes, [{}, {}]) @responses.activate def test_server_error(self): responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='', status=500, content_type='application/json') self.assertRaises(SmartyStreetsServerError, self.client.zipcodes, [{}, {}]) @responses.activate def test_one_zipcode(self): """Ensure singular zipcode method returns a Zipcode""" responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='[{"city_states": [{"mailable_city": true, "city": "New York", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Empire State", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Gpo", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Greeley Square", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Macys Finance", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Manhattan", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "New York City", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "NY", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "NY City", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Nyc", "state": "New York", "state_abbreviation": "NY"}], "input_index": 0, "zipcodes": [{"county_fips": "36061", "zipcode_type": "S", "county_name": "New York", "zipcode": "10001", "longitude": -73.98935, "precision": "Zip5", "default_city": "New York", "latitude": 40.74949}]}]', status=200, content_type='application/json') response = self.client.zipcode({"zipcode": "10001"}) self.assertIsInstance(response, Zipcode) @responses.activate def test_zipcodes_response(self): """Ensure zipcodes return a ZipcodeCollection""" responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='[{"zipcode": "10001"}, {"zipcode": "10002"}]', status=200, content_type='application/json') response = self.client.zipcodes([{"zipcode": "10001"}, {"zipcode": "10002"}]) self.assertIsInstance(response, ZipcodeCollection) self.assertEqual(2, len(response))
def get_address_components(address, city, state, zip): client = Client(auth_id=SMARTY_STREETS_AUTH_ID, auth_token=SMARTY_STREETS_AUTH_TOKEN) # reassemble components into string # smarty streets specifically wants strings (not unicode) so... full_address = "%(address)s, %(city)s, %(state)s, %(zip)s" % \ {'address': str(address), 'city': str(city), 'state': str(state), 'zip': str(zip)} response = client.street_address(str(full_address)) if not response or not response.get('analysis', False) or \ response['analysis'].get('active', 'N') != 'Y': raise ValidationError("could not validate address", payload={ "address": address, "city": city, "state": state, "zip": zip }) # merge county into components dict d = response['components'] d['county_name'] = response['metadata']['county_name'] return d
def setUp(self): self.client = Client(auth_id='blah', auth_token='blibbidy')
class TestStreetAddress(unittest.TestCase): def setUp(self): self.client = Client(auth_id='blah', auth_token='blibbidy') @responses.activate def test_input_error(self): responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='', status=400, content_type='application/json') self.assertRaises(SmartyStreetsInputError, self.client.street_addresses, [{}, {}]) @responses.activate def test_auth_error(self): responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='', status=401, content_type='application/json') self.assertRaises(SmartyStreetsAuthError, self.client.street_addresses, [{}, {}]) @responses.activate def test_payment_error(self): responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='', status=402, content_type='application/json') self.assertRaises(SmartyStreetsPaymentError, self.client.street_addresses, [{}, {}]) @responses.activate def test_server_error(self): responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='', status=500, content_type='application/json') self.assertRaises(SmartyStreetsServerError, self.client.street_addresses, [{}, {}]) @responses.activate def test_one_address(self): """Ensure singluar street address method returns an Address""" responses.add(responses.POST, 'https://api.smartystreets.com/street-address', body='[{"street_address": "100 Main St"}]', status=200, content_type='application/json') response = self.client.street_address({"street": "100 Main st"}) self.assertIsInstance(response, Address) @responses.activate def test_addresses_response(self): """Ensure address return an AddressCollection""" responses.add( responses.POST, 'https://api.smartystreets.com/street-address', body= '[{"street_address": "100 Main St"}, {"street_address": "200 Main St"}]', status=200, content_type='application/json') response = self.client.street_addresses([{ "street": "100 Main st" }, { "street": "200 Main St" }]) self.assertIsInstance(response, AddressCollection) self.assertEqual(2, len(response))
class TestZipcode(unittest.TestCase): def setUp(self): self.client = Client(auth_id='blah', auth_token='blibbidy') @responses.activate def test_input_error(self): responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='', status=400, content_type='application/json') self.assertRaises(SmartyStreetsInputError, self.client.zipcodes, [{}, {}]) @responses.activate def test_auth_error(self): responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='', status=401, content_type='application/json') self.assertRaises(SmartyStreetsAuthError, self.client.zipcodes, [{}, {}]) @responses.activate def test_payment_error(self): responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='', status=402, content_type='application/json') self.assertRaises(SmartyStreetsPaymentError, self.client.zipcodes, [{}, {}]) @responses.activate def test_server_error(self): responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='', status=500, content_type='application/json') self.assertRaises(SmartyStreetsServerError, self.client.zipcodes, [{}, {}]) @responses.activate def test_one_zipcode(self): """Ensure singular zipcode method returns a Zipcode""" responses.add( responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body= '[{"city_states": [{"mailable_city": true, "city": "New York", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Empire State", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Gpo", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Greeley Square", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Macys Finance", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Manhattan", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "New York City", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "NY", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "NY City", "state": "New York", "state_abbreviation": "NY"}, {"mailable_city": false, "city": "Nyc", "state": "New York", "state_abbreviation": "NY"}], "input_index": 0, "zipcodes": [{"county_fips": "36061", "zipcode_type": "S", "county_name": "New York", "zipcode": "10001", "longitude": -73.98935, "precision": "Zip5", "default_city": "New York", "latitude": 40.74949}]}]', status=200, content_type='application/json') response = self.client.zipcode({"zipcode": "10001"}) self.assertIsInstance(response, Zipcode) @responses.activate def test_zipcodes_response(self): """Ensure zipcodes return a ZipcodeCollection""" responses.add(responses.POST, 'https://us-zipcode.api.smartystreets.com/lookup', body='[{"zipcode": "10001"}, {"zipcode": "10002"}]', status=200, content_type='application/json') response = self.client.zipcodes([{ "zipcode": "10001" }, { "zipcode": "10002" }]) self.assertIsInstance(response, ZipcodeCollection) self.assertEqual(2, len(response))