Skip to content

Commit f9452a8

Browse files
authored
feat: Add Audiences API support (#47)
1 parent abf3a3e commit f9452a8

File tree

7 files changed

+152
-0
lines changed

7 files changed

+152
-0
lines changed

src/Audience.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Resend;
4+
5+
/**
6+
* @property string $id The unique identifier for the Audience.
7+
* @property string $name The name of the Audience.
8+
* @property string $created_at Time at which the Audience was created.
9+
*/
10+
class Audience extends Resource
11+
{
12+
//
13+
}

src/Client.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Client used to send requests to the Resend API.
1111
*
1212
* @property \Resend\Service\ApiKey $apiKeys
13+
* @property \Resend\Service\Audience $audiences
1314
* @property \Resend\Service\Batch $batch
1415
* @property \Resend\Service\Contact $contacts
1516
* @property \Resend\Service\Domain $domains

src/Service/Audience.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Resend\Service;
4+
5+
use Resend\ValueObjects\Transporter\Payload;
6+
7+
class Audience extends Service
8+
{
9+
/**
10+
* Retrieve a single audience by the given ID.
11+
*
12+
* @see https://resend.com/docs/api-reference/audiences/get-audience
13+
*/
14+
public function get(string $id): \Resend\Audience
15+
{
16+
$payload = Payload::get('audiences', $id);
17+
18+
$result = $this->transporter->request($payload);
19+
20+
return $this->createResource('audiences', $result);
21+
}
22+
23+
/**
24+
* Add a new audience.
25+
*
26+
* @see https://resend.com/docs/api-reference/audiences/create-audience
27+
*/
28+
public function create(array $parameters): \Resend\Audience
29+
{
30+
$payload = Payload::create('audiences', $parameters);
31+
32+
$result = $this->transporter->request($payload);
33+
34+
return $this->createResource('audiences', $result);
35+
}
36+
37+
/**
38+
* List all audiences.
39+
*
40+
* @return \Resend\Collection<\Resend\Audience>
41+
*
42+
* @see https://resend.com/docs/api-reference/audiences/list-audiences
43+
*/
44+
public function list(): \Resend\Collection
45+
{
46+
$payload = Payload::list('audiences');
47+
48+
$result = $this->transporter->request($payload);
49+
50+
return $this->createResource('audiences', $result);
51+
}
52+
53+
/**
54+
* Remove a audience with the given ID.
55+
*
56+
* @see https://resend.com/docs/api-reference/audiences/delete-audience
57+
*/
58+
public function remove(string $id): \Resend\Audience
59+
{
60+
$payload = Payload::delete('audiences', $id);
61+
62+
$result = $this->transporter->request($payload);
63+
64+
return $this->createResource('audiences', $result);
65+
}
66+
}

src/Service/Service.php

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Resend\Service;
44

55
use Resend\ApiKey;
6+
use Resend\Audience;
67
use Resend\Collection;
78
use Resend\Contact;
89
use Resend\Contracts\Transporter;
@@ -17,6 +18,7 @@ abstract class Service
1718
*/
1819
protected $mapping = [
1920
'api-keys' => ApiKey::class,
21+
'audiences' => Audience::class,
2022
'contacts' => Contact::class,
2123
'domains' => Domain::class,
2224
'emails' => Email::class,

src/Service/ServiceFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ServiceFactory
1313
*/
1414
private static array $classMap = [
1515
'apiKeys' => ApiKey::class,
16+
'audiences' => Audience::class,
1617
'batch' => Batch::class,
1718
'contacts' => Contact::class,
1819
'domains' => Domain::class,

tests/Fixtures/Audience.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
function audience(): array
4+
{
5+
return [
6+
'object' => 'audience',
7+
'id' => '78261eea-8f8b-4381-83c6-79fa7120f1cf',
8+
'name' => 'Registered Users',
9+
'created_at' => '2023-10-06T22:59:55.977Z',
10+
];
11+
}
12+
13+
function audiences(): array
14+
{
15+
return [
16+
'object' => 'list',
17+
'data' => [
18+
[
19+
'object' => 'audience',
20+
'id' => '78261eea-8f8b-4381-83c6-79fa7120f1cf',
21+
'name' => 'Registered Users',
22+
'created_at' => '2023-10-06T22:59:55.977Z',
23+
],
24+
],
25+
];
26+
}

tests/Service/Audience.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use Resend\Audience;
4+
use Resend\Collection;
5+
6+
it('can get a audience resource', function () {
7+
$client = mockClient('GET', 'audiences/78261eea-8f8b-4381-83c6-79fa7120f1cf', [], audience());
8+
9+
$result = $client->audiences->get('78261eea-8f8b-4381-83c6-79fa7120f1cf');
10+
11+
expect($result)->toBeInstanceOf(Audience::class)
12+
->id->toBe('78261eea-8f8b-4381-83c6-79fa7120f1cf');
13+
});
14+
15+
it('can create an audience resource', function () {
16+
$client = mockClient('POST', 'audiences', [
17+
'name' => 'Registered Users',
18+
], audience());
19+
20+
$result = $client->audiences->create([
21+
'name' => 'Registered Users',
22+
]);
23+
24+
expect($result)->toBeInstanceOf(Audience::class)
25+
->id->toBe('78261eea-8f8b-4381-83c6-79fa7120f1cf');
26+
});
27+
28+
it('can get a list of audience resources', function () {
29+
$client = mockClient('GET', 'audiences', [], audiences());
30+
31+
$result = $client->audiences->list();
32+
33+
expect($result)->toBeInstanceOf(Collection::class)
34+
->data->toBeArray();
35+
});
36+
37+
it('can remove a audience resource', function () {
38+
$client = mockClient('DELETE', 'audiences/78261eea-8f8b-4381-83c6-79fa7120f1cf', [], audience());
39+
40+
$result = $client->audiences->remove('78261eea-8f8b-4381-83c6-79fa7120f1cf');
41+
42+
expect($result)->toBeInstanceOf(Audience::class);
43+
});

0 commit comments

Comments
 (0)