3 namespace seekat\API\Call
;
5 use http\Client\Response
;
8 use seekat\Exception\RequestException
;
13 function __construct(API
$api) {
18 * @param Response $response
20 * @throws RequestException
22 function __invoke(Response
$response) : API
{
23 $links = $this->checkResponseMeta($response);
24 $type = $this->checkResponseType($response);
25 $data = $this->checkResponseBody($response, $type);
27 $this->api
->getLogger()->info("response -> info", [
28 "type" => $type->getType(),
29 "links" => $links->getRelations(),
31 $this->api
->getLogger()->debug("response -> data", [
35 return $this->api
= $this->api
->with(compact("type", "data", "links"));
39 * @throws RequestException
41 private function checkResponseMeta(Response
$response) : API\Links
{
42 if ($response->getResponseCode() >= 400) {
43 $e = new RequestException($response);
45 $this->api
->getLogger()->critical("response -> error: ".$e->getMessage(), [
46 "url" => (string) $this->api
->getUrl(),
52 if (!($link = $response->getHeader("Link", Header
::class))) {
56 return new API\
Links($link);
60 * @throws RequestException
62 private function checkResponseType(Response
$response) : API\ContentType
{
63 if (!($type = $response->getHeader("Content-Type", Header
::class))) {
64 $e = new RequestException($response);
66 $this->api
->getLogger()->error("response -> error: Empty Content-Type -> ".$e->getMessage(), [
67 "url" => (string) $this->api
->getUrl(),
73 return new API\
ContentType($this->api
->getVersion(), $type->value
);
79 private function checkResponseBody(Response
$response, API\ContentType
$type) : mixed {
81 $data = $type->decode($response->getBody());
82 } catch (\Exception
$e) {
83 $this->api
->getLogger()->error("response -> error: ".$e->getMessage(), [
84 "url" => (string) $this->api
->getUrl(),