3 namespace seekat\API\Call
;
5 use http\Client\Response
;
8 use seekat\Exception\RequestException
;
14 function __construct(API
$api) {
18 function __invoke(Response
$response) : API
{
19 $hit = $response->getHeader("X-Cache-Time") ?
"cached" : "enqueued";
20 $this->api
->getLogger()->info("$hit -> response", [
21 "url" => (string) $this->api
->getUrl(),
22 "info" => $response->getInfo(),
25 $links = $this->checkResponseMeta($response);
26 $type = $this->checkResponseType($response);
29 $data = $type->parseBody($response->getBody());
30 } catch (\Exception
$e) {
31 $this->api
->getLogger()->error("response -> error: ".$e->getMessage(), [
32 "url" => (string) $this->api
->getUrl(),
38 $this->api
= $this->api
->with(compact("type", "data", "links"));
44 * @param Response $response
45 * @return null|API\Links
46 * @throws RequestException
48 private function checkResponseMeta(Response
$response) {
49 if ($response->getResponseCode() >= 400) {
50 $e = new RequestException($response);
52 $this->api
->getLogger()->critical("response -> error: ".$e->getMessage(), [
53 "url" => (string) $this->api
->getUrl(),
59 if (($link = $response->getHeader("Link", Header
::class))) {
60 $links = new API\
Links($link);
68 private function checkResponseType(Response
$response) {
69 if (!($type = $response->getHeader("Content-Type", Header
::class))) {
70 $e = new RequestException($response);
72 $this->api
->getLogger()->error("response -> error: Empty Content-Type -> ".$e->getMessage(), [
73 "url" => (string) $this->api
->getUrl(),
79 return new API\
ContentType($type);