-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrun_checks.php
More file actions
186 lines (156 loc) · 6.78 KB
/
run_checks.php
File metadata and controls
186 lines (156 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
declare(strict_types=1);
require_once __DIR__.'/vendor/autoload.php';
use Dotenv\Dotenv;
use Fingerprint\ServerAPI\Api\FingerprintApi;
use Fingerprint\ServerAPI\Configuration;
use Fingerprint\ServerAPI\Model\EventsGetResponse;
use Fingerprint\ServerAPI\Model\EventsUpdateRequest;
use Fingerprint\ServerAPI\Model\VisitorsGetResponse;
use Fingerprint\ServerAPI\Model\SearchEventsResponse;
use Fingerprint\ServerAPI\Webhook\WebhookVerifier;
use GuzzleHttp\Client;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
function env(string $key, ?string $default = null): ?string
{
return $_ENV[$key] ?? getenv($key) ?: $default;
}
$apiKey = env('FP_PRIVATE_API_KEY');
if (!$apiKey) {
throw new Exception('FP_PRIVATE_API_KEY is not defined.');
}
$regionEnv = env('FP_REGION', 'us');
$visitorIdToDelete = env('FP_VISITOR_ID_TO_DELETE');
$requestIdToUpdate = env('FP_REQUEST_ID_TO_UPDATE');
$region = match(strtolower(trim($regionEnv))) {
'eu' => Configuration::REGION_EUROPE,
'ap' => Configuration::REGION_ASIA,
default => Configuration::REGION_GLOBAL,
};
$config = Configuration::getDefaultConfiguration(
$apiKey,
$region,
);
$client = new FingerprintApi(
new Client(),
$config
);
$start = (new DateTime())->sub(new DateInterval('P3M'));
$end = new DateTime();
// FingerprintApi->searchEvents usage example
try {
/** @var SearchEventsResponse $result */
list($result, $response) = $client->searchEvents(10, start: $start->getTimestamp() * 1000, end: $end->getTimestamp() * 1000);
if (!is_countable($result->getEvents()) || count($result->getEvents()) === 0) {
throw new Exception('No events found');
}
$identificationData = $result->getEvents()[0]->getProducts()->getIdentification()->getData();
$visitorId = $identificationData->getVisitorId();
$requestId = $identificationData->getRequestId();
fwrite(STDOUT, sprintf("\n\nGot events: %s \n", $response->getBody()->getContents()));
} catch (Exception $e) {
fwrite(STDERR, sprintf("\n\nException when calling FingerprintApi->searchEvents: %s\n", $e->getMessage()));
exit(1);
}
// FingerprintApi->getVisits usage example
try {
/** @var VisitorsGetResponse $result */
list($result, $response) = $client->getVisits($visitorId);
if ($result->getVisitorId() !== $visitorId) {
throw new Exception('Argument visitorId is not equal to deserialized getVisitorId');
}
fwrite(STDOUT, sprintf("Got visits: %s \n", $response->getBody()->getContents()));
} catch (Exception $e) {
fwrite(STDERR, sprintf("Exception when calling FingerprintApi->getVisits: %s\n", $e->getMessage()));
exit(1);
}
// FingerprintApi->deleteVisitorData usage example
if ($visitorIdToDelete) {
try {
list($model, $response) = $client->deleteVisitorData($visitorIdToDelete);
fwrite(STDOUT, sprintf("Visitor data deleted: %s \n", $response->getBody()->getContents()));
} catch (Exception $e) {
fwrite(STDERR, sprintf("Exception when calling FingerprintApi->deleteVisitorData: %s\n", $e->getMessage()));
exit(1);
}
}
// FingerprintApi->getEvent usage example
try {
/** @var EventsGetResponse $result */
list($result, $response) = $client->getEvent($requestId);
if ($result->getProducts()->getIdentification()->getData()->getRequestId() !== $requestId) {
throw new Exception('Argument requestId is not equal to deserialized getRequestId');
}
fwrite(STDOUT, sprintf("\n\nGot event: %s \n", $response->getBody()->getContents()));
} catch (Exception $e) {
fwrite(STDERR, sprintf("\n\nException when calling FingerprintApi->getEvent: %s\n", $e->getMessage()));
exit(1);
}
// FingerprintApi->updateEvent usage example
if ($requestIdToUpdate) {
try {
$body = new EventsUpdateRequest([
'linked_id' => date('Y-m-d H:i:s'),
]);
list($model, $response) = $client->updateEvent($body, $requestIdToUpdate);
fwrite(STDOUT, sprintf("\n\nEvent updated: %s \n", $response->getBody()->getContents()));
} catch (Exception $e) {
fwrite(STDOUT, sprintf("\n\nException when calling FingerprintApi->updateEvent: %s\n", $e->getMessage()));
exit(1);
}
}
// Call API asynchronously examples
$eventPromise = $client->getEventAsync($requestId);
$eventPromise->then(function ($tuple) use ($requestId) {
list($result, $response) = $tuple;
if ($result->getProducts()->getIdentification()->getData()->getRequestId() !== $requestId) {
throw new Exception('Argument requestId is not equal to deserialized getRequestId');
}
fwrite(STDOUT, sprintf("\n\nGot async event: %s \n", $response->getBody()->getContents()));
}, function ($exception) {
fwrite(STDERR, sprintf("\n\nException when calling FingerprintApi->getVisits: %s\n", $exception->getMessage()));
exit(1);
})->wait();
$visitsPromise = $client->getVisitsAsync($visitorId);
$visitsPromise->then(function ($tuple) use ($visitorId) {
list($result, $response) = $tuple;
if ($result->getVisitorId() !== $visitorId) {
throw new Exception('Argument visitorId is not equal to deserialized getVisitorId');
}
fwrite(STDOUT, sprintf("\n\nGot async visits: %s \n", $response->getBody()->getContents()));
}, function ($exception) {
fwrite(STDERR, sprintf("\n\nException when calling FingerprintApi->getEvent: %s\n", $exception->getMessage()));
exit(1);
})->wait();
// Webhook verification example
$webhookSecret = 'secret';
$webhookData = 'data';
$webhookHeader = 'v1=1b2c16b75bd2a870c114153ccda5bcfca63314bc722fa160d690de133ccbb9db';
$isValidWebhookSign = WebhookVerifier::IsValidWebhookSignature($webhookHeader, $webhookData, $webhookSecret);
if ($isValidWebhookSign) {
fwrite(STDOUT, "\n\nVerified webhook signature\n");
} else {
fwrite(STDERR, "\n\nWebhook signature verification failed\n");
exit(1);
}
// Check that old events still match expected format
try {
list($resultOld) = $client->searchEvents(1, start: $start->getTimestamp() * 1000, end: $end->getTimestamp() * 1000, reverse: true);
if (!is_countable($resultOld->getEvents()) || count($resultOld->getEvents()) === 0) {
throw new Exception('No old events found');
}
$identificationDataOld = $resultOld->getEvents()[0]->getProducts()->getIdentification()->getData();
$visitorIdOld = $identificationDataOld->getVisitorId();
$requestIdOld = $identificationDataOld->getRequestId();
if ($requestId === $requestIdOld) {
throw new Exception('Old events are identical to new');
}
list($result, $response) = $client->getEvent($requestIdOld);
list($result, $response) = $client->getVisits($visitorIdOld);
fwrite(STDOUT, "\n\nOld events are good\n");
} catch (Exception $e) {
fwrite(STDERR, sprintf("\n\nException when trying to read old data: %s\n", $e->getMessage()));
}
fwrite(STDOUT, "\n\nChecks passed\n");
exit(0);