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);
27 $data = $this->checkResponseBody($response, $type);
29 return $this->api
= $this->api
->with(compact("type", "data", "links"));
33 * @param Response $response
34 * @return null|API\Links
35 * @throws RequestException
37 private function checkResponseMeta(Response
$response) {
38 if ($response->getResponseCode() >= 400) {
39 $e = new RequestException($response);
41 $this->api
->getLogger()->critical("response -> error: ".$e->getMessage(), [
42 "url" => (string) $this->api
->getUrl(),
48 if (!($link = $response->getHeader("Link", Header
::class))) {
52 return new API\
Links($link);
55 private function checkResponseType(Response
$response) {
56 if (!($type = $response->getHeader("Content-Type", Header
::class))) {
57 $e = new RequestException($response);
59 $this->api
->getLogger()->error("response -> error: Empty Content-Type -> ".$e->getMessage(), [
60 "url" => (string) $this->api
->getUrl(),
66 return new API\
ContentType($type);
69 private function checkResponseBody(Response
$response, API\ContentType
$type) {
71 $data = $type->parseBody($response->getBody());
72 } catch (\Exception
$e) {
73 $this->api
->getLogger()->error("response -> error: ".$e->getMessage(), [
74 "url" => (string) $this->api
->getUrl(),