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

Fix async support #300

Merged
merged 1 commit into from
Feb 19, 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
2 changes: 1 addition & 1 deletion src/OpenSearch/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function sendRequest(
array $params = [],
mixed $body = null,
array $headers = [],
): array|string|null {
): iterable|string|null {
$request = $this->createRequest($method, $uri, $params, $body, $headers);
$response = $this->client->sendRequest($request);
$statusCode = $response->getStatusCode();
Expand Down
14 changes: 6 additions & 8 deletions src/OpenSearch/LegacyTransportWrapper.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace OpenSearch;
declare(strict_types=1);

use GuzzleHttp\Ring\Future\FutureArrayInterface;
namespace OpenSearch;

// @phpstan-ignore classConstant.deprecatedClass
@trigger_error(LegacyTransportWrapper::class . ' is deprecated in 2.4.0 and will be removed in 3.0.0.', E_USER_DEPRECATED);
Expand All @@ -28,13 +28,11 @@ public function sendRequest(
array $params = [],
mixed $body = null,
array $headers = [],
): array|string|null {
): iterable|string|null {
$promise = $this->transport->performRequest($method, $uri, $params, $body);
$futureArray = $this->transport->resultOrFuture($promise);
if ($futureArray instanceof FutureArrayInterface) {
return $futureArray->wait();
}
return $futureArray;
// Provide legacy support for options.
$options = $headers;
return $this->transport->resultOrFuture($promise, $options);
}

}
2 changes: 1 addition & 1 deletion src/OpenSearch/TransportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public function sendRequest(
array $params = [],
string|array|null $body = null,
array $headers = [],
): array|string|null;
): iterable|string|null;

}
62 changes: 62 additions & 0 deletions tests/LegacyTransportWrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace OpenSearch\Tests;

use GuzzleHttp\Ring\Future\FutureArray;
use OpenSearch\ConnectionPool\AbstractConnectionPool;
use OpenSearch\Connections\Connection;
use OpenSearch\LegacyTransportWrapper;
use OpenSearch\Transport;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use React\Promise\Deferred;

/**
* Tests for the LegacyTransportWrapper class.
*
* @coversDefaultClass \OpenSearch\LegacyTransportWrapper
* @group legacy
* @deprecated in 2.4.2 and will be removed in 3.0.0.
*/
class LegacyTransportWrapperTest extends TestCase
{
private Connection&MockObject $connection;

private AbstractConnectionPool&MockObject $connectionPool;

private MockObject&LoggerInterface $logger;

public function setUp(): void
{
$this->logger = $this->createMock(LoggerInterface::class);
$this->connectionPool = $this->createMock(AbstractConnectionPool::class);
$this->connection = $this->createMock(Connection::class);
}

public function testSuccess(): void
{
$deferred = new Deferred();
$deferred->resolve(['foo' => 'bar']);
$future = new FutureArray($deferred->promise());

$this->connection->method('performRequest')
->willReturn($future);

$this->connectionPool->method('nextConnection')
->willReturn($this->connection);

$transport = new Transport(1, $this->connectionPool, $this->logger);

$wrapper = new LegacyTransportWrapper($transport);

$response = $wrapper->sendRequest('GET', 'http://localhost:9200', [], null, []);

$this->assertIsIterable($response);

$this->assertEquals(['foo' => 'bar'], $response);
}

}
Loading