|
| 1 | +import os |
| 2 | + |
| 3 | +from smartystreets_python_sdk import BasicAuthCredentials, ClientBuilder |
| 4 | +from smartystreets_python_sdk.exceptions import NotModifiedError |
| 5 | +from smartystreets_python_sdk.us_enrichment import BusinessDetailLookup, BusinessLookup |
| 6 | + |
| 7 | + |
| 8 | +def run(): |
| 9 | + auth_id = os.environ['SMARTY_AUTH_ID'] |
| 10 | + auth_token = os.environ['SMARTY_AUTH_TOKEN'] |
| 11 | + |
| 12 | + credentials = BasicAuthCredentials(auth_id, auth_token) |
| 13 | + client = ClientBuilder(credentials).build_us_enrichment_api_client() |
| 14 | + |
| 15 | + smarty_key = "1962995076" |
| 16 | + |
| 17 | + business_id = exercise_summary_etag(client, smarty_key) |
| 18 | + if business_id is None: |
| 19 | + return |
| 20 | + |
| 21 | + exercise_detail_etag(client, business_id) |
| 22 | + |
| 23 | + |
| 24 | +def exercise_summary_etag(client, smarty_key): |
| 25 | + print("=== Business.Summary ETag round trip ===") |
| 26 | + |
| 27 | + first = BusinessLookup(smarty_key) |
| 28 | + try: |
| 29 | + client.send_business_lookup(first) |
| 30 | + except Exception as ex: |
| 31 | + print(" Initial Summary call failed: {}".format(ex)) |
| 32 | + return None |
| 33 | + |
| 34 | + initial_results = first.result |
| 35 | + captured_etag = first.response_etag |
| 36 | + print(" Call 1 (no Etag): captured Etag={}, results={}".format( |
| 37 | + display(captured_etag), len(initial_results or []))) |
| 38 | + |
| 39 | + if not captured_etag: |
| 40 | + print(" Server did not return an Etag header; skipping conditional calls.") |
| 41 | + return first_business_id(initial_results) |
| 42 | + |
| 43 | + second = BusinessLookup(smarty_key) |
| 44 | + second.request_etag = captured_etag |
| 45 | + try: |
| 46 | + client.send_business_lookup(second) |
| 47 | + print(" Call 2 (matching Etag): 200 — server did NOT honor the conditional. Results={}, Etag={}".format( |
| 48 | + len(second.result or []), display(second.response_etag))) |
| 49 | + except NotModifiedError as ex: |
| 50 | + print(" Call 2 (matching Etag): 304 NotModifiedError — caller treats this as cache-valid. Refreshed Etag={}".format( |
| 51 | + display(ex.response_etag))) |
| 52 | + except Exception as ex: |
| 53 | + print(" Call 2 unexpected failure: {}: {}".format(type(ex).__name__, ex)) |
| 54 | + return None |
| 55 | + |
| 56 | + third = BusinessLookup(smarty_key) |
| 57 | + third.request_etag = captured_etag + "X" |
| 58 | + try: |
| 59 | + client.send_business_lookup(third) |
| 60 | + print(" Call 3 (mutated Etag): 200 as expected. Results={}, Etag={}".format( |
| 61 | + len(third.result or []), display(third.response_etag))) |
| 62 | + except NotModifiedError: |
| 63 | + print(" Call 3 (mutated Etag): 304 — UNEXPECTED. Server treated a different Etag as matching.") |
| 64 | + except Exception as ex: |
| 65 | + print(" Call 3 unexpected failure: {}: {}".format(type(ex).__name__, ex)) |
| 66 | + |
| 67 | + return first_business_id(initial_results) |
| 68 | + |
| 69 | + |
| 70 | +def exercise_detail_etag(client, business_id): |
| 71 | + print() |
| 72 | + print("=== Business.Detail ETag round trip (businessId: {}) ===".format(business_id)) |
| 73 | + |
| 74 | + first = BusinessDetailLookup(business_id) |
| 75 | + try: |
| 76 | + client.send_business_detail_lookup(first) |
| 77 | + except Exception as ex: |
| 78 | + print(" Initial Detail call failed: {}".format(ex)) |
| 79 | + return |
| 80 | + |
| 81 | + initial = first.result |
| 82 | + captured_etag = first.response_etag |
| 83 | + initial_id = initial.business_id if initial is not None else "<null>" |
| 84 | + print(" Call 1 (no Etag): captured Etag={}, businessId={}".format( |
| 85 | + display(captured_etag), initial_id)) |
| 86 | + |
| 87 | + if not captured_etag: |
| 88 | + print(" Server did not return an Etag header; skipping conditional calls.") |
| 89 | + return |
| 90 | + |
| 91 | + second = BusinessDetailLookup(business_id) |
| 92 | + second.request_etag = captured_etag |
| 93 | + try: |
| 94 | + client.send_business_detail_lookup(second) |
| 95 | + second_id = second.result.business_id if second.result is not None else "<null>" |
| 96 | + print(" Call 2 (matching Etag): 200 — server did NOT honor the conditional. businessId={}, Etag={}".format( |
| 97 | + second_id, display(second.response_etag))) |
| 98 | + except NotModifiedError as ex: |
| 99 | + print(" Call 2 (matching Etag): 304 NotModifiedError — caller treats this as cache-valid. Refreshed Etag={}".format( |
| 100 | + display(ex.response_etag))) |
| 101 | + except Exception as ex: |
| 102 | + print(" Call 2 unexpected failure: {}: {}".format(type(ex).__name__, ex)) |
| 103 | + return |
| 104 | + |
| 105 | + third = BusinessDetailLookup(business_id) |
| 106 | + third.request_etag = captured_etag + "X" |
| 107 | + try: |
| 108 | + client.send_business_detail_lookup(third) |
| 109 | + third_id = third.result.business_id if third.result is not None else "<null>" |
| 110 | + print(" Call 3 (mutated Etag): 200 as expected. businessId={}, Etag={}".format( |
| 111 | + third_id, display(third.response_etag))) |
| 112 | + except NotModifiedError: |
| 113 | + print(" Call 3 (mutated Etag): 304 — UNEXPECTED. Server treated a different Etag as matching.") |
| 114 | + except Exception as ex: |
| 115 | + print(" Call 3 unexpected failure: {}: {}".format(type(ex).__name__, ex)) |
| 116 | + |
| 117 | + |
| 118 | +def first_business_id(results): |
| 119 | + if not results: |
| 120 | + return None |
| 121 | + businesses = results[0].businesses |
| 122 | + if not businesses: |
| 123 | + return None |
| 124 | + return businesses[0].business_id |
| 125 | + |
| 126 | + |
| 127 | +def display(s): |
| 128 | + return "<none>" if not s else s |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + run() |
0 commit comments