-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_webhook_download.php
More file actions
61 lines (55 loc) · 2.02 KB
/
Copy pathapi_webhook_download.php
File metadata and controls
61 lines (55 loc) · 2.02 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
<?php
declare(strict_types=1);
use imperazim\http\HttpRequest;
use imperazim\http\RetryPolicy;
use imperazim\http\client\HttpClient;
use imperazim\http\client\JsonHttpClient;
use imperazim\http\download\DownloadResult;
use imperazim\http\download\FileDownloader;
use imperazim\http\exception\JsonHttpException;
use imperazim\http\exception\StatusHttpException;
use imperazim\http\exception\TransportHttpException;
use imperazim\http\webhook\DiscordWebhookClient;
use imperazim\http\webhook\DiscordWebhookMessage;
/**
* Example: call an external JSON API with retry handling.
*
* @return array<string, mixed>
*/
function fetchStatusPayload(string $apiUrl, HttpClient $http = new HttpClient()): array {
try {
return (new JsonHttpClient($http))->sendJson(
HttpRequest::get($apiUrl)
->withTimeout(8.0)
->withConnectTimeout(3.0),
RetryPolicy::networkDefaults()
);
} catch (TransportHttpException | StatusHttpException | JsonHttpException $error) {
throw new RuntimeException('Status API request failed: ' . $error->getMessage(), previous: $error);
}
}
/**
* Example: send a Discord webhook notification without hand-writing JSON.
*/
function sendDeploymentNotice(string $webhookUrl, string $repository, string $commit): void {
$message = DiscordWebhookMessage::create()
->withUsername('EasyLibrary')
->withContent('Deployment completed.')
->addEmbed([
'title' => $repository,
'description' => 'Commit `' . $commit . '` is now live.',
'color' => 0x2ecc71,
]);
(new DiscordWebhookClient())->send($webhookUrl, $message);
}
/**
* Example: download a release asset atomically and verify its SHA-256 hash.
*/
function downloadVerifiedAsset(string $url, string $targetPath, string $sha256): DownloadResult {
return (new FileDownloader())->download(
HttpRequest::get($url)->withTimeout(30.0),
$targetPath,
maxBytes: 64 * 1024 * 1024,
expectedSha256: $sha256
);
}