-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
135 lines (109 loc) · 4.56 KB
/
Client.php
File metadata and controls
135 lines (109 loc) · 4.56 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/*
* This file is part of the CwdPowerDNS Client
*
* (c) 2024 cwd.at GmbH <office@cwd.at>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Cwd\PowerDNSClient;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Request;
use Http\Discovery\HttpClientDiscovery;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class Client
{
private $basePath = 'api/v1';
private $apiKey;
private $apiUri;
/** @var GuzzleClient */
private $client;
/** @var Serializer */
private $serializer;
public function __construct($apiHost, $apiKey, GuzzleClient $client = null)
{
$this->apiKey = $apiKey;
$this->apiUri = sprintf('%s/%s', $apiHost, $this->basePath);
if (null === $client) {
// $this->client = new GuzzleClient(['base_uri' => $this->apiUri]);
$this->client = HttpClientDiscovery::find();
}
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory);
$normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter(), new PropertyAccessor(), new ReflectionExtractor(), $discriminator);
$this->serializer = new Serializer([new DateTimeNormalizer(), new BackedEnumNormalizer(), new ArrayDenormalizer(), $normalizer], ['json' => new JsonEncoder()]);
}
/**
* @param string|null $payload
* @param string|null $hydrationClass
* @param bool $isList
* @param string $method
*
* @throws \Http\Client\Exception
* @throws \LogicException
*/
public function call($payload = null, $uri, $hydrationClass = null, $isList = false, $method = 'GET', array $queryParams = [], $isJson = true)
{
$uri = rtrim(sprintf('%s/%s', $this->apiUri, $uri), '/');
if (count($queryParams) > 0) {
$uri .= '?'.http_build_query($queryParams);
}
$request = new Request($method, $uri, [
'X-API-Key' => $this->apiKey,
'Content-Type' => 'application/json',
], $payload);
$response = $this->client->sendRequest($request);
$responseBody = $response->getBody()->getContents();
if (!$isJson) {
return $responseBody;
}
$responseData = json_decode($responseBody);
if ($response->getStatusCode() >= 300 && isset($responseData->error)) {
throw new \LogicException(sprintf('Error on %s request %s: %s', $method, $uri, $responseData->error));
}
if ($response->getStatusCode() >= 300) {
$message = isset($responseData->message) ?? 'Unknown';
throw new \Exception(sprintf('Error on request %s: %s', $response->getStatusCode(), $message));
}
if (null !== $hydrationClass && class_exists($hydrationClass)) {
return $this->denormalizeObject($hydrationClass, $responseData, $isList);
}
if (null !== $hydrationClass && !class_exists($hydrationClass)) {
throw new \Exception(sprintf('HydrationClass (%s) does not exist', $hydrationClass));
}
return $responseData;
}
public function denormalizeObject($hydrationClass, $dataObject, $isList = false)
{
if (!$isList) {
$dataObject = [$dataObject];
}
$result = [];
foreach ($dataObject as $data) {
$result[] = $this->serializer->denormalize($data, $hydrationClass, null, [
ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => false,
]);
}
if ($isList) {
return $result;
}
return current($result);
}
public function getSerializer(): Serializer
{
return $this->serializer;
}
}