From a51ad68555d184bad4822d46b77288295e81584d Mon Sep 17 00:00:00 2001 From: nixx Date: Wed, 27 Jan 2021 16:47:58 +0300 Subject: [PATCH 1/2] support new amphp http client Improve error handling Remove unnecessary changes --- composer.json | 2 +- src/Client.php | 23 ++++++++++++----------- src/Error.php | 17 +++++++++++++---- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 9db80da..0d42ecd 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "require": { "php": "~7.4.0|~8.0.0", "ext-json": "*", - "amphp/artax": "^3.0", + "amphp/http-client": "^4.5", "amphp/amp": "^2.1" }, "autoload": { diff --git a/src/Client.php b/src/Client.php index 672a640..ed49f7e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,10 +4,10 @@ namespace Webgriffe\AmpElasticsearch; -use Amp\Artax\Client as HttpClient; -use Amp\Artax\DefaultClient; -use Amp\Artax\Request; -use Amp\Artax\Response; +use Amp\Http\Client\HttpClient; +use Amp\Http\Client\HttpClientBuilder; +use Amp\Http\Client\Request; +use Amp\Http\Client\Response; use function Amp\call; use Amp\Promise; @@ -24,7 +24,7 @@ class Client public function __construct(string $baseUri, HttpClient $httpClient = null) { - $this->httpClient = new DefaultClient(); + $this->httpClient = HttpClientBuilder::buildDefault(); if ($httpClient) { $this->httpClient = $httpClient; } @@ -165,7 +165,7 @@ public function refresh(string $indexOrIndices = null, array $options = []): Pro return $this->doJsonRequest($method, $uri); } - public function search(array $query, string $indexOrIndices = null, array $options = []): Promise + public function search(array $query, ?string $indexOrIndices = null, array $options = []): Promise { $method = 'POST'; $uri = [$this->baseUri]; @@ -214,11 +214,12 @@ public function bulk(array $body, string $index = null, array $options = []): Pr private function createJsonRequest(string $method, string $uri, string $body = null): Request { - $request = (new Request($uri, $method)) - ->withHeader('Content-Type', 'application/json') - ->withHeader('Accept', 'application/json'); + $request = new Request($uri, $method); + $request->setHeader('Content-Type', 'application/json'); + $request->setHeader('Accept', 'application/json'); + if ($body) { - $request = $request->withBody($body); + $request->setBody($body); } return $request; } @@ -228,7 +229,7 @@ private function doRequest(Request $request): Promise return call(function () use ($request) { /** @var Response $response */ $response = yield $this->httpClient->request($request); - $body = yield $response->getBody(); + $body = yield $response->getBody()->buffer(); $statusClass = (int) ($response->getStatus() / 100); if ($statusClass !== 2) { throw new Error($body, $response->getStatus()); diff --git a/src/Error.php b/src/Error.php index 3d70bb7..a0e89c5 100644 --- a/src/Error.php +++ b/src/Error.php @@ -4,6 +4,8 @@ namespace Webgriffe\AmpElasticsearch; +use JsonException; + class Error extends \RuntimeException { /** @@ -13,16 +15,23 @@ class Error extends \RuntimeException /** * Error constructor. - * @param string $errorJson + * @param string|null $errorJson * @param int $code */ public function __construct(?string $errorJson, int $code) { $message = 'An error occurred. Response code: ' . $code; if ($errorJson) { - $this->data = (array)json_decode($errorJson, true); - $prettyErrorJson = json_encode(json_decode($errorJson, false), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); - $message .= PHP_EOL . $prettyErrorJson; + try { + $this->data = (array)json_decode($errorJson, true, 512, JSON_THROW_ON_ERROR); + $prettyErrorJson = json_encode( + json_decode($errorJson, false, 512, JSON_THROW_ON_ERROR), + JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES + ); + $message .= PHP_EOL . $prettyErrorJson; + } catch (JsonException $jsonException) { + // Do not fail because of JsonException + } } parent::__construct($message, $code); } From 834b818a250f6de924964c143db5b45caa476571 Mon Sep 17 00:00:00 2001 From: Manuele Menozzi Date: Fri, 30 Sep 2022 17:42:23 +0200 Subject: [PATCH 2/2] Remove the ability to use an externally provided http client --- src/Client.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Client.php b/src/Client.php index ed49f7e..54cdd9b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -22,12 +22,9 @@ class Client */ private $httpClient; - public function __construct(string $baseUri, HttpClient $httpClient = null) + public function __construct(string $baseUri) { $this->httpClient = HttpClientBuilder::buildDefault(); - if ($httpClient) { - $this->httpClient = $httpClient; - } $this->baseUri = rtrim($baseUri, '/'); }