-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathHttpSecretsTrait.php
79 lines (71 loc) · 2.49 KB
/
HttpSecretsTrait.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
<?php
namespace Dapr\Client;
use Dapr\Deserialization\IDeserializer;
use Dapr\Serialization\ISerializer;
use GuzzleHttp\Client;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Trait HttpSecretsTrait
* @package Dapr\Client
*/
trait HttpSecretsTrait
{
use PromiseHandlingTrait;
public IDeserializer $deserializer;
public ISerializer $serializer;
private Client $httpClient;
public function getSecret(string $storeName, string $key, array $metadata = []): array|null
{
return $this->getSecretAsync($storeName, $key, $metadata)->wait();
}
public function getSecretAsync(string $storeName, string $key, array $metadata = []): PromiseInterface
{
$storeName = rawurlencode($storeName);
$key = rawurlencode($key);
return $this->handlePromise(
$this->httpClient->getAsync(
"/v1.0/secrets/$storeName/$key",
[
'query' => array_merge(
...array_map(
fn($key, $value) => ["metadata.$key" => $value],
array_keys($metadata),
$metadata
)
)
]
),
fn(ResponseInterface $response) => $this->deserializer->from_json(
'array',
$response->getBody()->getContents()
)
);
}
public function getBulkSecret(string $storeName, array $metadata = []): array
{
return $this->getBulkSecretAsync($storeName, $metadata)->wait();
}
public function getBulkSecretAsync(string $storeName, array $metadata = []): PromiseInterface
{
$storeName = rawurlencode($storeName);
return $this->handlePromise(
$this->httpClient->getAsync(
"/v1.0/secrets/$storeName/bulk",
[
'query' => array_merge(
...array_map(
fn($key, $value) => ["metadata.$key" => $value],
array_keys($metadata),
$metadata
)
)
]
),
fn(ResponseInterface $response) => $this->deserializer->from_json(
'array',
$response->getBody()->getContents()
)
);
}
}