|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import os |
| 3 | +import json |
| 4 | + |
| 5 | +from smartystreets_python_sdk import SharedCredentials, StaticCredentials, exceptions, ClientBuilder |
| 6 | +from smartystreets_python_sdk.us_street import Lookup as StreetLookup |
| 7 | +from smartystreets_python_sdk.us_street.match_type import MatchType |
| 8 | + |
| 9 | + |
| 10 | +def run(): |
| 11 | + # For client-side requests (browser/mobile), use this code: |
| 12 | + # key = os.environ['SMARTY_AUTH_WEB'] |
| 13 | + # hostname = os.environ['SMARTY_WEBSITE_DOMAIN'] |
| 14 | + # credentials = SharedCredentials(key, hostname) |
| 15 | + |
| 16 | + # For server-to-server requests, use this code: |
| 17 | + auth_id = os.environ['SMARTY_AUTH_ID'] |
| 18 | + auth_token = os.environ['SMARTY_AUTH_TOKEN'] |
| 19 | + credentials = StaticCredentials(auth_id, auth_token) |
| 20 | + |
| 21 | + client = ( ClientBuilder(credentials) |
| 22 | + .with_feature_component_analysis() # To add component analysis feature you need to specify when you create the client. |
| 23 | + .build_us_street_api_client() |
| 24 | + ) |
| 25 | + |
| 26 | + lookup = StreetLookup() |
| 27 | + lookup.street = "1 Rosedale" |
| 28 | + lookup.secondary = "APT 2" |
| 29 | + lookup.city = "Baltimore" |
| 30 | + lookup.state = "MD" |
| 31 | + lookup.zipcode = "21229" |
| 32 | + lookup.match = MatchType.ENHANCED # Enhanced matching is required to return component analysis results. |
| 33 | + |
| 34 | + try: |
| 35 | + client.send_lookup(lookup) |
| 36 | + except exceptions.SmartyException as err: |
| 37 | + print(err) |
| 38 | + return |
| 39 | + |
| 40 | + result = lookup.result |
| 41 | + |
| 42 | + if not result: |
| 43 | + return |
| 44 | + |
| 45 | + first_candidate = result[0] |
| 46 | + |
| 47 | + # Here is an example of how to access the result of component analysis. |
| 48 | + componentAnalysis = first_candidate.analysis.components |
| 49 | + print("Component Analysis Results:") |
| 50 | + print(json.dumps(componentAnalysis.__dict__, default=lambda o: o.__dict__, indent=2)) |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + run() |
0 commit comments