Skip to content

Commit

Permalink
WIP: Upgrading guzzle.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Aug 7, 2024
1 parent 1fae7e5 commit 787aa5d
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 160 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
],
"require": {
"php": "^7.3 || ^8.0",
"ext-json": ">=1.3.7",
"ext-curl": "*",
"ezimuel/ringphp": "^1.1.2",
"ext-json": ">=1.3.7",
"guzzlehttp/guzzle": "^7.9",
"guzzlehttp/promises": "^2.0",
"psr/http-client": "^1.0",
"psr/http-message": "^1.1 || ^2.0",
"psr/log": "^1|^2|^3",
"symfony/yaml": "*"
},
Expand Down
85 changes: 45 additions & 40 deletions src/OpenSearch/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
use OpenSearch\Namespaces\NamespaceBuilderInterface;
use OpenSearch\Serializers\SerializerInterface;
use OpenSearch\Serializers\SmartSerializer;
use GuzzleHttp\Ring\Client\CurlHandler;
use GuzzleHttp\Ring\Client\CurlMultiHandler;
use GuzzleHttp\Ring\Client\Middleware;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use ReflectionClass;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Handler\CurlMultiHandler;
use GuzzleHttp\Handler\Proxy;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Utils;

class ClientBuilder
{
Expand Down Expand Up @@ -239,30 +241,30 @@ public static function fromConfig(array $config, bool $quiet = false): Client
return $builder->build();
}

/**
* Get the default handler
*
* @param array $multiParams
* @param array $singleParams
* @throws \RuntimeException
*/
public static function defaultHandler(array $multiParams = [], array $singleParams = []): callable
{
$future = null;
if (extension_loaded('curl')) {
$config = array_merge([ 'mh' => curl_multi_init() ], $multiParams);
if (function_exists('curl_reset')) {
$default = new CurlHandler($singleParams);
$future = new CurlMultiHandler($config);
} else {
$default = new CurlMultiHandler($config);
}
} else {
throw new \RuntimeException('OpenSearch-PHP requires cURL, or a custom HTTP handler.');
}

return $future ? Middleware::wrapFuture($default, $future) : $default;
}
// /**
// * Get the default handler
// *
// * @param array $multiParams
// * @param array $singleParams
// * @throws \RuntimeException
// */
// public static function defaultHandler(array $multiParams = [], array $singleParams = []): callable
// {
// $future = null;
// if (extension_loaded('curl')) {
// $config = array_merge([ 'mh' => curl_multi_init() ], $multiParams);
// if (function_exists('curl_reset')) {
// $default = new CurlHandler($singleParams);
// $future = new CurlMultiHandler($config);
// } else {
// $default = new CurlMultiHandler($config);
// }
// } else {
// throw new \RuntimeException('OpenSearch-PHP requires cURL, or a custom HTTP handler.');
// }

// return $future ? Proxy::wrapSync($default, $future) : $default;
// }

/**
* Get the multi handler for async (CurlMultiHandler)
Expand Down Expand Up @@ -583,7 +585,7 @@ public function build(): Client
$this->buildLoggers();

if (is_null($this->handler)) {
$this->handler = ClientBuilder::defaultHandler();
$this->handler = Utils::chooseHandler(); // ClientBuilder::defaultHandler();
}

if (!is_null($this->sigV4CredentialProvider)) {
Expand All @@ -610,18 +612,21 @@ public function build(): Client
}

if (!is_null($sslOptions)) {
$sslHandler = function (callable $handler, array $sslOptions) {
return function (array $request) use ($handler, $sslOptions) {
// Add our custom headers
foreach ($sslOptions as $key => $value) {
$request['client'][$key] = $value;
}

// Send the request using the handler and return the response.
return $handler($request);
};
};
$this->handler = $sslHandler($this->handler, $sslOptions);
// $sslHandler = function (callable $handler, array $sslOptions) {
// return function (RequestInterface $request, array $options) use ($handler, $sslOptions) {
// // Add our custom headers
// foreach ($sslOptions as $key => $value) {
// $request['client'][$key] = $value;
// }

// // Send the request using the handler and return the response.
// return $handler($request);
// };
// };
// $this->handler = $sslHandler($this->handler, $sslOptions);
foreach ($sslOptions as $key => $value) {
$this->connectionParams['client'][$key] = $value;
}
}

if (is_null($this->serializer)) {
Expand Down
Loading

0 comments on commit 787aa5d

Please sign in to comment.