Skip to content

Commit

Permalink
Add a new transport approach and BC
Browse files Browse the repository at this point in the history
Signed-off-by: Kim Pepper <[email protected]>
  • Loading branch information
kimpepper committed Nov 19, 2024
1 parent ce60d42 commit faf6f5c
Show file tree
Hide file tree
Showing 38 changed files with 2,781 additions and 289 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
"require": {
"php": "^8.0",
"ext-json": ">=1.3.7",
"php-http/async-client-implementation": "^1.0",
"ext-curl": "*",
"ezimuel/ringphp": "^1.2.2",
"php-http/discovery": "^1.20",
"php-http/guzzle7-adapter": "^1.0",
"psr/http-client": "^1.0",
"psr/http-client-implementation": "^1.0",
"psr/http-client-implementation": "*",
"psr/http-factory": "^1.1",
"psr/http-factory-implementation": "^2.4",
"psr/http-factory-implementation": "*",
"psr/http-message": "^2.0",
"psr/http-message-implementation": "^1.0",
"psr/log": "^1|^2|^3",
"psr/http-message-implementation": "*",
"psr/log": "^2|^3",
"symfony/yaml": "*"
},
"require-dev": {
Expand Down
43 changes: 43 additions & 0 deletions src/OpenSearch/Aws/SigningRequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace OpenSearch\Aws;

use Aws\Credentials\CredentialsInterface;
use Aws\Signature\SignatureInterface;
use OpenSearch\RequestFactory;
use OpenSearch\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;

/**
* A decorator request factory that signs requests using the provided AWS credentials and signer.
*/
class SigningRequestFactory implements RequestFactoryInterface
{
public function __construct(
protected RequestFactory $requestFactory,
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 request factory.'
);
}
}

/**
* {@inheritdoc}
*/
public function createRequest(
string $method,
string $uri,
array $params = [],
array|string|null $body = null,
array $headers = [],
): RequestInterface {
$request = $this->requestFactory->createRequest($method, $uri, $params, $body, $headers);
$request = $request->withHeader('x-amz-content-sha256', hash('sha256', (string) $request->getBody()));
return $this->signer->signRequest($request, $this->credentials);
}

}
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());
}
}
Loading

0 comments on commit faf6f5c

Please sign in to comment.