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

Make EndpointFactory and optional Client constructor param #315

Merged
merged 2 commits into from
Feb 27, 2025
Merged
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
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
Loading