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
|
|
|
* |
42
|
|
|
* @throws UnknownProperties |
43
|
|
|
*/ |
44
|
107 |
|
public function __construct(array $config = [], GuzzleClient $httpClient = null) |
45
|
|
|
{ |
46
|
107 |
|
$config['endpoint'] = $this->generateEndpoint($config); |
47
|
107 |
|
$this->config = new HttpClientConfig($config); |
48
|
|
|
|
49
|
107 |
|
$this->httpClient = $httpClient ?? new GuzzleClient($this->config->mapGuzzleHttpClientConfig()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array<mixed> $config |
54
|
|
|
*/ |
55
|
107 |
|
public function generateEndpoint(array $config): string |
56
|
|
|
{ |
57
|
107 |
|
if (isset($config['endpoint'])) { |
58
|
|
|
return (string) $config['endpoint']; |
59
|
|
|
} |
60
|
107 |
|
$endpoint = 'http://localhost:8529'; |
61
|
107 |
|
if (isset($config['host'])) { |
62
|
2 |
|
$endpoint = (string) $config['host']; |
63
|
|
|
} |
64
|
107 |
|
if (isset($config['port'])) { |
65
|
2 |
|
$endpoint .= ':'.(string) $config['port']; |
66
|
|
|
} |
67
|
|
|
|
68
|
107 |
|
return $endpoint; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array<mixed>|HttpRequestOptions $options |
73
|
|
|
* |
74
|
|
|
* @throws ArangoException |
75
|
|
|
*/ |
76
|
107 |
|
public function request(string $method, string $uri, array|HttpRequestOptions $options = [], ?string $database = null): stdClass |
77
|
|
|
{ |
78
|
107 |
|
$uri = $this->prependDatabaseToUri($uri, $database); |
79
|
|
|
|
80
|
107 |
|
if (is_array($options)) { |
|
|
|
|
81
|
107 |
|
$options = $this->prepareRequestOptions($options); |
82
|
|
|
} |
83
|
|
|
|
84
|
107 |
|
$response = null; |
|
|
|
|
85
|
|
|
try { |
86
|
107 |
|
$response = $this->httpClient->request($method, $uri, $options->all()); |
87
|
2 |
|
} catch (Throwable $e) { |
88
|
2 |
|
$this->handleGuzzleException($e); |
89
|
|
|
} |
90
|
|
|
|
91
|
107 |
|
return $this->cleanupResponse($response); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array<mixed> $options |
96
|
|
|
* |
97
|
|
|
* @throws ArangoException |
98
|
|
|
*/ |
99
|
107 |
|
protected function prepareRequestOptions(array $options): HttpRequestOptions |
100
|
|
|
{ |
101
|
107 |
|
if (isset($options['body'])) { |
102
|
69 |
|
$options['body'] = $this->jsonEncode($options['body']); |
103
|
|
|
} |
104
|
|
|
|
105
|
107 |
|
return new HttpRequestOptions($options); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return the response with debug information (for internal testing purposes). |
110
|
|
|
* |
111
|
|
|
* @param array<mixed> $options |
112
|
|
|
* |
113
|
|
|
* @throws GuzzleException |
114
|
|
|
*/ |
115
|
2 |
|
public function debugRequest( |
116
|
|
|
string $method, |
117
|
|
|
string $uri, |
118
|
|
|
array $options = [], |
119
|
|
|
?string $database = null |
120
|
|
|
): ResponseInterface { |
121
|
2 |
|
$uri = $this->prependDatabaseToUri($uri, $database); |
122
|
2 |
|
$options['debug'] = true; |
123
|
|
|
|
124
|
2 |
|
return $this->httpClient->request($method, $uri, $options); |
125
|
|
|
} |
126
|
|
|
|
127
|
107 |
|
protected function prependDatabaseToUri(string $uri, ?string $database = null): string |
128
|
|
|
{ |
129
|
107 |
|
if (! isset($database)) { |
130
|
107 |
|
$database = $this->config->database; |
131
|
|
|
} |
132
|
|
|
|
133
|
107 |
|
return '/_db/'.urlencode($database).$uri; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @throws ArangoException |
138
|
|
|
*/ |
139
|
2 |
|
protected function handleGuzzleException(Throwable $e): void |
140
|
|
|
{ |
141
|
2 |
|
$message = $e->getMessage(); |
142
|
2 |
|
$code = $e->getCode(); |
143
|
|
|
|
144
|
2 |
|
if ($e instanceof RequestException && $e->hasResponse()) { |
145
|
2 |
|
$decodedResponse = $this->decodeResponse($e->getResponse()); |
146
|
2 |
|
$message = (string) $decodedResponse->errorMessage; |
147
|
2 |
|
$code = (int) $decodedResponse->code; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
throw( |
151
|
2 |
|
new ArangoException( |
152
|
|
|
$code.' - '.$message, |
153
|
|
|
$code |
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
160
|
|
|
*/ |
161
|
107 |
|
protected function cleanupResponse(?ResponseInterface $response): stdClass |
162
|
|
|
{ |
163
|
107 |
|
$response = $this->decodeResponse($response); |
164
|
107 |
|
unset($response->error); |
165
|
107 |
|
unset($response->code); |
166
|
|
|
|
167
|
107 |
|
return $response; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param array<scalar> $bindVars |
172
|
|
|
* @param array<mixed> $options |
173
|
|
|
* @return Statement |
174
|
|
|
*/ |
175
|
17 |
|
public function prepare( |
176
|
|
|
string $query, |
177
|
|
|
array $bindVars = [], |
178
|
|
|
array $options = [] |
179
|
|
|
): Traversable { |
180
|
17 |
|
return new Statement($this, $query, $bindVars, $options); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return array<array-key, mixed> |
|
|
|
|
185
|
|
|
*/ |
186
|
4 |
|
public function getConfig(): array |
187
|
|
|
{ |
188
|
4 |
|
return $this->config->toArray(); |
189
|
|
|
} |
190
|
|
|
|
191
|
107 |
|
public function setDatabase(string $name): void |
192
|
|
|
{ |
193
|
107 |
|
$this->config->database = $name; |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
public function getDatabase(): string |
197
|
|
|
{ |
198
|
1 |
|
return $this->config->database; |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
|
public function setHttpClient(GuzzleClient $httpClient): void |
202
|
|
|
{ |
203
|
1 |
|
$this->httpClient = $httpClient; |
204
|
|
|
} |
205
|
|
|
|
206
|
1 |
|
public function getHttpClient(): GuzzleClient |
207
|
|
|
{ |
208
|
1 |
|
return $this->httpClient; |
209
|
|
|
} |
210
|
|
|
|
211
|
107 |
|
public function getUser(): string |
212
|
|
|
{ |
213
|
107 |
|
return (string) $this->config->username; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|