Skip to content

Commit

Permalink
feat(client): send raw json request
Browse files Browse the repository at this point in the history
  • Loading branch information
imdhemy committed Mar 2, 2024
1 parent bf9b981 commit a99d97c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 24 deletions.
38 changes: 25 additions & 13 deletions src/OpenSearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,26 @@
namespace OpenSearch;

use OpenSearch\Common\Exceptions\BadMethodCallException;
use OpenSearch\Common\Exceptions\InvalidArgumentException;
use OpenSearch\Common\Exceptions\NoNodesAvailableException;
use OpenSearch\Common\Exceptions\BadRequest400Exception;
use OpenSearch\Common\Exceptions\Missing404Exception;
use OpenSearch\Common\Exceptions\TransportException;
use OpenSearch\Endpoints\AbstractEndpoint;
use OpenSearch\Namespaces\AbstractNamespace;
use OpenSearch\Namespaces\MachineLearningNamespace;
use OpenSearch\Namespaces\NamespaceBuilderInterface;
use OpenSearch\Namespaces\AsyncSearchNamespace;
use OpenSearch\Namespaces\BooleanRequestWrapper;
use OpenSearch\Namespaces\CatNamespace;
use OpenSearch\Namespaces\ClusterNamespace;
use OpenSearch\Namespaces\DanglingIndicesNamespace;
use OpenSearch\Namespaces\DataFrameTransformDeprecatedNamespace;
use OpenSearch\Namespaces\IndicesNamespace;
use OpenSearch\Namespaces\IngestNamespace;
use OpenSearch\Namespaces\MachineLearningNamespace;
use OpenSearch\Namespaces\MonitoringNamespace;
use OpenSearch\Namespaces\NamespaceBuilderInterface;
use OpenSearch\Namespaces\NodesNamespace;
use OpenSearch\Namespaces\SearchableSnapshotsNamespace;
use OpenSearch\Namespaces\SecurityNamespace;
use OpenSearch\Namespaces\SnapshotNamespace;
use OpenSearch\Namespaces\SqlNamespace;
use OpenSearch\Namespaces\TasksNamespace;
use OpenSearch\Namespaces\AsyncSearchNamespace;
use OpenSearch\Namespaces\DataFrameTransformDeprecatedNamespace;
use OpenSearch\Namespaces\MonitoringNamespace;
use OpenSearch\Namespaces\SearchableSnapshotsNamespace;
use OpenSearch\Namespaces\SslNamespace;
use OpenSearch\Namespaces\TasksNamespace;

/**
* Class Client
Expand Down Expand Up @@ -1357,7 +1352,7 @@ public function sql(): SqlNamespace

public function ml(): MachineLearningNamespace
{
return $this->ml;
return $this->ml;

Check warning on line 1355 in src/OpenSearch/Client.php

View check run for this annotation

Codecov / codecov/patch

src/OpenSearch/Client.php#L1355

Added line #L1355 was not covered by tests
}

/**
Expand Down Expand Up @@ -1391,6 +1386,23 @@ public function extractArgument(array &$params, string $arg)
}
}

/**
* Sends a raw request to the cluster
* @return callable|array
* @throws NoNodesAvailableException
*/
public function request(
string $method,
string $uri,
array $params = [],
$body = null,
array $options = []
) {
$promise = $this->transport->performRequest($method, $uri, $params, $body, $options);

return $this->transport->resultOrFuture($promise, $options);
}

/**
* @return callable|array
*/
Expand Down
89 changes: 78 additions & 11 deletions tests/ClientIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use OpenSearch\Client;
use OpenSearch\ClientBuilder;
use OpenSearch\Common\Exceptions\BadRequest400Exception;
use OpenSearch\Common\Exceptions\OpenSearchException;
use OpenSearch\Common\Exceptions\Missing404Exception;
use OpenSearch\Tests\ClientBuilder\ArrayLogger;
use Psr\Log\LogLevel;
Expand All @@ -33,7 +32,7 @@
* Class ClientTest
*
* @subpackage Tests
* @group Integration
* @group Integration
*/
class ClientIntegrationTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -88,7 +87,7 @@ public function testLogRequestFailHasWarning()
try {
$result = $client->get([
'index' => 'foo',
'id' => 'bar'
'id' => 'bar',
]);
} catch (Missing404Exception $e) {
$this->assertNotEmpty($this->getLevelOutput(LogLevel::WARNING, $this->logger->output));
Expand All @@ -103,8 +102,8 @@ public function testIndexCannotBeEmptyStringForDelete()

$client->delete(
[
'index' => '',
'id' => 'test'
'index' => '',
'id' => 'test',
]
);
}
Expand All @@ -117,8 +116,8 @@ public function testIdCannotBeEmptyStringForDelete()

$client->delete(
[
'index' => 'test',
'id' => ''
'index' => 'test',
'id' => '',
]
);
}
Expand All @@ -131,8 +130,8 @@ public function testIndexCannotBeArrayOfEmptyStringsForDelete()

$client->delete(
[
'index' => ['', '', ''],
'id' => 'test'
'index' => ['', '', ''],
'id' => 'test',
]
);
}
Expand All @@ -145,19 +144,87 @@ public function testIndexCannotBeArrayOfNullsForDelete()

$client->delete(
[
'index' => [null, null, null],
'id' => 'test'
'index' => [null, null, null],
'id' => 'test',
]
);
}

/** @test */
public function sendRawGetRequest(): void
{
$client = $this->getClient();

$response = $client->request('GET', '/');

$this->assertIsArray($response);
$expectedKeys = ['name', 'cluster_name', 'cluster_uuid', 'version', 'tagline'];
foreach ($expectedKeys as $key) {
$this->assertArrayHasKey($key, $response);
}
}

/** @test */
public function sendRawPostRequest(): void
{
$client = $this->getClient();
$index = 'test_index_' . time();
$client->indices()->create(['index' => $index]);

$response = $client->request('POST', "/$index/_doc", [], ['foo' => 'bar']);

$this->assertIsArray($response);
$this->assertArrayHasKey('_index', $response);
$this->assertEquals($index, $response['_index']);
$this->assertArrayHasKey('result', $response);
$this->assertEquals('created', $response['result']);

$client->indices()->delete(['index' => $index]);
}

/** @test */
public function sendRawPutRequest(): void
{
$client = $this->getClient();
$index = 'test_index_' . time();
$client->indices()->create(['index' => $index]);

$response = $client->request('PUT', "/$index/_settings", [], ['index' => ['number_of_replicas' => 2]]);

$this->assertIsArray($response);
$this->assertArrayHasKey('acknowledged', $response);
$this->assertTrue($response['acknowledged']);

$client->indices()->delete(['index' => $index]);
}

/** @test */
public function sendRawDeleteRequest(): void
{
$client = $this->getClient();
$index = 'test_index_' . time();
$client->indices()->create(['index' => $index]);
$client->index(['index' => $index, 'id' => 1, 'body' => ['foo' => 'bar']]);

$response = $client->request('DELETE', "/$index/_doc/1");

$this->assertIsArray($response);
$this->assertArrayHasKey('_index', $response);
$this->assertEquals($index, $response['_index']);
$this->assertArrayHasKey('result', $response);
$this->assertEquals('deleted', $response['result']);

$client->indices()->delete(['index' => $index]);
}

private function getLevelOutput(string $level, array $output): string
{
foreach ($output as $out) {
if (false !== strpos($out, $level)) {
return $out;
}
}

return '';
}
}

0 comments on commit a99d97c

Please sign in to comment.