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 5, 2024
1 parent d3ba4ac commit 8b124c6
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 174 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
18 changes: 18 additions & 0 deletions samples/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "opensearch-php/samples",
"description": "OpenSearch PHP client samples.",
"type": "project",
"authors": [],
"scripts": {
"index": ["php index.php"]
},
"repositories": [
{
"type": "path",
"url": "../"
}
],
"require": {
"opensearch-project/opensearch-php": "dev-main"
}
}
25 changes: 25 additions & 0 deletions samples/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

require_once __DIR__ . '/vendor/autoload.php';

$client = OpenSearch\ClientBuilder::fromConfig([
'Hosts' => [
'https://localhost:9200'
],
'BasicAuthentication' => ['admin', getenv('OPENSEARCH_PASSWORD')],
'Retries' => 2,
'SSLVerification' => false
]);

$info = $client->info();

print($info->getBody());

echo "{$info['version']['distribution']}: {$info['version']['number']}\n";

?>
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 8b124c6

Please sign in to comment.