-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeanStorageClient.php
114 lines (92 loc) · 3.31 KB
/
LeanStorageClient.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace Manyou\LeanStorage;
use Generator;
use GuzzleHttp\Promise\PromiseInterface;
use Manyou\LeanStorage\Request\Batchable;
use Manyou\LeanStorage\Request\HasFormBody;
use Manyou\LeanStorage\Request\HasJsonBody;
use Manyou\LeanStorage\Request\HasQuery;
use Manyou\LeanStorage\Request\Request;
use Manyou\PromiseHttpClient\PromiseHttpClientInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Contracts\HttpClient\ResponseInterface;
use function array_map;
use function parse_url;
use const PHP_URL_PATH;
class LeanStorageClient
{
private array $options;
private string $basePath;
public function __construct(
private PromiseHttpClientInterface $httpClient,
string $endpoint,
string $appId,
string $appKey,
string $sessionToken = '',
) {
$this->basePath = parse_url($endpoint, PHP_URL_PATH);
$defaultHeaders = [
'Content-Type' => 'application/json',
'X-LC-Id' => $appId,
'X-LC-Key' => $appKey,
];
if ($sessionToken !== '') {
$defaultHeaders['X-LC-Session'] = $sessionToken;
}
$this->options = ['base_uri' => $endpoint, 'headers' => $defaultHeaders];
}
public function request(Request $request): PromiseInterface
{
$options = [];
if ($request instanceof HasJsonBody) {
$options += ['json' => $request->getJsonBody()];
} elseif ($request instanceof HasFormBody) {
$options += ['body' => $request->getFormBody()];
}
if ($request instanceof HasQuery) {
$options += ['query' => $request->getQuery()];
}
return $this->httpClient->request($request->getMethod(), $request->getPath(), $options + $this->options)
->then(static function (ResponseInterface $response) {
$response = $response->toArray(false);
if (isset($response['error'])) {
throw RequestException::fromResponse($response);
}
return $response;
});
}
#[AsMessageHandler]
public function handle(Request $request): mixed
{
return $this->request($request)->wait();
}
/**
* @param Batchable[] $requests
*
* @return Generator|PromiseInterface[]
*/
public function batch(Batchable ...$requests): Generator
{
$batch = $this->httpClient->request('POST', 'batch', [
'json' => [
'requests' => array_map(
fn (Batchable $request) => $request->toBatchRequest($this->basePath),
$requests,
),
],
] + $this->options)->then(static fn (ResponseInterface $response) => $response->toArray());
foreach ($requests as $i => $request) {
yield $i => $batch->then(static function (array $responses) use ($i) {
$response = $responses[$i];
if (isset($response['error'])) {
throw RequestException::fromResponse($response['error']);
}
if (isset($response['success'])) {
return $response['success'];
}
throw new RequestException('Unknown response.');
});
}
}
}