Initial
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user