def test_single_lookup_values_correctly_assigned_to_parameters(self): sender = RequestCapturingSender() serializer = FakeDeserializer({}) client = Client(sender, serializer) lookup = Lookup() lookup.street = '0' lookup.street2 = '1' lookup.secondary = '2' lookup.city = '3' lookup.state = '4' lookup.zipcode = '5' lookup.lastline = '6' lookup.addressee = '7' lookup.urbanization = '8' lookup.match = match_type.INVALID lookup.candidates = '9' client.send_lookup(lookup) self.assertEqual('0', sender.request.parameters['street']) self.assertEqual('1', sender.request.parameters['street2']) self.assertEqual('2', sender.request.parameters['secondary']) self.assertEqual('3', sender.request.parameters['city']) self.assertEqual('4', sender.request.parameters['state']) self.assertEqual('5', sender.request.parameters['zipcode']) self.assertEqual('6', sender.request.parameters['lastline']) self.assertEqual('7', sender.request.parameters['addressee']) self.assertEqual('8', sender.request.parameters['urbanization']) self.assertEqual(match_type.INVALID, sender.request.parameters['match']) self.assertEqual('9', sender.request.parameters['candidates'])
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) # The appropriate license values to be used for you subscriptions # can be found on the Subscriptions page of the account dashboard. # https://www.smartystreets.com/docs/cloud/licensing client = ClientBuilder(credentials).with_licenses( ["us-core-cloud"]).build_us_street_api_client() # client = ClientBuilder(credentials).with_custom_header({'User-Agent': 'smartystreets ([email protected])', 'Content-Type': 'application/json'}).build_us_street_api_client() # client = ClientBuilder(credentials).with_proxy('localhost:8080', 'user', 'password').build_us_street_api_client() # Uncomment the line above to try it with a proxy instead # Documentation for input fields can be found at: # https://smartystreets.com/docs/us-street-api#input-fields lookup = StreetLookup() lookup.input_id = "24601" # Optional ID from your system lookup.addressee = "John Doe" lookup.street = "1600 Amphitheatre Pkwy" lookup.street2 = "closet under the stairs" lookup.secondary = "APT 2" lookup.urbanization = "" # Only applies to Puerto Rico addresses lookup.city = "Mountain View" lookup.state = "CA" lookup.zipcode = "94043" lookup.candidates = 3 lookup.match = "invalid" # "invalid" is the most permissive match, # this will always return at least one result even if the address is invalid. # Refer to the documentation for additional Match Strategy options. try: client.send_lookup(lookup) except exceptions.SmartyException as err: print(err) return result = lookup.result if not result: print("No candidates. This means the address is not valid.") return first_candidate = result[0] print("There is at least one candidate.") print("If the match parameter is set to STRICT, the address is valid.") print( "Otherwise, check the Analysis output fields to see if the address is valid.\n" ) print("ZIP Code: " + first_candidate.components.zipcode) print("County: " + first_candidate.metadata.county_name) print("Latitude: {}".format(first_candidate.metadata.latitude)) print("Longitude: {}".format(first_candidate.metadata.longitude))
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_street_api_client() # client = ClientBuilder(credentials).with_proxy('localhost:8080', 'user', 'password').build_us_street_api_client() # Uncomment the line above to try it with a proxy instead # Documentation for input fields can be found at: # https://smartystreets.com/docs/us-street-api#input-fields lookup = Lookup() lookup.input_id = "24601" # Optional ID from your system lookup.addressee = "John Doe" lookup.street = "1600 Amphitheatre Pkwy" lookup.street2 = "closet under the stairs" lookup.secondary = "APT 2" lookup.urbanization = "" # Only applies to Puerto Rico addresses lookup.city = "Mountain View" lookup.state = "CA" lookup.zipcode = "94043" lookup.candidates = 3 lookup.match = "Invalid" # "invalid" is the most permissive match try: client.send_lookup(lookup) except exceptions.SmartyException as err: print(err) return result = lookup.result if not result: print("No candidates. This means the address is not valid.") return first_candidate = result[0] print("Address is valid. (There is at least one candidate)\n") print("ZIP Code: " + first_candidate.components.zipcode) print("County: " + first_candidate.metadata.county_name) print("Latitude: {}".format(first_candidate.metadata.latitude)) print("Longitude: {}".format(first_candidate.metadata.longitude))
def address_validation(): found = True # 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'] session['shipping_fn'] = request.form['first_name'] session['shipping_ln'] = request.form['last_name'] input_address = { 'street': request.form['street1'] + ' ' + request.form['street2'], 'city': request.form['city'], 'state': request.form['state'], 'zip': request.form['zip_code'] } credentials = StaticCredentials(auth_id, auth_token) client = ClientBuilder(credentials).build_us_street_api_client() # client = ClientBuilder(credentials).with_custom_header({'User-Agent': 'smartystreets ([email protected])', 'Content-Type': 'application/json'}).build_us_street_api_client() # client = ClientBuilder(credentials).with_proxy('localhost:8080', 'user', 'password').build_us_street_api_client() # Uncomment the line above to try it with a proxy instead # Documentation for input fields can be found at: # https://smartystreets.com/docs/us-street-api#input-fields lookup = Lookup() #lookup.input_id = "24601" # Optional ID from your system lookup.addressee = request.form['first_name'] + ' ' + request.form[ 'last_name'] lookup.street = request.form['street1'] lookup.street2 = request.form['street2'] #lookup.secondary = "APT 2" lookup.urbanization = "" # Only applies to Puerto Rico addresses lookup.city = request.form['city'] lookup.state = request.form['state'] lookup.zipcode = request.form['zip_code'] lookup.candidates = 3 lookup.match = "Invalid" # "invalid" is the most permissive match try: client.send_lookup(lookup) except exceptions.SmartyException as err: print(err) return result = lookup.result if not result: found = False print("No candidates. This means the address is not valid.") flash('Address is not valid. Please re-enter shipping information.') return render_template('partials/address.html', found=found) #for output example fields here https://smartystreets.com/docs/cloud/us-street-api#http-response-status first_candidate = result[0] print("Address is valid. (There is at least one candidate)\n") suggested_address_line1 = first_candidate.delivery_line_1 suggested_address_line2 = first_candidate.components.city_name + ", " + first_candidate.components.state_abbreviation + " " + first_candidate.components.zipcode print(first_candidate.delivery_line_1) print(suggested_address_line1) print(first_candidate.components.city_name + ", " + first_candidate.components.state_abbreviation + " " + first_candidate.components.zipcode) return render_template('partials/address.html', found=found, suggested_address_line1=suggested_address_line1, suggested_address_line2=suggested_address_line2, input_address=input_address)
def run(): auth_id = context.get_context("auth_id") auth_token = context.get_context("auth_token") # 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) # The appropriate license values to be used for you subscriptions # can be found on the Subscriptions page of the account dashboard. # https://www.smartystreets.com/docs/cloud/licensing client = ClientBuilder(credentials).with_licenses(["us-standard-cloud"]).build_us_street_api_client() # client = ClientBuilder(credentials).with_custom_header({'User-Agent': 'smartystreets ([email protected])', 'Content-Type': 'application/json'}).build_us_street_api_client() # client = ClientBuilder(credentials).with_proxy('localhost:8080', 'user', 'password').build_us_street_api_client() # Uncomment the line above to try it with a proxy instead # Documentation for input fields can be found at: # https://smartystreets.com/docs/us-street-api#input-fields lookup = StreetLookup() lookup.input_id = "24601" # Optional ID from your system lookup.addressee = "John Doe" lookup.street = "1600 Amphitheatre Pkwy" lookup.street2 = "closet under the stairs" lookup.secondary = "APT 2" lookup.urbanization = "" # Only applies to Puerto Rico addresses lookup.city = "Mountain View" lookup.state = "CA" lookup.zipcode = "94043" lookup.candidates = 3 lookup.match = "invalid" # "invalid" is the most permissive match, # this will always return at least one result even if the address is invalid. # Refer to the documentation for additional Match Strategy options. try: client.send_lookup(lookup) except exceptions.SmartyException as err: print(err) return result = lookup.result if not result: print("No candidates. This means the address is not valid.") return first_candidate = result[0] print("Address is valid. (There is at least one candidate)\n") #print("Address = ", json.dumps(first_candidate.components)) print("ZIP Code: " + first_candidate.components.zipcode) print("County: " + first_candidate.metadata.county_name) print("Latitude: {}".format(first_candidate.metadata.latitude)) print("Longitude: {}".format(first_candidate.metadata.longitude)) # print("Precision: {}".format(first_candidate.metadata.precision)) # print("Residential: {}".format(first_candidate.metadata.rdi)) # print("Vacant: {}".format(first_candidate.analysis.dpv_vacant)) # Complete list of output fields is available here: https://smartystreets.com/docs/cloud/us-street-api#http-response-output sm = SmartyStreetsAdaptor(result) res = sm.to_json() print("All fields = \n", json.dumps(res, indent=2, default=str))