From 75ce0d658dd707d510e159066cfc924d91b2ccf4 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Fri, 17 Jan 2025 11:31:49 +1100 Subject: [PATCH] Generate Client Signed-off-by: Kim Pepper --- src/OpenSearch/Client.php | 126 ++------------------------------------ 1 file changed, 4 insertions(+), 122 deletions(-) diff --git a/src/OpenSearch/Client.php b/src/OpenSearch/Client.php index 45c1fc699..e97d6a28a 100644 --- a/src/OpenSearch/Client.php +++ b/src/OpenSearch/Client.php @@ -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; @@ -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 */ @@ -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); @@ -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); @@ -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, @@ -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 $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)); } }