Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
heyjorgedev committed Jul 15, 2024
1 parent 96ca03e commit 085da6f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
21 changes: 19 additions & 2 deletions src/Resources/MessageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand Down
12 changes: 9 additions & 3 deletions src/Responses/MessagePublishResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
1 change: 1 addition & 0 deletions src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/ValueObjects/MessageToPublish.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
20 changes: 10 additions & 10 deletions src/ValueObjects/Transporter/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
);
}
}
5 changes: 5 additions & 0 deletions src/ValueObjects/Transporter/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 085da6f

Please sign in to comment.