Skip to content

Commit

Permalink
make code more resilient when mailcoach is slow
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 4, 2023
1 parent f4cc446 commit a346bfa
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions app/Services/Mailcoach/MailcoachApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ public function getSubscriber(string $email, string $listUuid = null): ?Subscrib
{
$listUuid ??= '4af46b59-3784-41a5-9272-6da31afa3a02';

$subscribers = Http::withToken(config('services.mailcoach.token'))
$response = Http::timeout(10)->withToken(config('services.mailcoach.token'))
->get("https://spatie.mailcoach.app/api/email-lists/{$listUuid}/subscribers", [
'filter' => [
'email' => $email,
],
])
->json('data');
]);

if (!$response->successful()) {
return null;
}

$subscribers = $response->json('data');

if (! isset($subscribers[0])) {
if (!isset($subscribers[0])) {
return null;
}

Expand All @@ -29,13 +34,13 @@ public function subscribe(string $email, string $listUuid = null, bool $skipConf
{
$listUuid ??= '4af46b59-3784-41a5-9272-6da31afa3a02';

$response = Http::withToken(config('services.mailcoach.token'))
$response = Http::timeout(10)->withToken(config('services.mailcoach.token'))
->post("https://spatie.mailcoach.app/api/email-lists/{$listUuid}/subscribers", [
'email' => $email,
'skip_confirmation' => $skipConfirmation,
]);

if (! $response->successful() || ! $response->json('data') || ! $response->json('data.uuid')) {
if (!$response->successful() || !$response->json('data') || !$response->json('data.uuid')) {
return null;
}

Expand All @@ -44,13 +49,13 @@ public function subscribe(string $email, string $listUuid = null, bool $skipConf

public function unsubscribe(Subscriber $subscriber): void
{
Http::withToken(config('services.mailcoach.token'))
Http::timeout(10)->withToken(config('services.mailcoach.token'))
->post("https://spatie.mailcoach.app/api/subscribers/{$subscriber->uuid}/unsubscribe");
}

public function addTags(Subscriber $subscriber, array $tags): void
{
Http::withToken(config('services.mailcoach.token'))
Http::timeout(10)->withToken(config('services.mailcoach.token'))
->patch("https://spatie.mailcoach.app/api/subscribers/{$subscriber->uuid}", [
'tags' => $tags,
'append_tags' => true,
Expand All @@ -60,9 +65,9 @@ public function addTags(Subscriber $subscriber, array $tags): void

public function removeTag(Subscriber $subscriber, string $tag): void
{
$tags = array_filter($subscriber->tags, fn (string $existingTag) => $existingTag !== $tag);
$tags = array_filter($subscriber->tags, fn(string $existingTag) => $existingTag !== $tag);

Http::withToken(config('services.mailcoach.token'))
Http::timeout(10)->withToken(config('services.mailcoach.token'))
->patch("https://spatie.mailcoach.app/api/subscribers/{$subscriber->uuid}", [
'tags' => $tags,
'append_tags' => false,
Expand Down

0 comments on commit a346bfa

Please sign in to comment.