Skip to content

Commit

Permalink
Merge pull request #4 from webgriffe/new-http-client
Browse files Browse the repository at this point in the history
Support new amphp http client
  • Loading branch information
mmenozzi authored Sep 30, 2022
2 parents 8ad1692 + 834b818 commit 7e2b8d3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
28 changes: 13 additions & 15 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,12 +22,9 @@ class Client
*/
private $httpClient;

public function __construct(string $baseUri, HttpClient $httpClient = null)
public function __construct(string $baseUri)
{
$this->httpClient = new DefaultClient();
if ($httpClient) {
$this->httpClient = $httpClient;
}
$this->httpClient = HttpClientBuilder::buildDefault();
$this->baseUri = rtrim($baseUri, '/');
}

Expand Down Expand Up @@ -165,7 +162,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];
Expand Down Expand Up @@ -214,11 +211,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;
}
Expand All @@ -228,7 +226,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());
Expand Down
17 changes: 13 additions & 4 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Webgriffe\AmpElasticsearch;

use JsonException;

class Error extends \RuntimeException
{
/**
Expand All @@ -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);
}
Expand Down

0 comments on commit 7e2b8d3

Please sign in to comment.