1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArangoClient; |
6
|
|
|
|
7
|
|
|
use ArangoClient\Exceptions\ArangoException; |
8
|
|
|
use ArangoClient\Http\HttpClientConfig; |
9
|
|
|
use ArangoClient\Http\HttpRequestOptions; |
10
|
|
|
use ArangoClient\Statement\Statement; |
11
|
|
|
use ArangoClient\Transactions\SupportsTransactions; |
12
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
13
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
14
|
|
|
use GuzzleHttp\Exception\RequestException; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use Spatie\DataTransferObject\Exceptions\UnknownProperties; |
17
|
|
|
use stdClass; |
18
|
|
|
use Throwable; |
19
|
|
|
use Traversable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The arangoClient handles connections to ArangoDB's HTTP REST API. |
23
|
|
|
* |
24
|
|
|
* @see https://www.arangodb.com/docs/stable/http/ |
25
|
|
|
*/ |
26
|
|
|
class ArangoClient |
27
|
|
|
{ |
28
|
|
|
use HandlesJson; |
29
|
|
|
use HasManagers; |
30
|
|
|
use SupportsTransactions; |
31
|
|
|
|
32
|
|
|
protected GuzzleClient $httpClient; |
33
|
|
|
|
34
|
|
|
protected HttpClientConfig $config; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* ArangoClient constructor. |
38
|
|
|
* |
39
|
|
|
* @param array<string|numeric|null> $config |
40
|
|
|
* @param GuzzleClient|null $httpClient |
41
|
|
|
* @throws UnknownProperties |
42
|
|
|
*/ |
43
|
105 |
|
public function __construct(array $config = [], GuzzleClient $httpClient = null) |
44
|
|
|
{ |
45
|
105 |
|
$config['endpoint'] = $this->generateEndpoint($config); |
46
|
105 |
|
$this->config = new HttpClientConfig($config); |
47
|
|
|
|
48
|
105 |
|
$this->httpClient = $httpClient ?? new GuzzleClient($this->config->mapGuzzleHttpClientConfig()); |
49
|
105 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array<mixed> $config |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
105 |
|
public function generateEndpoint(array $config): string |
56
|
|
|
{ |
57
|
105 |
|
if (isset($config['endpoint'])) { |
58
|
|
|
return (string) $config['endpoint']; |
59
|
|
|
} |
60
|
105 |
|
$endpoint = 'http://localhost:8529'; |
61
|
105 |
|
if (isset($config['host'])) { |
62
|
2 |
|
$endpoint = (string) $config['host']; |
63
|
|
|
} |
64
|
105 |
|
if (isset($config['port'])) { |
65
|
2 |
|
$endpoint .= ':' . (string) $config['port']; |
66
|
|
|
} |
67
|
|
|
|
68
|
105 |
|
return $endpoint; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @psalm-suppress MixedReturnStatement |
73
|
|
|
* |
74
|
|
|
* @param string $method |
75
|
|
|
* @param string $uri |
76
|
|
|
* @param array<mixed>|HttpRequestOptions $options |
77
|
|
|
* @param string|null $database |
78
|
|
|
* @return stdClass |
79
|
|
|
* @throws ArangoException |
80
|
|
|
*/ |
81
|
105 |
|
public function request(string $method, string $uri, $options = [], ?string $database = null): stdClass |
82
|
|
|
{ |
83
|
105 |
|
$uri = $this->prependDatabaseToUri($uri, $database); |
84
|
|
|
|
85
|
105 |
|
if (is_array($options)) { |
86
|
105 |
|
$options = $this->prepareRequestOptions($options); |
87
|
|
|
} |
88
|
|
|
|
89
|
105 |
|
$response = null; |
|
|
|
|
90
|
|
|
try { |
91
|
105 |
|
$response = $this->httpClient->request($method, $uri, $options->all()); |
92
|
1 |
|
} catch (Throwable $e) { |
93
|
1 |
|
$this->handleGuzzleException($e); |
94
|
|
|
} |
95
|
|
|
|
96
|
105 |
|
return $this->cleanupResponse($response); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array<mixed> $options |
101
|
|
|
* @return HttpRequestOptions |
102
|
|
|
* @throws ArangoException |
103
|
|
|
*/ |
104
|
105 |
|
protected function prepareRequestOptions(array $options): HttpRequestOptions |
105
|
|
|
{ |
106
|
105 |
|
if (isset($options['body'])) { |
107
|
68 |
|
$options['body'] = $this->jsonEncode($options['body']); |
108
|
|
|
} |
109
|
|
|
|
110
|
105 |
|
return new HttpRequestOptions($options); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return the response with debug information (for internal testing purposes). |
115
|
|
|
* |
116
|
|
|
* @param string $method |
117
|
|
|
* @param string $uri |
118
|
|
|
* @param array<mixed> $options |
119
|
|
|
* @param string|null $database |
120
|
|
|
* @return ResponseInterface |
121
|
|
|
* @throws GuzzleException |
122
|
|
|
*/ |
123
|
2 |
|
public function debugRequest( |
124
|
|
|
string $method, |
125
|
|
|
string $uri, |
126
|
|
|
array $options = [], |
127
|
|
|
?string $database = null |
128
|
|
|
): ResponseInterface { |
129
|
2 |
|
$uri = $this->prependDatabaseToUri($uri, $database); |
130
|
2 |
|
$options['debug'] = true; |
131
|
|
|
|
132
|
2 |
|
return $this->httpClient->request($method, $uri, $options); |
133
|
|
|
} |
134
|
|
|
|
135
|
105 |
|
protected function prependDatabaseToUri(string $uri, ?string $database = null): string |
136
|
|
|
{ |
137
|
105 |
|
if (! isset($database)) { |
138
|
105 |
|
$database = $this->config->database; |
139
|
|
|
} |
140
|
105 |
|
return '/_db/' . urlencode($database) . $uri; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Throwable $e |
145
|
|
|
* @throws ArangoException |
146
|
|
|
*/ |
147
|
1 |
|
protected function handleGuzzleException(Throwable $e): void |
148
|
|
|
{ |
149
|
1 |
|
$message = $e->getMessage(); |
150
|
1 |
|
$code = $e->getCode(); |
151
|
|
|
|
152
|
1 |
|
if ($e instanceof RequestException && $e->hasResponse()) { |
153
|
1 |
|
$decodedResponse = $this->decodeResponse($e->getResponse()); |
154
|
1 |
|
$message = (string) $decodedResponse->errorMessage; |
155
|
1 |
|
$code = (int) $decodedResponse->code; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
throw( |
159
|
1 |
|
new ArangoException( |
160
|
1 |
|
$code . ' - ' . $message, |
161
|
1 |
|
(int) $code |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @psalm-suppress MixedAssignment, MixedArrayOffset |
168
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
169
|
|
|
* |
170
|
|
|
* @param ResponseInterface|null $response |
171
|
|
|
* @return stdClass |
172
|
|
|
*/ |
173
|
105 |
|
protected function cleanupResponse(?ResponseInterface $response): stdClass |
174
|
|
|
{ |
175
|
105 |
|
$response = $this->decodeResponse($response); |
176
|
105 |
|
unset($response->error); |
177
|
105 |
|
unset($response->code); |
178
|
|
|
|
179
|
105 |
|
return $response; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $query |
184
|
|
|
* @param array<scalar> $bindVars |
185
|
|
|
* @param array<mixed> $options |
186
|
|
|
* @return Traversable<mixed> |
187
|
|
|
*/ |
188
|
17 |
|
public function prepare( |
189
|
|
|
string $query, |
190
|
|
|
array $bindVars = [], |
191
|
|
|
array $options = [] |
192
|
|
|
): Traversable { |
193
|
17 |
|
return new Statement($this, $query, $bindVars, $options); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return array<array-key, mixed> |
|
|
|
|
198
|
|
|
*/ |
199
|
4 |
|
public function getConfig(): array |
200
|
|
|
{ |
201
|
4 |
|
return $this->config->toArray(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $name |
206
|
|
|
* @return void |
207
|
|
|
*/ |
208
|
105 |
|
public function setDatabase(string $name): void |
209
|
|
|
{ |
210
|
105 |
|
$this->config->database = $name; |
211
|
105 |
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
1 |
|
public function getDatabase(): string |
217
|
|
|
{ |
218
|
1 |
|
return $this->config->database; |
219
|
|
|
} |
220
|
|
|
|
221
|
1 |
|
public function setHttpClient(GuzzleClient $httpClient): void |
222
|
|
|
{ |
223
|
1 |
|
$this->httpClient = $httpClient; |
224
|
1 |
|
} |
225
|
|
|
|
226
|
1 |
|
public function getHttpClient(): GuzzleClient |
227
|
|
|
{ |
228
|
1 |
|
return $this->httpClient; |
229
|
|
|
} |
230
|
|
|
|
231
|
105 |
|
public function getUser(): string |
232
|
|
|
{ |
233
|
105 |
|
return (string) $this->config->username; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|