Skip to content

Commit 3c6c370

Browse files
author
Deploy
committed
feat: added disconnect functionality
1 parent 84dc9c2 commit 3c6c370

File tree

3 files changed

+126
-7
lines changed

3 files changed

+126
-7
lines changed

docs/arangodb-client.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,28 @@ Send a request to ArangoDB's HTTP REST API. This is mostly for internal use but
6666
$arangoClient->request(
6767
'get',
6868
'/_api/version',
69-
'query' => [
70-
'details' => $details
69+
[
70+
'query' => [
71+
'details' => $details
72+
]
73+
]
74+
]);
75+
```
76+
77+
### rawRequest(string $method, string $uri, array|HttpRequestOptions $options = []): ResponseInterface|null
78+
Returns the raw response of the request.
79+
*Note* that the request itself is made against the configured endpoint but the databasename is _not_ automatically
80+
prepended to the uri as opposed to a regular request.
81+
82+
83+
```
84+
$arangoClient->rawRequest(
85+
'get',
86+
'/_api/version',
87+
[
88+
'query' => [
89+
'details' => $details
90+
]
7191
]
7292
]);
7393
```
@@ -106,4 +126,12 @@ $arangoClient->schema()->createCollection('users');
106126
Pass chained method to the admin manager.
107127
```
108128
$arangoClient->admin()->getVersion();
129+
```
130+
131+
### disconnect(): bool
132+
Disconnect from the current keep-alive connection, if any.
133+
*Note* that a disconnect request is sent to the database upon destruction of the arangoClient object as well.
134+
135+
```
136+
$arangoClient->disconnect();
109137
```

src/ArangoClient.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,42 @@ public function __construct(array $config = [], ?GuzzleClient $httpClient = null
5050
$this->httpClient = $httpClient ?? new GuzzleClient($this->config->mapGuzzleHttpClientConfig());
5151
}
5252

53+
public function __destruct()
54+
{
55+
$this->disconnect();
56+
}
57+
58+
public function disconnect(): bool
59+
{
60+
$config = $this->getConfig();
61+
62+
if ($config['connection'] !== 'Keep-Alive') {
63+
return true;
64+
}
65+
66+
$response = $this->rawRequest(
67+
'HEAD',
68+
'/_api/version',
69+
[
70+
'headers' => [
71+
'Connection' => 'close',
72+
],
73+
],
74+
);
75+
76+
if ($response === null) {
77+
return false;
78+
}
79+
80+
$connection = $response->getHeader('Connection');
81+
if (reset($connection) !== 'Close') {
82+
return false;
83+
}
84+
85+
return true;
86+
}
87+
88+
5389
/**
5490
* @param array<mixed> $config
5591
*/
@@ -58,10 +94,12 @@ public function generateEndpoint(array $config): string
5894
if (isset($config['endpoint'])) {
5995
return (string) $config['endpoint'];
6096
}
97+
6198
$endpoint = 'http://localhost:8529';
6299
if (isset($config['host'])) {
63100
$endpoint = (string) $config['host'];
64101
}
102+
65103
if (isset($config['port'])) {
66104
$endpoint .= ':' . (string) $config['port'];
67105
}
@@ -96,6 +134,28 @@ public function request(string $method, string $uri, array|HttpRequestOptions $o
96134
return new stdClass();
97135
}
98136

137+
/**
138+
* @param array<mixed>|HttpRequestOptions $options
139+
*
140+
* @throws ArangoException
141+
*/
142+
public function rawRequest(string $method, string $uri, array|HttpRequestOptions $options = []): ResponseInterface|null
143+
{
144+
if (is_array($options)) {
145+
$options = $this->prepareRequestOptions($options);
146+
}
147+
148+
$response = null;
149+
try {
150+
$response = $this->httpClient->request($method, $uri, $options->all());
151+
} catch (Throwable $e) {
152+
$this->handleGuzzleException($e);
153+
}
154+
155+
return $response;
156+
}
157+
158+
99159
/**
100160
* @param array<mixed> $options
101161
*

tests/ArangoClientTest.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44

55
use ArangoClient\Admin\AdminManager;
66
use ArangoClient\ArangoClient;
7+
use ArangoClient\Http\HttpClientConfig;
78
use ArangoClient\Schema\SchemaManager;
89
use ArangoClient\Statement\Statement;
910
use GuzzleHttp\Client;
11+
use GuzzleHttp\Client as GuzzleClient;
1012
use GuzzleHttp\Handler\MockHandler;
1113
use GuzzleHttp\HandlerStack;
1214
use GuzzleHttp\Middleware;
1315
use GuzzleHttp\Psr7\Response;
1416

17+
use function PHPUnit\Framework\assertTrue;
18+
1519
uses(Tests\TestCase::class);
1620

1721
test('get config', function () {
@@ -46,21 +50,21 @@
4650
test('client with host port config', function () {
4751
$config = [
4852
'host' => 'http://127.0.0.1',
49-
'port' => '1234',
53+
'port' => '8529',
5054
'username' => 'root',
5155
];
5256
$client = new ArangoClient($config);
5357
$retrievedConfig = $client->getConfig();
5458

55-
expect($retrievedConfig['endpoint'])->toEqual('http://127.0.0.1:1234');
59+
expect($retrievedConfig['endpoint'])->toEqual('http://127.0.0.1:8529');
5660
});
5761

5862
test('config with alien properties', function () {
5963
$config = [
6064
'name' => 'arangodb',
6165
'driver' => 'arangodb',
6266
'host' => 'http://127.0.0.1',
63-
'port' => '1234',
67+
'port' => '8529',
6468
'username' => 'root',
6569
];
6670
$client = new ArangoClient($config);
@@ -73,8 +77,26 @@
7377
test('set and get http client', function () {
7478
$oldClient = $this->arangoClient->getHttpClient();
7579

76-
$newClient = Mockery::mock(Client::class);
80+
$defaultConfig = [
81+
'endpoint' => 'http://localhost:8529',
82+
'host' => null,
83+
'port' => null,
84+
'version' => 1.1,
85+
'connection' => 'Keep-Alive',
86+
'allow_redirects' => false,
87+
'connect_timeout' => 0.0,
88+
'username' => 'root',
89+
'password' => null,
90+
'database' => $this->testDatabaseName,
91+
'jsonStreamDecoderThreshold' => 1048576,
92+
];
93+
94+
$config = new HttpClientConfig($defaultConfig);
95+
96+
$newClient = new GuzzleClient($config->mapGuzzleHttpClientConfig());
97+
7798
$this->arangoClient->setHttpClient($newClient);
99+
78100
$retrievedClient = $this->arangoClient->getHttpClient();
79101

80102
expect($oldClient)->toBeInstanceOf(Client::class);
@@ -103,10 +125,13 @@
103125

104126
$database = $this->arangoClient->getDatabase();
105127
expect($database)->toBe($newDatabaseName);
128+
129+
// Reset DB name
130+
$this->arangoClient->setDatabase($this->testDatabaseName);
106131
});
107132

108133
test('database name is used in requests', function () {
109-
$database = 'some_database';
134+
$database = 'arangodb_php_client__test';
110135
if (!$this->arangoClient->schema()->hasDatabase($database)) {
111136
$this->arangoClient->schema()->createDatabase($database);
112137
}
@@ -234,3 +259,9 @@
234259

235260
$this->schemaManager->deleteCollection($collection);
236261
});
262+
263+
test('disconnect', function () {
264+
$disconnected = $this->arangoClient->disconnect();
265+
266+
assertTrue($disconnected);
267+
});

0 commit comments

Comments
 (0)