46 lines
1.0 KiB
PHP
46 lines
1.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Ekdos\N8n;
|
|
|
|
use Ekdos\Support\Json;
|
|
|
|
/** Eine Antwort von n8n, roh und unbewertet. */
|
|
final readonly class Reply
|
|
{
|
|
public function __construct(
|
|
public int $status,
|
|
public string $body,
|
|
public string $contentType = '',
|
|
public bool $stale = false,
|
|
) {}
|
|
|
|
public function ok(): bool
|
|
{
|
|
return $this->status >= 200 && $this->status < 300;
|
|
}
|
|
|
|
/** Status fuer die Oberflaeche: ein Verbindungsfehler wird zu 502. */
|
|
public function statusOr(int $fallback = 502): int
|
|
{
|
|
return $this->status === 0 ? $fallback : $this->status;
|
|
}
|
|
|
|
public function decoded(): array
|
|
{
|
|
return Json::decode($this->body);
|
|
}
|
|
|
|
/** n8n liefert Listen mal als {"key":[...]}, mal als nacktes Array. */
|
|
public function items(string $key): array
|
|
{
|
|
return Json::listFrom($this->decoded(), $key);
|
|
}
|
|
|
|
public function asStale(): self
|
|
{
|
|
return new self($this->status, $this->body, $this->contentType, true);
|
|
}
|
|
}
|