Skip to content

Commit 9150f71

Browse files
committed
Added AdapterOptions class and setNodePool() in ClientBuilder
1 parent fdfb0c1 commit 9150f71

File tree

5 files changed

+91
-33
lines changed

5 files changed

+91
-33
lines changed

BREAKING_CHANGES.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ We tried to reduce the BC breaks as much as possible but there are some (big) di
55

66
## Architectural changes:
77

8-
- we changed the namespace, now everything is under `Elastic\Elasticsearch`;
8+
- we changed the namespace, now everything is under `Elastic\Elasticsearch`
99
- we used the [elastic-transport-php](https://github.com/elastic/elastic-transport-php) library for HTTP communications;
1010
- we changed the `Exception` model, using the namespace `Elastic\Elasticsearch\Exception`. All the exceptions extends the
11-
`ElasticsearchException` interface, as in 7.x;
11+
`ElasticsearchException` interface, as in 7.x
1212
- we changed the response type of each endpoints using an [Elasticsearch](src/Response/Elasticsearch.php) response class.
1313
This class wraps a a [PSR-7](https://www.php-fig.org/psr/psr-7/) response allowing the access of the body response
1414
as array or object. This means you can access the API response as in 7.x, no BC break here! :angel:
15+
- we changed the `ConnectionPool` in `NodePool`. The `connection` naming was ambigous since the objects are nodes (hosts)
1516

1617
## Specific changes:
1718

@@ -24,15 +25,15 @@ The following functions has been removed:
2425
- `ClientBuilder::multiHandler()`
2526
- `ClientBuilder::singleHandler()`
2627
- `ClientBuilder::setConnectionFactory()`
27-
- `ClientBuilder::setConnectionPool()`
28+
- `ClientBuilder::setConnectionPool()`, you can use `ClientBuilder::setNodePool` instead
2829
- `ClientBuilder::setEndpoint()`
2930
- `ClientBuilder::registerNamespace()`
3031
- `ClientBuilder::setTransport()`, you can specify an HTTP PSR-18 client using `ClientBuilder::setHttpClient()`
3132
- `ClientBuilder::setHandler()`
3233
- `ClientBuilder::setTracer()`, you can only set a Logger using `ClientBuilder::setLogger()`
3334
- `ClientBuilder::setSerializer()`
3435
- `ClientBuilder::setConnectionParams()`, you can use `ClientBuilder::setHttpClientOptions()` instead
35-
- `ClientBuilder::setSelector()`
36+
- `ClientBuilder::setSelector()`, you can set a `Selector` using the `setNodePool`, see [here](https://github.com/elastic/elastic-transport-php/blob/8.x/README.md#use-a-custom-selector) for more information
3637
- `ClientBuilder::setSniffOnStart()`
3738
- `ClientBuilder::includePortInHostHeader()`
3839

src/ClientBuilder.php

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,39 @@
1919
use Elastic\Elasticsearch\Exception\HttpClientException;
2020
use Elastic\Elasticsearch\Exception\InvalidArgumentException;
2121
use Elastic\Elasticsearch\Transport\Adapter\AdapterInterface;
22+
use Elastic\Elasticsearch\Transport\Adapter\AdapterOptions;
2223
use Elastic\Elasticsearch\Transport\RequestOptions;
23-
use Elastic\Transport\Transport;
24+
use Elastic\Transport\NodePool\NodePoolInterface;
2425
use Elastic\Transport\TransportBuilder;
2526
use Http\Client\HttpAsyncClient;
2627
use Psr\Http\Client\ClientInterface;
2728
use Psr\Log\LoggerInterface;
28-
use Psr\Log\NullLogger;
2929
use ReflectionClass;
3030

3131
class ClientBuilder
3232
{
3333
const DEFAULT_HOST = 'localhost:9200';
34-
const HTTP_ADAPTERS = [
35-
"GuzzleHttp\\Client" => "Elastic\\Elasticsearch\\Transport\\Adapter\\Guzzle"
36-
];
3734

38-
private Transport $transport;
35+
/**
36+
* PSR-18 client
37+
*/
3938
private ClientInterface $httpClient;
39+
40+
/**
41+
* The HTTP async client
42+
*/
4043
private HttpAsyncClient $asyncHttpClient;
44+
45+
/**
46+
* PSR-3 Logger
47+
*/
4148
private LoggerInterface $logger;
4249

50+
/**
51+
* The NodelPool
52+
*/
53+
private NodePoolInterface $nodePool;
54+
4355
/**
4456
* Hosts (elasticsearch nodes)
4557
*/
@@ -181,6 +193,15 @@ public function setLogger(LoggerInterface $logger): ClientBuilder
181193
return $this;
182194
}
183195

196+
/**
197+
* Set the NodePool
198+
*/
199+
public function setNodePool(NodePoolInterface $nodePool): ClientBuilder
200+
{
201+
$this->nodePool = $nodePool;
202+
return $this;
203+
}
204+
184205
/**
185206
* Set the hosts (nodes)
186207
*/
@@ -290,21 +311,21 @@ public function setHttpClientOptions(array $options): ClientBuilder
290311
*/
291312
public function build(): Client
292313
{
293-
// Logger
294-
if (empty($this->logger)) {
295-
$this->logger = new NullLogger();
296-
}
314+
// Transport builder
315+
$builder = TransportBuilder::create();
297316

298317
// Set the default hosts if empty
299318
if (empty($this->hosts)) {
300319
$this->hosts = [self::DEFAULT_HOST];
301320
}
302-
303-
// Transport
304-
$builder = TransportBuilder::create();
305-
$builder->setLogger($this->logger);
306321
$builder->setHosts($this->hosts);
307322

323+
// Logger
324+
if (!empty($this->logger)) {
325+
$builder->setLogger($this->logger);
326+
}
327+
328+
// Http client
308329
if (!empty($this->httpClient)) {
309330
$builder->setClient($this->httpClient);
310331
} else {
@@ -315,44 +336,49 @@ public function build(): Client
315336
);
316337
}
317338
}
339+
340+
// Cloud id
318341
if (!empty($this->cloudId)) {
319342
$builder->setCloudId($this->cloudId);
320343
}
344+
345+
// Node Pool
321346
if (!empty($this->nodePool)) {
322347
$builder->setNodePool($this->nodePool);
323348
}
324-
$this->transport = $builder->build();
349+
350+
$transport = $builder->build();
325351

326352
// The default retries is equal to the number of hosts
327353
if (empty($this->retries)) {
328354
$this->retries = count($this->hosts);
329355
}
330-
$this->transport->setRetries($this->retries);
356+
$transport->setRetries($this->retries);
331357

332358
// Async client
333359
if (!empty($this->asyncHttpClient)) {
334-
$this->transport->setAsyncClient($this->asyncHttpClient);
360+
$transport->setAsyncClient($this->asyncHttpClient);
335361
}
336362

337363
// Basic authentication
338364
if (!empty($this->username) && !empty($this->password)) {
339-
$this->transport->setUserInfo($this->username, $this->password);
365+
$transport->setUserInfo($this->username, $this->password);
340366
}
341367

342368
// API key
343369
if (!empty($this->apiKey)) {
344370
if (!empty($this->username)) {
345371
throw new AuthenticationException('You cannot use APIKey and Basic Authenication together');
346372
}
347-
$this->transport->setHeader('Authorization', sprintf("ApiKey %s", $this->apiKey));
373+
$transport->setHeader('Authorization', sprintf("ApiKey %s", $this->apiKey));
348374
}
349375

350376
// Elastic cloud optimized with gzip
351377
if (!empty($this->cloudId)) {
352-
$this->transport->setHeader('Accept-Encoding', 'gzip');
378+
$transport->setHeader('Accept-Encoding', 'gzip');
353379
}
354380

355-
$client = new Client($this->transport, $this->logger);
381+
$client = new Client($transport, $transport->getLogger());
356382
// Enable or disable the x-elastic-client-meta header
357383
$client->setElasticMetaHeader($this->elasticMetaHeader);
358384

@@ -386,13 +412,13 @@ protected function setOptions(ClientInterface $client, array $config, array $cli
386412
return $client;
387413
}
388414
$class = get_class($client);
389-
if (!isset(self::HTTP_ADAPTERS[$class])) {
415+
if (!isset(AdapterOptions::HTTP_ADAPTERS[$class])) {
390416
throw new HttpClientException(sprintf(
391417
"The HTTP client %s is not supported for custom options",
392418
$class
393419
));
394420
}
395-
$adapterClass = self::HTTP_ADAPTERS[$class];
421+
$adapterClass = AdapterOptions::HTTP_ADAPTERS[$class];
396422
if (!class_exists($adapterClass) || !in_array(AdapterInterface::class, class_implements($adapterClass))) {
397423
throw new HttpClientException(sprintf(
398424
"The class %s does not exists or does not implement %s",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Transport\Adapter;
16+
17+
/**
18+
* The HTTP client adapters supported
19+
*/
20+
final class AdapterOptions
21+
{
22+
const HTTP_ADAPTERS = [
23+
"GuzzleHttp\\Client" => "Elastic\\Elasticsearch\\Transport\\Adapter\\Guzzle"
24+
];
25+
}

tests/ClientBuilderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Elastic\Elasticsearch\Exception\AuthenticationException;
2020
use Elastic\Elasticsearch\Exception\ConfigException;
2121
use Elastic\Elasticsearch\Exception\InvalidArgumentException;
22+
use Elastic\Transport\NodePool\NodePoolInterface;
2223
use Http\Client\HttpAsyncClient;
2324
use Nyholm\Psr7\Factory\Psr17Factory;
2425
use PHPUnit\Framework\TestCase;
@@ -35,6 +36,7 @@ public function setUp(): void
3536
$this->httpClient = $this->createStub(ClientInterface::class);
3637
$this->asyncHttpClient = $this->createStub(HttpAsyncClient::class);
3738
$this->logger = $this->createStub(LoggerInterface::class);
39+
$this->nodePool = $this->createStub(NodePoolInterface::class);
3840
$this->psr17Factory = new Psr17Factory();
3941
$this->builder = ClientBuilder::create();
4042
}
@@ -116,6 +118,12 @@ public function testSetLogger()
116118
$this->assertEquals($this->builder, $result);
117119
}
118120

121+
public function testSetNodePool()
122+
{
123+
$result = $this->builder->setNodePool($this->nodePool);
124+
$this->assertEquals($this->builder, $result);
125+
}
126+
119127
public function testSetHosts()
120128
{
121129
$result = $this->builder->setHosts(['localhost:9200']);

tests/ClientTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
namespace Elastic\Elasticsearch\Tests;
1616

1717
use Elastic\Elasticsearch\Client;
18-
use Elastic\Elasticsearch\Exception\NamespaceException;
1918
use Elastic\Elasticsearch\Exception\ProductCheckException;
2019
use Elastic\Elasticsearch\Exception\ClientResponseException;
2120
use Elastic\Elasticsearch\Exception\ServerResponseException;
2221
use Elastic\Elasticsearch\Response\Elasticsearch;
22+
use Elastic\Transport\NodePool\NodePoolInterface;
2323
use Elastic\Transport\TransportBuilder;
2424
use Http\Mock\Client as MockClient;
2525
use Http\Promise\Promise;
@@ -33,12 +33,10 @@ public function setUp(): void
3333
{
3434
$this->logger = $this->createStub(LoggerInterface::class);
3535
$this->httpClient = new MockClient();
36-
//$this->httpAsyncClient = $this->createStub(HttpAsyncClient::class);
3736

3837
$this->transport = TransportBuilder::create()
3938
->setClient($this->httpClient)
4039
->build();
41-
//$this->transport->setAsyncClient($this->httpAsyncClient);
4240

4341
$this->psr17Factory = new Psr17Factory();
4442
$this->client = new Client($this->transport, $this->logger);
@@ -59,20 +57,20 @@ public function testDefaultAsyncIsFalse()
5957
$this->assertFalse($this->client->getAsync());
6058
}
6159

62-
public function testSetAsync()
60+
public function testSetGetAsync()
6361
{
6462
$async = $this->client->getAsync();
6563
$this->client->setAsync(!$async);
6664
$this->assertEquals(!$async, $this->client->getAsync());
6765
}
6866

69-
public function testSetElasticMetaHeader()
67+
public function testSetGetElasticMetaHeader()
7068
{
7169
$this->client->setElasticMetaHeader(false);
7270
$this->assertFalse($this->client->getElasticMetaHeader());
7371
}
7472

75-
public function testSetResponseException()
73+
public function testSetGetResponseException()
7674
{
7775
$this->client->setResponseException(false);
7876
$this->assertFalse($this->client->getResponseException());

0 commit comments

Comments
 (0)