Skip to content

Add Symfony HTTP client adapter and remove Accept-Encoding header (gzip) for Symfony #1243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"phpunit/phpunit": "^9.5",
"symfony/finder": "~4.0",
"nyholm/psr7": "^1.5",
"php-http/mock-client": "^1.5"
"php-http/mock-client": "^1.5",
"symfony/http-client": "^5.0|^6.0"
},
"autoload": {
"psr-4": {
Expand All @@ -45,6 +46,9 @@
"integration-test" : [
"vendor/bin/phpunit --testdox -c phpunit-integration-tests.xml"
],
"cloud-test" : [
"vendor/bin/phpunit --testdox -c phpunit-integration-cloud-tests.xml"
],
"phpstan": [
"phpstan analyse src --level 2 --no-progress"
]
Expand Down
26 changes: 26 additions & 0 deletions phpunit-integration-cloud-tests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" failOnRisky="true" verbose="true" beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="false">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<php>
<!-- Disable E_USER_DEPRECATED setting E_ALL & ~E_USER_DEPRECATED-->
<ini name="error_reporting" value="16383"/>
<ini name="memory_limit" value="-1"/>
</php>
<testsuites>
<testsuite name="Elastic Cloud tests">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>
<groups>
<include>
<group>cloud</group>
</include>
</groups>
<logging>
<junit outputFile="tests/yaml-test-junit.xml"/>
</logging>
</phpunit>
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<group>integration</group>
<group>free</group>
<group>platinum</group>
<group>cloud</group>
</exclude>
</groups>
</phpunit>
34 changes: 25 additions & 9 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use Elastic\Elasticsearch\Transport\Adapter\AdapterOptions;
use Elastic\Elasticsearch\Transport\RequestOptions;
use Elastic\Transport\NodePool\NodePoolInterface;
use Elastic\Transport\Transport;
use Elastic\Transport\TransportBuilder;
use GuzzleHttp\Client as GuzzleHttpClient;
use Http\Client\HttpAsyncClient;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -344,14 +346,11 @@ public function build(): Client
// Http client
if (!empty($this->httpClient)) {
$builder->setClient($this->httpClient);
} else {
// Set HTTP client options
if (!empty($this->getConfig()) || !empty($this->httpClientOptions)) {
$builder->setClient(
$this->setOptions($builder->getClient(), $this->getConfig(), $this->httpClientOptions)
);
}
}
// Set HTTP client options
$builder->setClient(
$this->setOptions($builder->getClient(), $this->getConfig(), $this->httpClientOptions)
);

// Cloud id
if (!empty($this->cloudId)) {
Expand Down Expand Up @@ -389,8 +388,11 @@ public function build(): Client
$transport->setHeader('Authorization', sprintf("ApiKey %s", $this->apiKey));
}

// Elastic cloud optimized with gzip
if (!empty($this->cloudId)) {
/**
* Elastic cloud optimized with gzip
* @see https://github.com/elastic/elasticsearch-php/issues/1241 omit for Symfony HTTP Client
*/
if (!empty($this->cloudId) && !$this->isSymfonyHttpClient($transport)) {
$transport->setHeader('Accept-Encoding', 'gzip');
}

Expand All @@ -401,6 +403,20 @@ public function build(): Client
return $client;
}

/**
* Returns true if the transport HTTP client is Symfony
*/
protected function isSymfonyHttpClient(Transport $transport): bool
{
if (false !== strpos(get_class($transport->getClient()), 'Symfony\Component\HttpClient')) {
return true;
}
if (false !== strpos(get_class($transport->getAsyncClient()), 'Symfony\Component\HttpClient')) {
return true;
}
return false;
}

/**
* Returns the configuration to be used in the HTTP client
*/
Expand Down
4 changes: 3 additions & 1 deletion src/Transport/Adapter/AdapterOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
final class AdapterOptions
{
const HTTP_ADAPTERS = [
"GuzzleHttp\\Client" => "Elastic\\Elasticsearch\\Transport\\Adapter\\Guzzle"
"GuzzleHttp\\Client" => "Elastic\\Elasticsearch\\Transport\\Adapter\\Guzzle",
"Symfony\\Component\\HttpClient\\HttplugClient" => "Elastic\\Elasticsearch\\Transport\\Adapter\\Symfony",
"Symfony\\Component\\HttpClient\\Psr18Client" => "Elastic\\Elasticsearch\\Transport\\Adapter\\Symfony"
];
}
22 changes: 10 additions & 12 deletions src/Transport/Adapter/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@

namespace Elastic\Elasticsearch\Transport\Adapter;

use Elastic\Elasticsearch\Exception\HttpClientException;
use Elastic\Elasticsearch\Transport\RequestOptions as Options;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Elastic\Elasticsearch\Transport\RequestOptions;
use GuzzleHttp\RequestOptions As GuzzleOptions;
use Psr\Http\Client\ClientInterface;

class Guzzle implements AdapterInterface
Expand All @@ -27,17 +25,17 @@ public function setConfig(ClientInterface $client, array $config, array $clientO
$guzzleConfig = [];
foreach ($config as $key => $value) {
switch ($key) {
case Options::SSL_CERT:
$guzzleConfig[RequestOptions::CERT] = $value;
case RequestOptions::SSL_CERT:
$guzzleConfig[GuzzleOptions::CERT] = $value;
break;
case Options::SSL_KEY:
$guzzleConfig[RequestOptions::SSL_KEY] = $value;
case RequestOptions::SSL_KEY:
$guzzleConfig[GuzzleOptions::SSL_KEY] = $value;
break;
case Options::SSL_VERIFY:
$guzzleConfig[RequestOptions::VERIFY] = $value;
case RequestOptions::SSL_VERIFY:
$guzzleConfig[GuzzleOptions::VERIFY] = $value;
break;
case Options::SSL_CA:
$guzzleConfig[RequestOptions::VERIFY] = $value;
case RequestOptions::SSL_CA:
$guzzleConfig[GuzzleOptions::VERIFY] = $value;
}
}
$class = get_class($client);
Expand Down
46 changes: 46 additions & 0 deletions src/Transport/Adapter/Symfony.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Elasticsearch PHP Client
*
* @link https://github.com/elastic/elasticsearch-php
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license https://opensource.org/licenses/MIT MIT License
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);

namespace Elastic\Elasticsearch\Transport\Adapter;

use Elastic\Elasticsearch\Transport\RequestOptions;
use Psr\Http\Client\ClientInterface;
use Symfony\Component\HttpClient\HttpClient;

class Symfony implements AdapterInterface
{
public function setConfig(ClientInterface $client, array $config, array $clientOptions): ClientInterface
{
$symfonyConfig = [];
foreach ($config as $key => $value) {
switch ($key) {
case RequestOptions::SSL_CERT:
$symfonyConfig['local_cert'] = $value;
break;
case RequestOptions::SSL_KEY:
$symfonyConfig['local_pk'] = $value;
break;
case RequestOptions::SSL_VERIFY:
$symfonyConfig['verify_host'] = $value;
$symfonyConfig['verify_peer'] = $value;
break;
case RequestOptions::SSL_CA:
$symfonyConfig['cafile'] = $value;
}
}
$class = get_class($client);
$httpClient = HttpClient::create(array_merge($clientOptions, $symfonyConfig));
return new $class($httpClient);
}
}
35 changes: 35 additions & 0 deletions tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpClient\HttplugClient;

class ClientBuilderTest extends TestCase
{
Expand Down Expand Up @@ -331,4 +333,37 @@ public function testSetHttpClientOptions()
$result = $this->builder->setHttpClientOptions([]);
$this->assertEquals($this->builder, $result);
}

public function testClientWithSymfonyPsr18Client()
{
$symfonyClient = new Psr18Client();
$client = ClientBuilder::create()
->setHttpClient($symfonyClient)
->build();

$this->assertInstanceOf(Client::class, $client);
$this->assertEquals($symfonyClient, $client->getTransport()->getClient());
}

public function testClientWithSymfonyHttplugClient()
{
$symfonyClient = new HttplugClient();
$client = ClientBuilder::create()
->setHttpClient($symfonyClient)
->build();

$this->assertInstanceOf(Client::class, $client);
$this->assertEquals($symfonyClient, $client->getTransport()->getClient());
}

public function testAsyncClientWithSymfonyHttplugClient()
{
$symfonyClient = new HttplugClient();
$client = ClientBuilder::create()
->setAsyncHttpClient($symfonyClient)
->build();

$this->assertInstanceOf(Client::class, $client);
$this->assertEquals($symfonyClient, $client->getTransport()->getAsyncClient());
}
}
2 changes: 0 additions & 2 deletions tests/Integration/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

namespace Elastic\Elasticsearch\Tests\Integration;

use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Tests\Utility;
use Elastic\Transport\Exception\NoNodeAvailableException;
use PHPUnit\Framework\TestCase;

/**
Expand Down
77 changes: 77 additions & 0 deletions tests/Integration/ElasticCloudTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Elasticsearch PHP Client
*
* @link https://github.com/elastic/elasticsearch-php
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license https://opensource.org/licenses/MIT MIT License
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);

namespace Elastic\Elasticsearch\Tests\Integration;

use Elastic\Elasticsearch\ClientBuilder;
use Http\Promise\Promise;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpClient\HttplugClient;

/**
* @group cloud
*/
class ElasticCloudTest extends TestCase
{
const CLOUD_ID = 'ELASTIC_CLOUD_ID';
const API_KEY = 'ELASTIC_API_KEY';

protected ClientBuilder $clientBuilder;

public function setUp(): void
{
if (!getenv(self::CLOUD_ID) && !getenv(self::API_KEY)) {
$this->markTestSkipped(sprintf(
"I cannot execute the Elastic Cloud test without the env variables %s and %s",
self::CLOUD_ID,
self::API_KEY
));
}
$this->clientBuilder = ClientBuilder::create()
->setElasticCloudId(getenv(self::CLOUD_ID))
->setApiKey(getenv(self::API_KEY));
}

public function testInfoWithAsyncSymfonyHttplugClient()
{
$symfonyClient = new HttplugClient();

$client = $this->clientBuilder->setAsyncHttpClient($symfonyClient)
->build();

$client->setAsync(true);

$promise = $client->info();
$this->assertInstanceOf(Promise::class, $promise);
$response = $promise->wait();

$this->assertEquals(200, $response->getStatusCode());
$this->assertNotEmpty($response['name']);
$this->assertNotEmpty($response['version']['number']);
}

public function testInfoWithSymfonyHttpPsr18Client()
{
$symfonyClient = new Psr18Client();

$client = $this->clientBuilder->setHttpClient($symfonyClient)
->build();

$response = $client->info();
$this->assertEquals(200, $response->getStatusCode());
$this->assertNotEmpty($response['name']);
$this->assertNotEmpty($response['version']['number']);
}
}
Loading