-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalrusClient.php
93 lines (84 loc) · 3.3 KB
/
WalrusClient.php
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
<?php
namespace Suicore\Walrus;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Suicore\Walrus\Responses\StoreBlobOptions;
use Suicore\Walrus\Responses\StoreBlobResponse;
class WalrusClient
{
private Client $publisherClient;
private Client $aggregatorClient;
public function __construct(
?string $publisherUrl = '',
?string $aggregatorUrl = '',
?array $options = [],
?float $timeout = 20.0,
?Client $publisherClient = null,
?Client $aggregatorClient = null
) {
if ($publisherClient) {
$this->publisherClient = $publisherClient;
} elseif ($publisherUrl != '') {
$publisherOptions = $options['publisher'] ?? [];
$publisherOptions = array_merge(['base_uri' => $publisherUrl, 'timeout' => $timeout], $publisherOptions);
$this->publisherClient = new Client($publisherOptions);
} else {
throw new \InvalidArgumentException('Either $publisherUrl or $publisherClient must be provided.');
}
if ($aggregatorClient) {
$this->aggregatorClient = $aggregatorClient;
} elseif ($aggregatorUrl != '') {
$aggregatorOptions = $options['aggregator'] ?? [];
$aggregatorOptions = array_merge(['base_uri' => $aggregatorUrl, 'timeout' => $timeout], $aggregatorOptions);
$this->aggregatorClient = new Client($aggregatorOptions);
} else {
throw new \InvalidArgumentException('Either $aggregatorUrl or $aggregatorClient must be provided.');
}
}
/**
* Store a blob using the publisher API.
*
* @param string $dataOrPath The data to store or a file path.
* @param StoreBlobOptions $options Options for the store request.
* @param bool $isFile Whether $dataOrPath is a file path.
*
* @return StoreBlobResponse
*
* @throws \Exception if the request fails.
*/
public function storeBlob(string $dataOrPath, StoreBlobOptions $options, bool $isFile = false): StoreBlobResponse
{
$query = [];
$query['epochs'] = $options->getEpochs();
if ($options->getSendObjectTo() !== '') {
$query['send_object_to'] = $options->getSendObjectTo();
}
if ($options->isDeletable()) {
$query['deletable'] = 'true';
}
$uri = '/v1/blobs';
if (!empty($query)) {
$uri .= '?' . http_build_query($query);
}
try {
$body = $isFile ? fopen($dataOrPath, 'r') : $dataOrPath;
$response = $this->publisherClient->request('PUT', $uri, [
'body' => $body,
]);
$content = $response->getBody()->getContents();
return StoreBlobResponse::fromJson($content);
} catch (GuzzleException $e) {
throw new \Exception("Guzzle error: " . $e->getMessage(), $e->getCode(), $e);
}
}
public function getBlob(string $blobId): string
{
$uri = "/v1/blobs/{$blobId}";
try {
$response = $this->aggregatorClient->request('GET', $uri);
return $response->getBody()->getContents();
} catch (GuzzleException $e) {
throw new \Exception("Guzzle error: " . $e->getMessage(), $e->getCode(), $e);
}
}
}