Skip to content

Commit

Permalink
Generate Client
Browse files Browse the repository at this point in the history
  • Loading branch information
kimpepper committed Jan 17, 2025
1 parent 23bfd34 commit fff8698
Showing 1 changed file with 4 additions and 122 deletions.
126 changes: 4 additions & 122 deletions src/OpenSearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@

namespace OpenSearch;

use OpenSearch\Common\Exceptions\BadRequest400Exception;
use OpenSearch\Common\Exceptions\Conflict409Exception;
use OpenSearch\Common\Exceptions\Forbidden403Exception;
use OpenSearch\Common\Exceptions\Missing404Exception;
use OpenSearch\Common\Exceptions\NoDocumentsToGetException;
use OpenSearch\Common\Exceptions\NoShardAvailableException;
use OpenSearch\Common\Exceptions\RequestTimeout408Exception;
use OpenSearch\Common\Exceptions\RoutingMissingException;
use OpenSearch\Common\Exceptions\ScriptLangNotSupportedException;
use OpenSearch\Common\Exceptions\ServerErrorResponseException;
use OpenSearch\Common\Exceptions\Unauthorized401Exception;
use OpenSearch\Endpoints\AbstractEndpoint;
use OpenSearch\Namespaces\BooleanRequestWrapper;
use OpenSearch\Namespaces\NamespaceBuilderInterface;
Expand Down Expand Up @@ -106,11 +95,6 @@ class Client
*/
protected $registeredNamespaces = [];

/**
* @deprecated in 2.3.2 and will be removed in 3.0.0.
*/
private bool $throwExceptions = false;

/**
* @var AsyncSearchNamespace
*/
Expand Down Expand Up @@ -284,13 +268,11 @@ class Client
* @param TransportInterface|Transport $transport
* @param callable|EndpointFactoryInterface $endpointFactory
* @param NamespaceBuilderInterface[] $registeredNamespaces
* @param bool $throwExceptions
*/
public function __construct(
TransportInterface|Transport $transport,
callable|EndpointFactoryInterface $endpointFactory,
array $registeredNamespaces,
bool $throwExceptions = false,
) {
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);
Expand All @@ -311,13 +293,6 @@ public function __construct(
}
$this->endpoints = $endpoints;
$this->endpointFactory = $endpointFactory;
if ($throwExceptions === true) {
@trigger_error(
'The $throwExceptions parameter is deprecated in 2.4.0 and will be removed in 3.0.0. Check the response \'status_code\' instead',
E_USER_DEPRECATED
);
$this->throwExceptions = true;
}
$this->asyncSearch = new AsyncSearchNamespace($transport, $this->endpointFactory);
$this->asynchronousSearch = new AsynchronousSearchNamespace($transport, $this->endpointFactory);
$this->cat = new CatNamespace($transport, $this->endpointFactory);
Expand Down Expand Up @@ -2098,7 +2073,7 @@ public function extractArgument(array &$params, string $arg)
* Send a raw request to the cluster.
*
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \OpenSearch\Common\Exceptions\OpenSearchException
* @throws \OpenSearch\Exception\HttpExceptionInterface
*/
public function request(
string $method,
Expand All @@ -2109,117 +2084,24 @@ public function request(
$body = $attributes['body'] ?? null;
$options = $attributes['options'] ?? [];

$response = $this->httpTransport->sendRequest($method, $uri, $params, $body, $options['headers'] ?? []);

// @todo: Remove this in the next major release.
// Throw legacy exceptions.
if ($this->throwExceptions) {
if (isset($response['status']) && $response['status'] >= 400) {
$this->throwLegacyException($response);
}
}

return $response;
return $this->httpTransport->sendRequest($method, $uri, $params, $body, $options['headers'] ?? []);
}

/**
* Send a request for an endpoint.
*
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \OpenSearch\Common\Exceptions\OpenSearchException
* @throws \OpenSearch\Exception\HttpExceptionInterface
*/
private function performRequest(AbstractEndpoint $endpoint): array|string|null
{
$response = $this->httpTransport->sendRequest(
return $this->httpTransport->sendRequest(
$endpoint->getMethod(),
$endpoint->getURI(),
$endpoint->getParams(),
$endpoint->getBody(),
$endpoint->getOptions()
);

// @todo: Remove this in the next major release.
// Throw legacy exceptions.
if ($this->throwExceptions) {
if (isset($response['status']) && $response['status'] >= 400) {
$this->throwLegacyException($response);
}
}

return $response;
}

/**
* Throw legacy exceptions.
*
* @param array<string,mixed> $response
*
* @throws \OpenSearch\Common\Exceptions\OpenSearchException
*/
private function throwLegacyException(array $response): void
{
if ($response['status'] >= 400 && $response['status'] < 500) {
$this->throwLegacyClientException($response);
}
if ($response['status'] >= 500) {
$this->throwLegacyServerException($response);
}
}

/**
* Throw legacy client exceptions based on status code.
*
* @throws \OpenSearch\Common\Exceptions\OpenSearchException
*/
private function throwLegacyClientException($response): void
{
$statusCode = $response['status_code'];
$responseBody = $this->convertBodyToString($response['body'], $statusCode);
throw match ($statusCode) {
401 => new Unauthorized401Exception($responseBody, $statusCode),
403 => new Forbidden403Exception($responseBody, $statusCode),
404 => new Missing404Exception($responseBody, $statusCode),
409 => new Conflict409Exception($responseBody, $statusCode),
400 => (str_contains($responseBody, 'script_lang not supported'))
? new ScriptLangNotSupportedException($responseBody . $statusCode)
: new BadRequest400Exception($responseBody, $statusCode),
408 => new RequestTimeout408Exception($responseBody, $statusCode),
default => new BadRequest400Exception($responseBody, $statusCode),
};
}

/**
* Throw legacy server exceptions based on status code.
*
* @throws \OpenSearch\Common\Exceptions\OpenSearchException
*/
private function throwLegacyServerException($response): void
{
$statusCode = $response['status_code'];
$error = $response['body']['error'] ?? [];
$reason = $error['reason'] ?? 'undefined reason';
$type = $error['type'] ?? 'undefined type';
$errorMessage = "$type: $reason";
$responseBody = $this->convertBodyToString($response['body'], $statusCode);

$exception = new ServerErrorResponseException($responseBody, $statusCode);
if ($statusCode === 500) {
if (str_contains($responseBody, "RoutingMissingException")) {
$exception = new RoutingMissingException($errorMessage, $statusCode);
} elseif (preg_match('/ActionRequestValidationException.+ no documents to get/', $responseBody) === 1) {
$exception = new NoDocumentsToGetException($errorMessage, $statusCode);
} elseif (str_contains($responseBody, 'NoShardAvailableActionException')) {
$exception = new NoShardAvailableException($errorMessage, $statusCode);
}
}
throw $exception;
}

private function convertBodyToString(mixed $body, int $statusCode): string
{
return empty($body)
? "Unknown $statusCode error from OpenSearch"
: (is_string($body) ? $body : json_encode($body));
}

}

0 comments on commit fff8698

Please sign in to comment.