-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDaprHttpClient.php
80 lines (71 loc) · 2.16 KB
/
DaprHttpClient.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
<?php
namespace Dapr\Client;
use Dapr\Deserialization\IDeserializer;
use Dapr\Serialization\ISerializer;
use GuzzleHttp\Client;
use Psr\Log\LoggerInterface;
/**
* Class DaprHttpClient
* @package Dapr\Client
*/
class DaprHttpClient extends DaprClient
{
use HttpStateTrait;
use HttpSecretsTrait;
use HttpInvokeTrait;
use HttpPubSubTrait;
use HttpBindingTrait;
private Client $httpClient;
public function __construct(
private string $baseHttpUri,
IDeserializer $deserializer,
ISerializer $serializer,
LoggerInterface $logger
) {
parent::__construct($deserializer, $serializer, $logger);
if (str_ends_with($this->baseHttpUri, '/')) {
$this->baseHttpUri = rtrim($this->baseHttpUri, '/');
}
$this->httpClient = new Client(
[
'base_uri' => $this->baseHttpUri,
'allow_redirects' => false,
'headers' => [
'User-Agent' => 'DaprPHPSDK/v2.0',
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]
]
);
}
public function isDaprHealthy(): bool
{
try {
$result = $this->httpClient->get('/v1.0/healthz');
if (204 === $result->getStatusCode()) {
return true;
}
return false;
} catch (\Throwable $exception) {
return false;
}
}
public function getMetadata(): MetadataResponse|null
{
try {
$result = $this->httpClient->get('/v1.0/metadata');
return $this->deserializer->from_json(MetadataResponse::class, $result->getBody()->getContents());
} catch (\Throwable $exception) {
return null;
}
}
public function shutdown(bool $afterRequest = true): void
{
$shutdown = fn() => $this->httpClient->post('/v1.0/shutdown');
if ($afterRequest) {
register_shutdown_function($shutdown);
return;
}
$shutdown();
}
}