dpl/app/Services/MailFlow/ResendClient.php

41 lines
1.2 KiB
PHP

<?php
namespace App\Services\MailFlow;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
class ResendClient
{
private function request(): PendingRequest
{
return Http::baseUrl('https://api.resend.com')->withToken((string) Settings::get('resend_key'))
->acceptJson()->connectTimeout(3)->timeout(8);
}
public function usage(): array
{
$response = $this->request()->get('/usage');
if (!$response->successful()) {
throw new \RuntimeException('resend_usage_unavailable');
}
return $response->json('emails') ?? [];
}
public function send(array $payload, string $key): Response
{
// Automatic HTTP retries would hide uncertainty; retries use the same persisted key/payload.
return $this->request()->withHeaders(['Idempotency-Key' => $key])->post('/emails', $payload);
}
public function retrieve(string $id): array
{
$response = $this->request()->get('/emails/' . rawurlencode($id));
if (!$response->successful()) {
throw new \RuntimeException('resend_status_unavailable');
}
return $response->json();
}
}