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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
declare(strict_types=1);
namespace Kreait\Firebase\Database;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Request;
use Kreait\Firebase\Exception\DatabaseApiExceptionConverter;
use Kreait\Firebase\Exception\DatabaseException;
use Kreait\Firebase\Util\JSON;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use Throwable;
/**
* @internal
*/
class ApiClient
{
private ClientInterface $client;
protected DatabaseApiExceptionConverter $errorHandler;
/**
* @internal
*/
public function __construct(ClientInterface $httpClient)
{
$this->client = $httpClient;
$this->errorHandler = new DatabaseApiExceptionConverter();
}
/**
* @param UriInterface|string $uri
*
* @throws DatabaseException
*
* @return mixed
*/
public function get($uri)
{
$response = $this->requestApi('GET', $uri);
return JSON::decode((string) $response->getBody(), true);
}
/**
* @internal This method should only be used in the context of Database translations
*
* @param UriInterface|string $uri
*
* @throws DatabaseException
*
* @return array<string, mixed>
*/
public function getWithETag($uri): array
{
$response = $this->requestApi('GET', $uri, [
'headers' => [
'X-Firebase-ETag' => 'true',
],
]);
$value = JSON::decode((string) $response->getBody(), true);
$etag = $response->getHeaderLine('ETag');
return [
'value' => $value,
'etag' => $etag,
];
}
/**
* @param UriInterface|string $uri
* @param mixed $value
*
* @throws DatabaseException
*
* @return mixed
*/
public function set($uri, $value)
{
$response = $this->requestApi('PUT', $uri, ['json' => $value]);
return JSON::decode((string) $response->getBody(), true);
}
/**
* @internal This method should only be used in the context of Database translations
*
* @param UriInterface|string $uri
* @param mixed $value
*
* @throws DatabaseException
*
* @return mixed
*/
public function setWithEtag($uri, $value, string $etag)
{
$response = $this->requestApi('PUT', $uri, [
'headers' => [
'if-match' => $etag,
],
'json' => $value,
]);
return JSON::decode((string) $response->getBody(), true);
}
/**
* @internal This method should only be used in the context of Database translations
*
* @param UriInterface|string $uri
*
* @throws DatabaseException
*/
public function removeWithEtag($uri, string $etag): void
{
$this->requestApi('DELETE', $uri, [
'headers' => [
'if-match' => $etag,
],
]);
}
/**
* @param UriInterface|string $uri
*
* @throws DatabaseException
*
* @return mixed
*/
public function updateRules($uri, RuleSet $ruleSet)
{
$rules = $ruleSet->getRules();
$encodedRules = Json::encode((object) $rules);
$response = $this->requestApi('PUT', $uri, [
'body' => $encodedRules,
]);
return JSON::decode((string) $response->getBody(), true);
}
/**
* @param UriInterface|string $uri
* @param mixed $value
*
* @throws DatabaseException
*/
public function push($uri, $value): string
{
$response = $this->requestApi('POST', $uri, ['json' => $value]);
return JSON::decode((string) $response->getBody(), true)['name'];
}
/**
* @param UriInterface|string $uri
*
* @throws DatabaseException
*/
public function remove($uri): void
{
$this->requestApi('DELETE', $uri);
}
/**
* @param UriInterface|string $uri
* @param array<mixed> $values
*
* @throws DatabaseException
*/
public function update($uri, array $values): void
{
$this->requestApi('PATCH', $uri, ['json' => $values]);
}
/**
* @param UriInterface|string $uri
* @param array<string, mixed>|null $options
*
* @throws DatabaseException
*/
private function requestApi(string $method, $uri, ?array $options = null): ResponseInterface
{
$options ??= [];
$request = new Request($method, $uri);
try {
return $this->client->send($request, $options);
} catch (Throwable $e) {
throw $this->errorHandler->convertException($e);
}
}
}