1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * Copyright OpenSearch Contributors
7: * SPDX-License-Identifier: Apache-2.0
8: *
9: * OpenSearch PHP client
10: *
11: * @link https://github.com/opensearch-project/opensearch-php/
12: * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
13: * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
14: * @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
15: *
16: * Licensed to Elasticsearch B.V under one or more agreements.
17: * Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
18: * the GNU Lesser General Public License, Version 2.1, at your option.
19: * See the LICENSE file in the project root for more information.
20: */
21:
22: namespace OpenSearch\Namespaces;
23:
24: use GuzzleHttp\Ring\Future\FutureArrayInterface;
25: use OpenSearch\Common\Exceptions\Missing404Exception;
26: use OpenSearch\Common\Exceptions\RoutingMissingException;
27: use OpenSearch\Endpoints\AbstractEndpoint;
28: use OpenSearch\Exception\NotFoundHttpException;
29: use OpenSearch\Transport;
30: use OpenSearch\TransportInterface;
31:
32: abstract class BooleanRequestWrapper
33: {
34: /**
35: * Send a request with a boolean response.
36: *
37: * @return bool
38: * Returns FALSE for a 404 error, otherwise TRUE.
39: *
40: * @throws \Psr\Http\Client\ClientExceptionInterface
41: */
42: public static function sendRequest(AbstractEndpoint $endpoint, TransportInterface $transport): bool
43: {
44: try {
45: $transport->sendRequest(
46: $endpoint->getMethod(),
47: $endpoint->getURI(),
48: $endpoint->getParams(),
49: $endpoint->getBody(),
50: $endpoint->getOptions()
51: );
52:
53: } catch (NotFoundHttpException|RoutingMissingException $e) {
54: // Return false for 404 errors.
55: return false;
56: }
57: return true;
58: }
59:
60: /**
61: * Perform Request
62: *
63: * @throws Missing404Exception
64: * @throws RoutingMissingException
65: *
66: * @deprecated in 2.4.0 and will be removed in 3.0.0. Use \OpenSearch\Namespaces\BooleanRequestWrapper::sendRequest() instead.
67: */
68: public static function performRequest(AbstractEndpoint $endpoint, Transport $transport)
69: {
70: @trigger_error(
71: __METHOD__ . '() is deprecated in 2.4.0 and will be removed in 3.0.0. Use \OpenSearch\Namespaces\BooleanRequestWrapper::sendRequest() instead.'
72: );
73: try {
74: $response = $transport->performRequest(
75: $endpoint->getMethod(),
76: $endpoint->getURI(),
77: $endpoint->getParams(),
78: $endpoint->getBody(),
79: $endpoint->getOptions()
80: );
81:
82: $response = $transport->resultOrFuture($response, $endpoint->getOptions());
83: if (!($response instanceof FutureArrayInterface)) {
84: if ($response['status'] === 200) {
85: return true;
86: } else {
87: return false;
88: }
89: } else {
90: // async mode, can't easily resolve this...punt to user
91: return $response;
92: }
93: } catch (Missing404Exception $exception) {
94: return false;
95: } catch (RoutingMissingException $exception) {
96: return false;
97: }
98: }
99: }
100: