From 919984a5c45f1ac1368d7443ccb480c33156dba8 Mon Sep 17 00:00:00 2001 From: Jorge Lapa <2780099+heyjorgedev@users.noreply.github.com> Date: Mon, 15 Jul 2024 16:45:29 +0100 Subject: [PATCH] wip --- src/Contracts/Resources/MessageInterface.php | 3 +- src/Resources/MessageResource.php | 25 +++++++++++++-- src/Responses/MessageEnqueueResponse.php | 33 ++++++++++++++++++++ src/Responses/MessagePublishResponse.php | 5 ++- src/ValueObjects/Message.php | 1 + src/ValueObjects/Transporter/Request.php | 2 +- 6 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 src/Responses/MessageEnqueueResponse.php diff --git a/src/Contracts/Resources/MessageInterface.php b/src/Contracts/Resources/MessageInterface.php index 970e13a..15cccd8 100644 --- a/src/Contracts/Resources/MessageInterface.php +++ b/src/Contracts/Resources/MessageInterface.php @@ -2,6 +2,7 @@ namespace HeyJorgeDev\QStash\Contracts\Resources; +use HeyJorgeDev\QStash\Responses\MessageEnqueueResponse; use HeyJorgeDev\QStash\Responses\MessagePublishResponse; use HeyJorgeDev\QStash\ValueObjects\Message; use HeyJorgeDev\QStash\ValueObjects\MessageToPublish; @@ -10,7 +11,7 @@ interface MessageInterface { public function publish(MessageToPublish $message): MessagePublishResponse; - public function enqueue(); + public function enqueue(string $queueName, MessageToPublish $message): MessageEnqueueResponse; public function batch(); diff --git a/src/Resources/MessageResource.php b/src/Resources/MessageResource.php index 56fbc3a..1b6cc2c 100644 --- a/src/Resources/MessageResource.php +++ b/src/Resources/MessageResource.php @@ -5,6 +5,7 @@ use HeyJorgeDev\QStash\Contracts\Resources\MessageInterface; use HeyJorgeDev\QStash\Contracts\TransporterInterface; use HeyJorgeDev\QStash\Exceptions\NotImplementedException; +use HeyJorgeDev\QStash\Responses\MessageEnqueueResponse; use HeyJorgeDev\QStash\Responses\MessagePublishResponse; use HeyJorgeDev\QStash\ValueObjects\Message; use HeyJorgeDev\QStash\ValueObjects\MessageToPublish; @@ -39,9 +40,29 @@ public function publish(MessageToPublish $message): MessagePublishResponse ); } - public function enqueue() + public function enqueue(string $queueName, MessageToPublish $message): MessageEnqueueResponse { - throw NotImplementedException::askForContributions('enqueue messages'); + $upstashHeaders = $message->toUpstashHeaders(); + + $request = Request::POST("/publish/{$queueName}/{$message->destination->toString()}") + ->withBody($message->body) + ->appendHeaders($upstashHeaders); + + $response = $this->transporter->send($request); + + if (! $response->isSuccessful()) { + return new MessageEnqueueResponse( + $response->statusCode, + [], + $response->body + ); + } + + return new MessageEnqueueResponse( + $response->statusCode, + $response->body, + [] + ); } public function batch() diff --git a/src/Responses/MessageEnqueueResponse.php b/src/Responses/MessageEnqueueResponse.php new file mode 100644 index 0000000..d31bfb9 --- /dev/null +++ b/src/Responses/MessageEnqueueResponse.php @@ -0,0 +1,33 @@ +statusCode === 200; + } + + public function getErrors(): array + { + return $this->errors; + } + + public function getData(): Message + { + return new Message( + id: $this->data['messageId'], + deduplicated: $this->data['deduplicated'] ?? false, + ); + } +} diff --git a/src/Responses/MessagePublishResponse.php b/src/Responses/MessagePublishResponse.php index 1d104d5..acde4af 100644 --- a/src/Responses/MessagePublishResponse.php +++ b/src/Responses/MessagePublishResponse.php @@ -25,6 +25,9 @@ public function getErrors(): array public function getData(): Message { - return new Message(id: $this->data['messageId']); + return new Message( + id: $this->data['messageId'], + deduplicated: $this->data['deduplicated'] ?? false, + ); } } diff --git a/src/ValueObjects/Message.php b/src/ValueObjects/Message.php index c6d25e8..852c836 100644 --- a/src/ValueObjects/Message.php +++ b/src/ValueObjects/Message.php @@ -6,6 +6,7 @@ class Message { public function __construct( public readonly string $id, + public readonly bool $deduplicated = false, ) {} public static function to(Url|TopicName $url): MessageToPublish diff --git a/src/ValueObjects/Transporter/Request.php b/src/ValueObjects/Transporter/Request.php index 260c236..0377b2e 100644 --- a/src/ValueObjects/Transporter/Request.php +++ b/src/ValueObjects/Transporter/Request.php @@ -43,7 +43,7 @@ public static function DELETE(string|Url $url): self public function withMethod(string $method): self { return new self( - method: $method, + method: strtoupper($method), url: $this->url, headers: $this->headers, body: $this->body,