Skip to content

Commit

Permalink
Switch to PSR interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Kim Pepper <[email protected]>
  • Loading branch information
kimpepper committed Oct 22, 2024
1 parent 1866e6e commit 2f4661d
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 154 deletions.
34 changes: 24 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,33 @@
}
],
"require": {
"php": "^7.3 || ^8.0",
"ext-json": ">=1.3.7",
"php": "^8.1",
"ext-curl": "*",
"ezimuel/ringphp": "^1.2.2",
"ext-json": ">=1.3.7",
"php-http/async-client-implementation": "^1.0",
"php-http/discovery": "^1.20",
"php-http/guzzle7-adapter": "^1.0",
"psr/http-client": "^1.0",
"psr/http-client-implementation": "^1.0",
"psr/http-factory": "^1.1",
"psr/http-factory-implementation": "^2.4",
"psr/http-message": "^2.0",
"psr/http-message-implementation": "^1.0",
"psr/log": "^1|^2|^3",
"symfony/yaml": "*"
},
"require-dev": {
"ext-zip": "*",
"aws/aws-sdk-php": "^3.0",
"friendsofphp/php-cs-fixer": "^3.0",
"mockery/mockery": "^1.2",
"phpstan/phpstan": "^1.7.15",
"phpstan/phpstan-mockery": "^1.1.0",
"phpunit/phpunit": "^9.3",
"symfony/finder": "~4.0 || ~5.0"
"friendsofphp/php-cs-fixer": "^v3.64",
"guzzlehttp/psr7": "^2.7",
"mockery/mockery": "^1.6",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^9.6",
"symfony/finder": "^6.4|^7.0",
"symfony/http-client": "^7.1",
"symfony/http-client-contracts": "^3.5"
},
"suggest": {
"monolog/monolog": "Allows for client-level logging and tracing",
Expand All @@ -54,7 +65,10 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": true
}
},
"scripts": {
"php-cs": [
Expand Down
42 changes: 35 additions & 7 deletions samples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,45 @@
* SPDX-License-Identifier: Apache-2.0
*/

use OpenSearch\Client;

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

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

$guzzleClient = new \GuzzleHttp\Client([
'base_uri' => 'https://localhost:9200',
'auth' => ['admin', getenv('OPENSEARCH_PASSWORD')],
'verify' => false,
'retries' => 2,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'User-Agent' => sprintf('opensearch-php/%s (%s; PHP %s)', Client::VERSION, PHP_OS, PHP_VERSION),
]
]);
$requestFactory = new \OpenSearch\RequestFactory();
$transport = new OpenSearch\Transport($guzzleClient, $requestFactory);

$client = new OpenSearch\Client($transport, $requestFactory, $httpFactory);
$info = $client->info();

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

// Symfony example

$symfonyClient = \Symfony\Component\HttpClient\HttpClient::create([
'base_uri' => 'https://localhost:9200',
'auth_basic' => ['admin', getenv('OPENSEARCH_PASSWORD')],
'verify_peer' => false,
'max_retries' => 2,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
]);

$client = new OpenSearch\Client($symfonyClient);
$info = $client->info();

echo "{$info['version']['distribution']}: {$info['version']['number']}\n";
62 changes: 46 additions & 16 deletions src/OpenSearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@

namespace OpenSearch;

use Http\Promise\Promise;
use OpenSearch\Common\Exceptions\BadMethodCallException;
use OpenSearch\Common\Exceptions\NoNodesAvailableException;
use OpenSearch\Endpoints\AbstractEndpoint;
use OpenSearch\Namespaces\NamespaceBuilderInterface;
use OpenSearch\Namespaces\BooleanRequestWrapper;
use OpenSearch\Namespaces\AsyncSearchNamespace;
use OpenSearch\Namespaces\BooleanRequestWrapper;
use OpenSearch\Namespaces\CatNamespace;
use OpenSearch\Namespaces\ClusterNamespace;
use OpenSearch\Namespaces\DanglingIndicesNamespace;
Expand All @@ -36,21 +35,23 @@
use OpenSearch\Namespaces\KnnNamespace;
use OpenSearch\Namespaces\MlNamespace;
use OpenSearch\Namespaces\MonitoringNamespace;
use OpenSearch\Namespaces\NamespaceBuilderInterface;
use OpenSearch\Namespaces\NodesNamespace;
use OpenSearch\Namespaces\NotificationsNamespace;
use OpenSearch\Namespaces\ObservabilityNamespace;
use OpenSearch\Namespaces\PplNamespace;
use OpenSearch\Namespaces\QueryNamespace;
use OpenSearch\Namespaces\RemoteStoreNamespace;
use OpenSearch\Namespaces\RollupsNamespace;
use OpenSearch\Namespaces\SearchPipelineNamespace;
use OpenSearch\Namespaces\SearchableSnapshotsNamespace;
use OpenSearch\Namespaces\SearchPipelineNamespace;
use OpenSearch\Namespaces\SecurityNamespace;
use OpenSearch\Namespaces\SnapshotNamespace;
use OpenSearch\Namespaces\SqlNamespace;
use OpenSearch\Namespaces\SslNamespace;
use OpenSearch\Namespaces\TasksNamespace;
use OpenSearch\Namespaces\TransformsNamespace;
use Psr\Http\Message\ResponseInterface;

/**
* Class Client
Expand All @@ -66,6 +67,11 @@ class Client
*/
public $transport;

/**
* Whether the client is async mode.
*/
protected bool $isAsync = false;

/**
* @var array
*/
Expand Down Expand Up @@ -1882,35 +1888,59 @@ public function extractArgument(array &$params, string $arg)
}
}

/**
* Check if the client is running in async mode.
*/
public function isAsync(): bool
{
return $this->isAsync;
}

/**
* Set the client to run in async mode.
*/
public function setAsync(bool $isAsync): Client
{
$this->isAsync = $isAsync;
return $this;
}

/**
* Sends a raw request to the cluster
* @return callable|array
* @throws NoNodesAvailableException
* @return \Http\Promise\Promise|\Psr\Http\Message\ResponseInterface
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \Exception
*/
public function request(string $method, string $uri, array $attributes = [])
public function request(string $method, string $uri, array $attributes = []): Promise|ResponseInterface
{
$params = $attributes['params'] ?? [];
$body = $attributes['body'] ?? null;
$options = $attributes['options'] ?? [];

$promise = $this->transport->performRequest($method, $uri, $params, $body, $options);
$request = $this->transport->createRequest($method, $uri, $params, $body);

return $this->transport->resultOrFuture($promise, $options);
if ($this->isAsync) {
return $this->transport->sendAsyncRequest($request);
}
return $this->transport->sendRequest($request);
}

/**
* @return callable|array
* @return \Http\Promise\Promise|\Psr\Http\Message\ResponseInterface
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \Exception
*/
private function performRequest(AbstractEndpoint $endpoint)
private function performRequest(AbstractEndpoint $endpoint): Promise|ResponseInterface
{
$promise = $this->transport->performRequest(
$request = $this->transport->createRequest(
$endpoint->getMethod(),
$endpoint->getURI(),
$endpoint->getParams(),
$endpoint->getBody(),
$endpoint->getOptions()
);

return $this->transport->resultOrFuture($promise, $endpoint->getOptions());
if ($this->isAsync) {
return $this->transport->sendAsyncRequest($request);
}
return $this->transport->sendRequest($request);
}

}
24 changes: 24 additions & 0 deletions src/OpenSearch/Common/Exceptions/NoAsyncClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* OpenSearch PHP client
*
* @link https://github.com/opensearch-project/opensearch-php/
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
* the GNU Lesser General Public License, Version 2.1, at your option.
* See the LICENSE file in the project root for more information.
*/

namespace OpenSearch\Common\Exceptions;

class NoAsyncClientException extends \Exception implements OpenSearchException
{
}
7 changes: 7 additions & 0 deletions src/OpenSearch/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ private function checkUserParams(array $params)
}
}

/**
* Get the headers for the endpoint request.
*/
public function getHeaders(): array {
return $this->options['client']['headers'] ?? [];
}

/**
* @param array<string, mixed> $params Note: this is passed by-reference!
*/
Expand Down
Loading

0 comments on commit 2f4661d

Please sign in to comment.