Last active
November 28, 2024 15:55
-
-
Save flexchar/43ac4e41e7f77d3263823e14c0165987 to your computer and use it in GitHub Desktop.
Minimal class for creating and deleting contacts from Laravel users in Loops.so (email service)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use App\Models\User; | |
use Illuminate\Http\Client\Response; | |
use Illuminate\Support\Facades\Http; | |
class LoopsService | |
{ | |
private string $token; | |
private string $baseUrl = 'https://app.loops.so/api/v1'; | |
public function __construct() | |
{ | |
$this->token = config('services.loops.token'); | |
if (!$this->token) { | |
throw new \Exception('Loops API token is not set'); | |
} | |
} | |
private function request() | |
{ | |
return Http::asJson() | |
->withToken($this->token) | |
->baseUrl($this->baseUrl); | |
} | |
public function createContact(User $user): Response | |
{ | |
return $this->request()->post('/contacts/create', [ | |
'email' => $user->email, | |
'firstName' => $user->first_name, | |
'lastName' => $user->last_name, | |
'source' => 'website', | |
'subscribed' => true, | |
'userId' => (string) $user->id, | |
]); | |
} | |
public function findContact(string $userId): ?array | |
{ | |
$response = $this->request()->get('/contacts/find', [ | |
'userId' => $userId, | |
]); | |
if (!$response->successful()) { | |
return null; | |
} | |
return $response->json(); | |
} | |
public function deleteContact(string $userId): bool | |
{ | |
$response = $this->request()->delete('/contacts/delete', [ | |
'userId' => $userId, | |
]); | |
return $response->successful(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ref: https://loops.so/docs/api-reference/create-contact