|
1 | 1 | import os |
2 | | -from datetime import datetime, timedelta |
| 2 | +import sys |
| 3 | +from dataclasses import dataclass |
| 4 | +from datetime import datetime, timedelta, timezone |
| 5 | +from dotenv import load_dotenv |
| 6 | +from typing import Tuple |
3 | 7 |
|
4 | | -import fingerprint_pro_server_api_sdk |
| 8 | +from fingerprint_pro_server_api_sdk import FingerprintApi, Configuration |
5 | 9 | from fingerprint_pro_server_api_sdk.rest import ApiException |
6 | | -from dotenv import load_dotenv |
7 | 10 |
|
8 | | -load_dotenv() |
9 | | - |
10 | | -# configure |
11 | | -configuration = fingerprint_pro_server_api_sdk.Configuration( |
12 | | - api_key=os.environ["PRIVATE_KEY"], region=os.environ.get("REGION", "us")) |
13 | | - |
14 | | -# create an instance of the API class |
15 | | -api_instance = fingerprint_pro_server_api_sdk.FingerprintApi(configuration) |
16 | | - |
17 | | -end = int(datetime.now().timestamp() * 1000) |
18 | | -start = int((datetime.now() - timedelta(days=90)).timestamp() * 1000) |
19 | | - |
20 | | - |
21 | | -# FingerprintApi->search_events usage example |
22 | | -try: |
23 | | - search_events_response = api_instance.search_events(2, start=start, end=end) |
24 | | - if len(search_events_response.events) == 0: |
25 | | - print("FingerprintApi.search_events: is empty") |
26 | | - exit(1) |
27 | | - first_event = search_events_response.events[0] |
28 | | - first_event_identification_data = first_event.products.identification.data |
29 | | - visitor_id = first_event_identification_data.visitor_id |
30 | | - request_id = first_event_identification_data.request_id |
31 | | - print("\n\n\nSearch events response: \n", search_events_response) |
32 | | - search_events_response_second_page = api_instance.search_events(2, start=start, end=end, pagination_key=search_events_response.pagination_key) |
33 | | - |
34 | | - if len(search_events_response_second_page.events) == 0: |
35 | | - print("Second page of FingerprintApi.search_events: is empty") |
36 | | - exit(1) |
37 | | - |
38 | | -except ApiException as e: |
39 | | - print("Exception when calling FingerprintApi.search_events: %s\n" % e) |
40 | | - exit(1) |
41 | | - |
42 | | -# Use existing visitor_id from FingerprintApi->search_events response to check FingerprintApi->get_visits method |
43 | | -try: |
44 | | - visits_response = api_instance.get_visits(visitor_id, limit=2) |
45 | | - print("\n\n\nVisits response: \n", visits_response) |
46 | | - |
47 | | -except ApiException as e: |
48 | | - print("Exception when calling FingerprintApi.get_visits: %s\n" % e) |
49 | | - exit(1) |
50 | | - |
51 | | -# Use existing request_id from FingerprintApi->search_events response to check FingerprintApi->get_event method |
52 | | -try: |
53 | | - events_response = api_instance.get_event(request_id) |
54 | | - print("\n\n\nEvent response: \n", events_response.products) |
55 | | - |
56 | | -except ApiException as e: |
57 | | - print("Exception when calling FingerprintApi.get_event: %s\n" % e) |
58 | | - exit(1) |
59 | | - |
60 | | -# Async methods examples |
61 | | -try: |
62 | | - visits_response_request = api_instance.get_visits(visitor_id, limit=2, async_req=True) |
63 | | - events_response_request = api_instance.get_event(request_id, async_req=True) |
64 | | - visits_response = visits_response_request.get() |
65 | | - print("\n\n\nVisits async response: \n", visits_response) |
66 | | - events_response = events_response_request.get() |
67 | | - print("\n\n\nEvent async response: \n", events_response.products) |
68 | | -except ApiException as e: |
69 | | - print("Exception when calling Async example: %s\n" % e) |
70 | | - exit(1) |
71 | | - |
72 | | -# Check that old events are still match expected format |
73 | | -try: |
74 | | - search_events_response_old = api_instance.search_events(1, start=start, end=end, reverse=True) |
75 | | - if len(search_events_response_old.events) == 0: |
76 | | - print("FingerprintApi.search_events: is empty for old events\n") |
77 | | - exit(1) |
78 | | - old_event_identification_data = search_events_response_old.events[0].products.identification.data |
79 | | - visitor_id_old = old_event_identification_data.visitor_id |
80 | | - request_id_old = old_event_identification_data.request_id |
81 | | - |
82 | | - if visitor_id_old == visitor_id or request_id_old == request_id: |
83 | | - print("Old events are identical to new\n") |
84 | | - exit(1) |
85 | | - |
86 | | - api_instance.get_visits(visitor_id_old, limit=2) |
87 | | - api_instance.get_event(request_id_old) |
88 | | - print("\n\n\nOld events are good\n") |
89 | | -except ApiException as e: |
90 | | - print("Exception when trying to read old data: %s\n" % e) |
91 | | - |
92 | | -print("Checks passed!") |
93 | | - |
94 | | -exit(0) |
| 11 | +@dataclass(frozen=True) |
| 12 | +class AppConfig: |
| 13 | + api_key: str |
| 14 | + region: str |
| 15 | + |
| 16 | +def load_config() -> AppConfig: |
| 17 | + load_dotenv() |
| 18 | + api_key = os.getenv("PRIVATE_KEY") |
| 19 | + if not api_key: |
| 20 | + print("Error: PRIVATE_KEY environment variable not set", file=sys.stderr) |
| 21 | + sys.exit(1) |
| 22 | + region = (os.getenv("REGION") or "us").lower() |
| 23 | + return AppConfig(api_key=api_key, region=region) |
| 24 | + |
| 25 | +def make_client(cfg: AppConfig) -> FingerprintApi: |
| 26 | + configuration = Configuration(api_key=cfg.api_key, region=cfg.region) |
| 27 | + return FingerprintApi(configuration) |
| 28 | + |
| 29 | +def create_range(days: int) -> Tuple[int, int]: |
| 30 | + end = datetime.now(tz=timezone.utc) |
| 31 | + start = end - timedelta(days=days) |
| 32 | + return int(start.timestamp() * 1000), int(end.timestamp() * 1000) |
| 33 | + |
| 34 | +def main() -> int: |
| 35 | + cfg = load_config() |
| 36 | + api = make_client(cfg) |
| 37 | + |
| 38 | + start, end = create_range(90) |
| 39 | + |
| 40 | + # FingerprintApi->search_events usage example |
| 41 | + try: |
| 42 | + search_events_response = api.search_events(2, start=start, end=end) |
| 43 | + if len(search_events_response.events) == 0: |
| 44 | + print("FingerprintApi.search_events: is empty", file=sys.stderr) |
| 45 | + return 1 |
| 46 | + first_event = search_events_response.events[0] |
| 47 | + first_event_identification_data = first_event.products.identification.data |
| 48 | + visitor_id = first_event_identification_data.visitor_id |
| 49 | + request_id = first_event_identification_data.request_id |
| 50 | + print("\n\n\nSearch events response: \n", search_events_response) |
| 51 | + search_events_response_second_page = api.search_events(2, start=start, end=end, |
| 52 | + pagination_key=search_events_response.pagination_key) |
| 53 | + |
| 54 | + if len(search_events_response_second_page.events) == 0: |
| 55 | + print("Second page of FingerprintApi.search_events: is empty", file=sys.stderr) |
| 56 | + return 1 |
| 57 | + |
| 58 | + except ApiException as e: |
| 59 | + print("Exception when calling FingerprintApi.search_events: %s\n" % e, file=sys.stderr) |
| 60 | + return 1 |
| 61 | + |
| 62 | + # Use existing visitor_id from FingerprintApi->search_events response to check FingerprintApi->get_visits method |
| 63 | + try: |
| 64 | + visits_response = api.get_visits(visitor_id, limit=2) |
| 65 | + print("\n\n\nVisits response: \n", visits_response) |
| 66 | + |
| 67 | + except ApiException as e: |
| 68 | + print("Exception when calling FingerprintApi.get_visits: %s\n" % e, file=sys.stderr) |
| 69 | + return 1 |
| 70 | + |
| 71 | + # Use existing request_id from FingerprintApi->search_events response to check FingerprintApi->get_event method |
| 72 | + try: |
| 73 | + events_response = api.get_event(request_id) |
| 74 | + print("\n\n\nEvent response: \n", events_response.products) |
| 75 | + |
| 76 | + except ApiException as e: |
| 77 | + print("Exception when calling FingerprintApi.get_event: %s\n" % e, file=sys.stderr) |
| 78 | + return 1 |
| 79 | + |
| 80 | + # Async methods examples |
| 81 | + try: |
| 82 | + visits_response_request = api.get_visits(visitor_id, limit=2, async_req=True) |
| 83 | + events_response_request = api.get_event(request_id, async_req=True) |
| 84 | + visits_response = visits_response_request.get() |
| 85 | + print("\n\n\nVisits async response: \n", visits_response) |
| 86 | + events_response = events_response_request.get() |
| 87 | + print("\n\n\nEvent async response: \n", events_response.products) |
| 88 | + except ApiException as e: |
| 89 | + print("Exception when calling Async example: %s\n" % e, file=sys.stderr) |
| 90 | + return 1 |
| 91 | + |
| 92 | + # Check that old events are still match expected format |
| 93 | + try: |
| 94 | + search_events_response_old = api.search_events(1, start=start, end=end, reverse=True) |
| 95 | + if len(search_events_response_old.events) == 0: |
| 96 | + print("FingerprintApi.search_events: is empty for old events\n", file=sys.stderr) |
| 97 | + return 1 |
| 98 | + old_event_identification_data = search_events_response_old.events[0].products.identification.data |
| 99 | + visitor_id_old = old_event_identification_data.visitor_id |
| 100 | + request_id_old = old_event_identification_data.request_id |
| 101 | + |
| 102 | + if request_id_old == request_id: |
| 103 | + print("Old events are identical to new\n", file=sys.stderr) |
| 104 | + return 1 |
| 105 | + |
| 106 | + api.get_visits(visitor_id_old, limit=2) |
| 107 | + api.get_event(request_id_old) |
| 108 | + print("\n\n\nOld events are good\n") |
| 109 | + except ApiException as e: |
| 110 | + print("Exception when trying to read old data: %s\n" % e, file=sys.stderr) |
| 111 | + return 1 |
| 112 | + |
| 113 | + print("Checks passed!") |
| 114 | + return 0 |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + sys.exit(main()) |
0 commit comments