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 * @param Response $response
40 * @return null|API\Links
41 * @throws RequestException
43 private function checkResponseMeta(Response
$response) {
44 if ($response->getResponseCode() >= 400) {
45 $e = new RequestException($response);
47 $this->api
->getLogger()->critical("response -> error: ".$e->getMessage(), [
48 "url" => (string) $this->api
->getUrl(),
54 if (!($link = $response->getHeader("Link", Header
::class))) {
58 return new API\
Links($link);
62 * @param Response $response
63 * @return API\ContentType
64 * @throws RequestException
66 private function checkResponseType(Response
$response) {
67 if (!($type = $response->getHeader("Content-Type", Header
::class))) {
68 $e = new RequestException($response);
70 $this->api
->getLogger()->error("response -> error: Empty Content-Type -> ".$e->getMessage(), [
71 "url" => (string) $this->api
->getUrl(),
77 return new API\
ContentType($this->api
->getVersion(), $type->value
);
81 * @param Response $response
82 * @param API\ContentType $type
86 private function checkResponseBody(Response
$response, API\ContentType
$type) {
88 $data = $type->decode($response->getBody());
89 } catch (\Exception
$e) {
90 $this->api
->getLogger()->error("response -> error: ".$e->getMessage(), [
91 "url" => (string) $this->api
->getUrl(),