X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fseekat;a=blobdiff_plain;f=lib%2FAPI%2FCall%2FResult.php;fp=lib%2FAPI%2FCall%2FResult.php;h=acbc0a81651319357e0a587bb08363063f6eb3a9;hp=0000000000000000000000000000000000000000;hb=2451d97f1cb7b97e445b4dd839835b8673a4d0fc;hpb=3958595e9ff27162ae918db1453ddecd4840d481 diff --git a/lib/API/Call/Result.php b/lib/API/Call/Result.php new file mode 100644 index 0000000..acbc0a8 --- /dev/null +++ b/lib/API/Call/Result.php @@ -0,0 +1,66 @@ +api = $api; + } + + function __invoke(Response $response) : API { + $url = $this->api->getUrl(); + $log = $this->api->getLogger(); + $log->info(($response->getHeader("X-Cache-Time") ? "cached" : "enqueued")." -> response", [ + "url" => (string) $url, + "info" => $response->getInfo(), + ]); + + if ($response->getResponseCode() >= 400) { + $e = new RequestException($response); + + $log->critical(__FUNCTION__.": ".$e->getMessage(), [ + "url" => (string) $url, + ]); + + throw $e; + } + + if (!($type = $response->getHeader("Content-Type", Header::class))) { + $e = new RequestException($response); + $log->error( + __FUNCTION__.": Empty Content-Type -> ".$e->getMessage(), [ + "url" => (string) $url, + ]); + throw $e; + } + + try { + $type = new API\ContentType($type); + $data = $type->parseBody($response->getBody()); + + if (($link = $response->getHeader("Link", Header::class))) { + $links = new API\Links($link); + } else { + $links = null; + } + + $this->api = $this->api->with(compact("type", "data", "links")); + } catch (\Exception $e) { + $log->error(__FUNCTION__.": ".$e->getMessage(), [ + "url" => (string) $url + ]); + + throw $e; + } + + return $this->api; + } +}