Skip to content

Commit e047ecf

Browse files
authored
Merge pull request #161 from fingerprintjs/inter-1799-improve-examples
2 parents eb9e2cb + a0d2278 commit e047ecf

10 files changed

Lines changed: 132 additions & 4 deletions

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ except ApiException as e:
114114
print("Exception when calling FingerprintApi->delete_visitor_data: %s\n" % e)
115115
```
116116

117+
To learn more, refer to example located in [examples/delete_visitor_example.py](examples/delete_visitor_example.py).
118+
117119
Fetching event details for `eventId`:
118120
```python
119121
import os
@@ -137,6 +139,8 @@ except ApiException as e:
137139
print("Exception when calling FingerprintApi->get_event: %s\n" % e)
138140
```
139141

142+
To learn more, refer to example located in [examples/get_event_example.py](examples/get_event_example.py).
143+
140144
Search events with custom filters:
141145
```python
142146
import os
@@ -171,6 +175,8 @@ except ApiException as e:
171175
print("Exception when calling FingerprintApi->get_event: %s\n" % e)
172176
```
173177

178+
To learn more, refer to example located in [examples/search_events_example.py](examples/search_events_example.py).
179+
174180
Update event for `eventId`:
175181
```python
176182
import os
@@ -199,6 +205,8 @@ except ApiException as e:
199205
print("Exception when calling FingerprintApi->update_event: %s\n" % e)
200206
```
201207

208+
To learn more, refer to example located in [examples/update_event_example.py](examples/update_event_example.py).
209+
202210
## Sealed results
203211

204212
This SDK provides utility methods for decoding [sealed results](https://dev.fingerprint.com/docs/sealed-client-results).
@@ -226,7 +234,7 @@ print("Unseal successful!")
226234

227235
exit(0)
228236
```
229-
To learn more, refer to example located in [sealed_results_example.py](sealed_results_example.py).
237+
To learn more, refer to example located in [examples/sealed_results_example.py](examples/sealed_results_example.py).
230238

231239
## Webhook signature validation
232240

@@ -270,7 +278,7 @@ if __name__ == '__main__':
270278
# Start the Flask application on the specified host and port
271279
app.run(host='0.0.0.0', port=5000)
272280
```
273-
To learn more, refer to example located in [webhook_signature_example.py](webhook_signature_example.py).
281+
To learn more, refer to example located in [examples/webhook_signature_example.py](examples/webhook_signature_example.py).
274282

275283
## Documentation for API Endpoints
276284

examples/get_event_example.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
3+
from dotenv import load_dotenv
4+
5+
import fingerprint_server_sdk
6+
from fingerprint_server_sdk.configuration import Region
7+
from fingerprint_server_sdk.rest import ApiException
8+
9+
load_dotenv()
10+
11+
# configure
12+
region_str = os.environ.get('REGION', 'us').upper()
13+
api_key = os.environ.get('PRIVATE_KEY')
14+
15+
if not api_key:
16+
print('API key not provided')
17+
exit(1)
18+
19+
configuration = fingerprint_server_sdk.Configuration(api_key=api_key, region=Region[region_str])
20+
21+
# create an instance
22+
api_instance = fingerprint_server_sdk.FingerprintApi(configuration)
23+
event_id = os.environ.get('EVENT_ID', None)
24+
# RULESET_ID - when provided, the response will include rule_action data.
25+
# See https://docs.fingerprint.com/docs/rules-engine#ruleset-evaluation-using-server-api
26+
ruleset_id = os.environ.get('RULESET_ID', None)
27+
28+
if not event_id:
29+
print('Event id not provided')
30+
exit(1)
31+
32+
try:
33+
event = api_instance.get_event(event_id, ruleset_id)
34+
print(f'Event ID: {event.event_id}')
35+
print(f'Timestamp: {event.timestamp}')
36+
if event.identification:
37+
print(f'Visitor ID: {event.identification.visitor_id}')
38+
print(f'Confidence: {event.identification.confidence.score}')
39+
if event.bot:
40+
print(f'Bot detection result: {event.bot}')
41+
if ruleset_id and event.rule_action:
42+
rule_action = event.rule_action.actual_instance
43+
print(f'Rule action: {rule_action.type}')
44+
if rule_action.type == 'block':
45+
print(f'Block with HTTP status code: `{rule_action.status_code}`')
46+
print(f'Block with response body: `{rule_action.body}`')
47+
print(f'Block with response headers: `{rule_action.headers}`')
48+
elif rule_action.type == 'allow':
49+
print(f'Allow with header modifications: `{rule_action.request_header_modifications}`')
50+
except ApiException as e:
51+
print(f'Exception when calling get_event: {e}\n')
52+
exit(1)
53+
54+
print('\nGet event successful!')
55+
56+
exit(0)

examples/search_events_example.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
from datetime import datetime, timedelta, timezone
3+
4+
from dotenv import load_dotenv
5+
6+
import fingerprint_server_sdk
7+
from fingerprint_server_sdk.configuration import Region
8+
from fingerprint_server_sdk.rest import ApiException
9+
10+
load_dotenv()
11+
12+
# configure
13+
region_str = os.environ.get('REGION', 'us').upper()
14+
configuration = fingerprint_server_sdk.Configuration(
15+
api_key=os.environ['PRIVATE_KEY'], region=Region[region_str]
16+
)
17+
18+
# create an instance
19+
api_instance = fingerprint_server_sdk.FingerprintApi(configuration)
20+
21+
end = datetime.now(tz=timezone.utc)
22+
start = end - timedelta(days=7)
23+
start_timestamp = int(start.timestamp() * 1000)
24+
end_timestamp = int(end.timestamp() * 1000)
25+
26+
try:
27+
response = api_instance.search_events(limit=10, start=start_timestamp, end=end_timestamp)
28+
29+
print(f'Found {len(response.events)} events')
30+
for event in response.events:
31+
event_visitor_id = event.identification.visitor_id if event.identification else '-'
32+
print(f'Event ID: {event.event_id}, Visitor ID: {event_visitor_id}')
33+
34+
if response.pagination_key:
35+
print(f'Fetching next page with pagination_key: {response.pagination_key}')
36+
next_page = api_instance.search_events(
37+
limit=10,
38+
start=start_timestamp,
39+
end=end_timestamp,
40+
pagination_key=response.pagination_key,
41+
)
42+
print(f'Found {len(next_page.events)} more events on next page')
43+
44+
except ApiException as e:
45+
print(f'Exception when calling search_events: {e}')
46+
exit(1)
47+
48+
print('Search events successful!')
49+
50+
exit(0)

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ dependencies = [
1818
"cryptography"
1919
]
2020

21+
[project.optional-dependencies]
22+
examples = ["python-dotenv (>=1.0.0)"]
23+
2124
[project.urls]
2225
Repository = "https://github.com/fingerprintjs/python-sdk"
2326

template/common_README.mustache

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ except ApiException as e:
3232
print("Exception when calling FingerprintApi->delete_visitor_data: %s\n" % e)
3333
```
3434

35+
To learn more, refer to example located in [examples/delete_visitor_example.py](examples/delete_visitor_example.py).
36+
3537
Fetching event details for `eventId`:
3638
```python
3739
import os
@@ -52,6 +54,8 @@ except ApiException as e:
5254
print("Exception when calling FingerprintApi->get_event: %s\n" % e)
5355
```
5456

57+
To learn more, refer to example located in [examples/get_event_example.py](examples/get_event_example.py).
58+
5559
Search events with custom filters:
5660
```python
5761
import os
@@ -83,6 +87,8 @@ except ApiException as e:
8387
print("Exception when calling FingerprintApi->get_event: %s\n" % e)
8488
```
8589

90+
To learn more, refer to example located in [examples/search_events_example.py](examples/search_events_example.py).
91+
8692
Update event for `eventId`:
8793
```python
8894
import os
@@ -108,6 +114,8 @@ except ApiException as e:
108114
print("Exception when calling FingerprintApi->update_event: %s\n" % e)
109115
```
110116

117+
To learn more, refer to example located in [examples/update_event_example.py](examples/update_event_example.py).
118+
111119
## Sealed results
112120

113121
This SDK provides utility methods for decoding [sealed results](https://dev.fingerprint.com/docs/sealed-client-results).
@@ -135,7 +143,7 @@ print("Unseal successful!")
135143

136144
exit(0)
137145
```
138-
To learn more, refer to example located in [sealed_results_example.py](sealed_results_example.py).
146+
To learn more, refer to example located in [examples/sealed_results_example.py](examples/sealed_results_example.py).
139147

140148
## Webhook signature validation
141149

@@ -179,7 +187,7 @@ if __name__ == '__main__':
179187
# Start the Flask application on the specified host and port
180188
app.run(host='0.0.0.0', port=5000)
181189
```
182-
To learn more, refer to example located in [webhook_signature_example.py](webhook_signature_example.py).
190+
To learn more, refer to example located in [examples/webhook_signature_example.py](examples/webhook_signature_example.py).
183191

184192
## Documentation for API Endpoints
185193

template/pyproject.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ dependencies = [
8989
"cryptography"
9090
]
9191
92+
[project.optional-dependencies]
93+
examples = ["python-dotenv (>=1.0.0)"]
94+
9295
[project.urls]
9396
Repository = "https://{{{gitHost}}}/{{{gitUserId}}}/{{{gitRepoId}}}"
9497

0 commit comments

Comments
 (0)