Skip to content
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

Psr interfaces #233

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 21 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,28 @@
"ext-json": ">=1.3.7",
"ext-curl": "*",
"ezimuel/ringphp": "^1.2.2",
"psr/log": "^1|^2|^3",
"php-http/discovery": "^1.20",
"psr/http-client": "^1.0",
"psr/http-client-implementation": "*",
"psr/http-factory": "^1.1",
"psr/http-factory-implementation": "*",
"psr/http-message": "^2.0",
"psr/http-message-implementation": "*",
"psr/log": "^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 +64,10 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": true
}
},
"scripts": {
"php-cs": [
Expand Down
57 changes: 49 additions & 8 deletions samples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,58 @@
* 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),
]
]);
$guzzleHttpFactory = new \GuzzleHttp\Psr7\HttpFactory();
$transport = (new OpenSearch\TransportFactory())
->setHttpClient($guzzleClient)
->setPsrRequestFactory($guzzleHttpFactory)
->setStreamFactory($guzzleHttpFactory)
->setUriFactory($guzzleHttpFactory)
->create();

$endpointFactory = new \OpenSearch\EndpointFactory();
$client = new Client($transport, $endpointFactory, []);

$info = $client->info();

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

// Symfony example

$symfonyPsr18Client = (new \Symfony\Component\HttpClient\Psr18Client())->withOptions([
'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',
],
]);


$transport = (new OpenSearch\TransportFactory())
->setHttpClient($symfonyPsr18Client)
->setPsrRequestFactory($symfonyPsr18Client)
->setStreamFactory($symfonyPsr18Client)
->setUriFactory($symfonyPsr18Client)
->create();

$client = new Client($transport, $endpointFactory, []);

$info = $client->info();
34 changes: 34 additions & 0 deletions src/OpenSearch/Aws/SigningClientDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace OpenSearch\Aws;

use Aws\Credentials\CredentialsInterface;
use Aws\Signature\SignatureInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* A decorator client that signs requests using the provided AWS credentials and signer.
*/
class SigningClientDecorator implements ClientInterface
{
public function __construct(
protected ClientInterface $inner,
protected CredentialsInterface $credentials,
protected SignatureInterface $signer,
) {
if (!class_exists(SignatureInterface::class)) {
throw new \RuntimeException(
'The AWS SDK for PHP must be installed in order to use the AWS signing client.'
);
}
}

public function sendRequest(RequestInterface $request): ResponseInterface
{
$request = $request->withHeader('x-amz-content-sha256', hash('sha256', (string) $request->getBody()));
$request = $this->signer->signRequest($request, $this->credentials);
return $this->inner->sendRequest($request);
}
}
41 changes: 26 additions & 15 deletions src/OpenSearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class Client
*/
public $transport;

private TransportInterface $httpTransport;

/**
* @var array
*/
Expand Down Expand Up @@ -251,13 +253,19 @@ class Client
/**
* Client constructor
*
* @param Transport $transport
* @param \OpenSearch\TransportInterface|\OpenSearch\Transport $transport
* @param callable|EndpointFactoryInterface $endpointFactory
* @param NamespaceBuilderInterface[] $registeredNamespaces
*/
public function __construct(Transport $transport, callable|EndpointFactoryInterface $endpointFactory, array $registeredNamespaces)
public function __construct(TransportInterface|Transport $transport, callable|EndpointFactoryInterface $endpointFactory, array $registeredNamespaces)
{
$this->transport = $transport;
if (!$transport instanceof TransportInterface) {
@trigger_error('Passing an instance of \OpenSearch\Transport to ' . __METHOD__ . '() is deprecated in 2.3.2 and will be removed in 3.0.0. Pass an instance of \OpenSearch\TransportInterface instead.', E_USER_DEPRECATED);
$this->transport = $transport;
$this->httpTransport = new LegacyTransportWrapper($transport);
} else {
$this->httpTransport = $transport;
}
if (is_callable($endpointFactory)) {
@trigger_error('Passing a callable as the $endpointFactory param in ' . __METHOD__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0. Pass an instance of \OpenSearch\EndpointFactoryInterface instead.', E_USER_DEPRECATED);
$endpoints = $endpointFactory;
Expand Down Expand Up @@ -2032,33 +2040,36 @@ public function extractArgument(array &$params, string $arg)

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

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

return $this->transport->resultOrFuture($promise, $options);
return $this->httpTransport->sendRequest($method, $uri, $params, $body, $options['headers'] ?? []);
}

/**
* @return callable|array
* Sends a request for the given endpoint.
*
* @param \OpenSearch\Endpoints\AbstractEndpoint $endpoint
*
* @return array|string|null
*
* @throws \Exception
*/
private function performRequest(AbstractEndpoint $endpoint)
private function performRequest(AbstractEndpoint $endpoint): array|string|null
{
$promise = $this->transport->performRequest(
$options = $endpoint->getOptions();
return $this->httpTransport->sendRequest(
$endpoint->getMethod(),
$endpoint->getURI(),
$endpoint->getParams(),
$endpoint->getBody(),
$endpoint->getOptions()
$options['headers'] ?? []
);

return $this->transport->resultOrFuture($promise, $endpoint->getOptions());
}
}
5 changes: 5 additions & 0 deletions src/OpenSearch/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
use Psr\Log\NullLogger;
use ReflectionClass;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);

/**
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
class ClientBuilder
{
public const ALLOWED_METHODS_FROM_CONFIG = ['includePortInHostHeader'];
Expand Down
19 changes: 19 additions & 0 deletions src/OpenSearch/ClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace OpenSearch;

class ClientFactory
{
public function __construct(
protected TransportInterface $transport,
protected EndpointFactoryInterface $endpointFactory,
protected array $registeredNamespaces = [],
) {
}

public function createClient(): Client
{
return new Client($this->transport, $this->endpointFactory, $this->registeredNamespaces);
}

}
4 changes: 4 additions & 0 deletions src/OpenSearch/Common/EmptyLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0. Use Psr\Log\NullLogger instead', E_USER_DEPRECATED);

/**
* Class EmptyLogger
*
* Logger that doesn't do anything. Similar to Monolog's NullHandler,
* but avoids the overhead of partially loading Monolog
*
* @deprecated in 2.3.2 and will be removed in 3.0.0. Use Psr\Log\NullLogger instead.
*/
class EmptyLogger extends AbstractLogger implements LoggerInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

namespace OpenSearch\Common\Exceptions;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);

/**
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
class AuthenticationConfigException extends \RuntimeException implements OpenSearchException
{
}
4 changes: 4 additions & 0 deletions src/OpenSearch/Common/Exceptions/BadMethodCallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@

namespace OpenSearch\Common\Exceptions;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);

/**
* BadMethodCallException
*
* Denote problems with a method call (e.g. incorrect number of arguments)
*
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
class BadMethodCallException extends \BadMethodCallException implements OpenSearchException
{
Expand Down
5 changes: 5 additions & 0 deletions src/OpenSearch/Common/Exceptions/BadRequest400Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

namespace OpenSearch\Common\Exceptions;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);

/**
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
class BadRequest400Exception extends \Exception implements OpenSearchException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

namespace OpenSearch\Common\Exceptions;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);

/**
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
class ClientErrorResponseException extends TransportException implements OpenSearchException
{
}
5 changes: 5 additions & 0 deletions src/OpenSearch/Common/Exceptions/Conflict409Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

namespace OpenSearch\Common\Exceptions;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);

/**
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
class Conflict409Exception extends \Exception implements OpenSearchException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
use OpenSearch\Common\Exceptions\OpenSearchException;
use OpenSearch\Common\Exceptions\TransportException;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);

/**
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
class CouldNotConnectToHost extends TransportException implements OpenSearchException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
use OpenSearch\Common\Exceptions\OpenSearchException;
use OpenSearch\Common\Exceptions\TransportException;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);

/**
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
class CouldNotResolveHostException extends TransportException implements OpenSearchException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
use OpenSearch\Common\Exceptions\OpenSearchException;
use OpenSearch\Common\Exceptions\TransportException;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);

/**
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
class OperationTimeoutException extends TransportException implements OpenSearchException
{
}
5 changes: 5 additions & 0 deletions src/OpenSearch/Common/Exceptions/Forbidden403Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

namespace OpenSearch\Common\Exceptions;

@trigger_error(__CLASS__ . ' is deprecated in 2.3.2 and will be removed in 3.0.0.', E_USER_DEPRECATED);

/**
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
class Forbidden403Exception extends \Exception implements OpenSearchException
{
}
Loading
Loading