Skip to content

Commit

Permalink
Make EndpointFactory and optional Client constructor param (#315)
Browse files Browse the repository at this point in the history
* Make EndpointFactory and optional Client constructor param

Signed-off-by: Kim Pepper <kim@pepper.id.au>

* Adds test

Signed-off-by: Kim Pepper <kim@pepper.id.au>

---------

Signed-off-by: Kim Pepper <kim@pepper.id.au>
  • Loading branch information
kimpepper authored Feb 27, 2025
1 parent 88235f6 commit 8263aac
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Added
### Changed
- Updated Client constructor to make EndpointFactory and optional parameter.
### Deprecated
### Removed
### Fixed
Expand Down
9 changes: 7 additions & 2 deletions src/OpenSearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,14 @@ class Client
* Client constructor
*
* @param TransportInterface|Transport $transport
* @param callable|EndpointFactoryInterface $endpointFactory
* @param callable|EndpointFactoryInterface|null $endpointFactory
* @param NamespaceBuilderInterface[] $registeredNamespaces
*
* @phpstan-ignore parameter.deprecatedClass
*/
public function __construct(
TransportInterface|Transport $transport,
callable|EndpointFactoryInterface $endpointFactory,
callable|EndpointFactoryInterface|null $endpointFactory = null,
array $registeredNamespaces = [],
) {
if (!$transport instanceof TransportInterface) {
Expand All @@ -336,17 +336,22 @@ public function __construct(
} else {
$this->httpTransport = $transport;
}

if (is_callable($endpointFactory)) {
@trigger_error('Passing a callable as the $endpointFactory param in ' . __METHOD__ . ' is deprecated in 2.4.0 and will be removed in 3.0.0. Pass an instance of \OpenSearch\EndpointFactoryInterface instead.', E_USER_DEPRECATED);
$endpoints = $endpointFactory;
// @phpstan-ignore new.deprecated
$endpointFactory = new LegacyEndpointFactory($endpointFactory);
} else {
if ($endpointFactory === null) {
$endpointFactory = new EndpointFactory();
}
$endpoints = function ($c) use ($endpointFactory) {
@trigger_error('The $endpoints property is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED);
return $endpointFactory->getEndpoint('OpenSearch\\Endpoints\\' . $c);
};
}

// @phpstan-ignore property.deprecated
$this->endpoints = $endpoints;
$this->endpointFactory = $endpointFactory;
Expand Down
22 changes: 22 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,26 @@ public function testSendRawRequest(): void
$this->assertEquals(['bang'], $response);
}

/**
* @covers ::request
*/
public function testOptionalEndpointFactory(): void
{

$this->transport->expects($this->once())
->method('sendRequest')
->with('GET', '/', ['foo' => 'bar'], 'whizz')
->willReturn(['bang']);

$this->client = new Client($this->transport);

$response = $this->client->request('GET', '/', [
'params' => ['foo' => 'bar'],
'body' => 'whizz',
]);

$this->assertEquals(['bang'], $response);

}

}
9 changes: 7 additions & 2 deletions util/template/client-class
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ class Client
* Client constructor
*
* @param TransportInterface|Transport $transport
* @param callable|EndpointFactoryInterface $endpointFactory
* @param callable|EndpointFactoryInterface|null $endpointFactory
* @param NamespaceBuilderInterface[] $registeredNamespaces
*
* @phpstan-ignore parameter.deprecatedClass
*/
public function __construct(
TransportInterface|Transport $transport,
callable|EndpointFactoryInterface $endpointFactory,
callable|EndpointFactoryInterface|null $endpointFactory = null,
array $registeredNamespaces = [],
) {
if (!$transport instanceof TransportInterface) {
Expand All @@ -89,17 +89,22 @@ class Client
} else {
$this->httpTransport = $transport;
}

if (is_callable($endpointFactory)) {
@trigger_error('Passing a callable as the $endpointFactory param in ' . __METHOD__ . ' is deprecated in 2.4.0 and will be removed in 3.0.0. Pass an instance of \OpenSearch\EndpointFactoryInterface instead.', E_USER_DEPRECATED);
$endpoints = $endpointFactory;
// @phpstan-ignore new.deprecated
$endpointFactory = new LegacyEndpointFactory($endpointFactory);
} else {
if ($endpointFactory === null) {
$endpointFactory = new EndpointFactory();
}
$endpoints = function ($c) use ($endpointFactory) {
@trigger_error('The $endpoints property is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED);
return $endpointFactory->getEndpoint('OpenSearch\\Endpoints\\' . $c);
};
}

// @phpstan-ignore property.deprecated
$this->endpoints = $endpoints;
$this->endpointFactory = $endpointFactory;
Expand Down

0 comments on commit 8263aac

Please sign in to comment.