diff --git a/src/Resources/MessageResource.php b/src/Resources/MessageResource.php index 1983207..3d2d687 100644 --- a/src/Resources/MessageResource.php +++ b/src/Resources/MessageResource.php @@ -7,6 +7,7 @@ use HeyJorgeDev\QStash\Responses\MessagePublishResponse; use HeyJorgeDev\QStash\ValueObjects\Message; use HeyJorgeDev\QStash\ValueObjects\MessageToPublish; +use HeyJorgeDev\QStash\ValueObjects\Transporter\Request; class MessageResource implements MessageInterface { @@ -16,9 +17,25 @@ public function publish(MessageToPublish $message): MessagePublishResponse { $upstashHeaders = $message->toUpstashHeaders(); - $response = $this->transporter->request('POST', '/publish'); + $request = Request::POST("/publish/{$message->destination->toString()}") + ->withBody($message->body) + ->appendHeaders($upstashHeaders); - return new MessagePublishResponse(); + $response = $this->transporter->send($request); + + if (! $response->isSuccessful()) { + return new MessagePublishResponse( + $response->statusCode, + [], + $response->body + ); + } + + return new MessagePublishResponse( + $response->statusCode, + $response->body, + [] + ); } public function enqueue() diff --git a/src/Responses/MessagePublishResponse.php b/src/Responses/MessagePublishResponse.php index 2d1bba4..1d104d5 100644 --- a/src/Responses/MessagePublishResponse.php +++ b/src/Responses/MessagePublishResponse.php @@ -7,18 +7,24 @@ class MessagePublishResponse implements ResponseWithDataInterface { + public function __construct( + protected int $statusCode, + protected array $data, + protected array $errors, + ) {} + public function isSuccessful(): bool { - // TODO: Implement isSuccessful() method. + return $this->statusCode === 200; } public function getErrors(): array { - // TODO: Implement getErrors() method. + return $this->errors; } public function getData(): Message { - return new Message(); + return new Message(id: $this->data['messageId']); } } diff --git a/src/Transporters/HttpTransporter.php b/src/Transporters/HttpTransporter.php index e07869f..b529a34 100644 --- a/src/Transporters/HttpTransporter.php +++ b/src/Transporters/HttpTransporter.php @@ -38,6 +38,7 @@ public function send(Request $request): Response ->appendHeaders($this->headers->with('Content-Type', 'application/json')); try { + $response = $this->httpClient->sendRequest($request->toPsr7Request()); return new Response( diff --git a/src/ValueObjects/MessageToPublish.php b/src/ValueObjects/MessageToPublish.php index 83bd7e1..f9ca92f 100644 --- a/src/ValueObjects/MessageToPublish.php +++ b/src/ValueObjects/MessageToPublish.php @@ -7,8 +7,8 @@ class MessageToPublish { public function __construct( - private Url|TopicName $destination, - private array|string $body = [], + public readonly Url|TopicName $destination, + public readonly array|string $body = [], private int $delay = 0, private int $retries = 0, private string $method = 'GET', diff --git a/src/ValueObjects/Transporter/Request.php b/src/ValueObjects/Transporter/Request.php index ed44837..260c236 100644 --- a/src/ValueObjects/Transporter/Request.php +++ b/src/ValueObjects/Transporter/Request.php @@ -75,16 +75,6 @@ public function appendHeaders(Headers $headers): self return $this->withHeaders($this->headers->merge($headers)); } - public function toPsr7Request(): Psr7Request - { - return new Psr7Request( - method: $this->method, - uri: $this->url->toString(), - headers: $this->headers->toArray(), - body: $this->body, - ); - } - public function withBody(string|array|null $body): self { return new self( @@ -99,4 +89,14 @@ public function withBaseUrl(Url $baseUrl): self { return $this->withUrl($baseUrl->append($this->url->toString())); } + + public function toPsr7Request(): Psr7Request + { + return new Psr7Request( + method: $this->method, + uri: $this->url->toString(), + headers: $this->headers->toArray(), + body: is_array($this->body) ? json_encode($this->body) : $this->body, + ); + } } diff --git a/src/ValueObjects/Transporter/Response.php b/src/ValueObjects/Transporter/Response.php index 6e09f71..d81fd0f 100644 --- a/src/ValueObjects/Transporter/Response.php +++ b/src/ValueObjects/Transporter/Response.php @@ -9,4 +9,9 @@ public function __construct( public readonly ?array $body = null, public readonly Headers $headers = new Headers([]), ) {} + + public function isSuccessful(): bool + { + return $this->statusCode >= 200 && $this->statusCode < 300; + } }