Skip to content

Instantly share code, notes, and snippets.

@flexchar
Last active November 28, 2024 15:55
Show Gist options
  • Save flexchar/43ac4e41e7f77d3263823e14c0165987 to your computer and use it in GitHub Desktop.
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)
<?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();
}
}
@flexchar
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment