Skip to content

Commit e692904

Browse files
authored
Merge pull request #62 from resend/feat-broadcasts
feat: Add Broadcasts API
2 parents d790075 + 046edf2 commit e692904

File tree

9 files changed

+194
-10
lines changed

9 files changed

+194
-10
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ on:
1010

1111
jobs:
1212
tests:
13-
runs-on: ubuntu-22.04
13+
runs-on: ubuntu-latest
1414

1515
strategy:
1616
fail-fast: true
1717
matrix:
18-
php: [8.1, 8.2, 8.3]
18+
php: [8.1, 8.2, 8.3, 8.4]
1919

2020
name: PHP ${{ matrix.php }}
2121

@@ -45,4 +45,4 @@ jobs:
4545
run: composer update --no-interaction --prefer-dist
4646

4747
- name: Execute tests
48-
run: vendor/bin/pest
48+
run: vendor/bin/pest --display-deprecations --fail-on-deprecation

src/Broadcast.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Resend;
4+
5+
class Broadcast extends Resource
6+
{
7+
//
8+
}

src/Client.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
/**
1010
* Client used to send requests to the Resend API.
1111
*
12-
* @property \Resend\Service\ApiKey $apiKeys
13-
* @property \Resend\Service\Audience $audiences
14-
* @property \Resend\Service\Batch $batch
15-
* @property \Resend\Service\Contact $contacts
16-
* @property \Resend\Service\Domain $domains
17-
* @property \Resend\Service\Email $emails
12+
* @property Service\ApiKey $apiKeys
13+
* @property Service\Audience $audiences
14+
* @property Service\Batch $batch
15+
* @property Service\Broadcast $broadcasts
16+
* @property Service\Contact $contacts
17+
* @property Service\Domain $domains
18+
* @property Service\Email $emails
1819
*/
1920
class Client implements ClientContract
2021
{
@@ -36,7 +37,7 @@ public function __construct(
3637
* Send an email with the given parameters.
3738
*
3839
* @deprecated
39-
* @see @see https://resend.com/docs/api-reference/emails/send-email#body-parameters
40+
* @see https://resend.com/docs/api-reference/emails/send-email#body-parameters
4041
*/
4142
public function sendEmail(array $parameters): Email
4243
{

src/Service/Broadcast.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Resend\Service;
4+
5+
use Resend\ValueObjects\Transporter\Payload;
6+
7+
class Broadcast extends Service
8+
{
9+
/**
10+
* Retrieve a single broadcast.
11+
*
12+
* @see https://resend.com/docs/api-reference/broadcasts/get-broadcast
13+
*/
14+
public function get(string $id): \Resend\Broadcast
15+
{
16+
$payload = Payload::get('broadcasts', $id);
17+
18+
$result = $this->transporter->request($payload);
19+
20+
return $this->createResource('broadcasts', $result);
21+
}
22+
23+
/**
24+
* Create a new broadcast to send to your audience.
25+
*
26+
* @see https://resend.com/docs/api-reference/broadcasts/create-broadcast
27+
*/
28+
public function create(array $parameters): \Resend\Broadcast
29+
{
30+
$payload = Payload::create('broadcasts', $parameters);
31+
32+
$result = $this->transporter->request($payload);
33+
34+
return $this->createResource('broadcasts', $result);
35+
}
36+
37+
/**
38+
* List all domains.
39+
*
40+
* @return \Resend\Collection<\Resend\Broadcast>
41+
*
42+
* @see https://resend.com/docs/api-reference/broadcasts/list-broadcasts
43+
*/
44+
public function list(): \Resend\Collection
45+
{
46+
$payload = Payload::list('broadcasts');
47+
48+
$result = $this->transporter->request($payload);
49+
50+
return $this->createResource('broadcasts', $result);
51+
}
52+
53+
/**
54+
* Start sending broadcasts to your audience.
55+
*
56+
* @see https://resend.com/docs/api-reference/broadcasts/send-broadcast
57+
*/
58+
public function send(string $broadcastId, array $parameters): \Resend\Broadcast
59+
{
60+
$payload = Payload::create("broadcasts/{$broadcastId}/send", $parameters);
61+
62+
$result = $this->transporter->request($payload);
63+
64+
return $this->createResource('broadcasts', $result);
65+
}
66+
67+
/**
68+
* Remove an existing broadcast.
69+
*
70+
* @see https://resend.com/docs/api-reference/broadcasts/delete-broadcast
71+
*/
72+
public function remove(string $id): \Resend\Broadcast
73+
{
74+
$payload = Payload::delete('broadcasts', $id);
75+
76+
$result = $this->transporter->request($payload);
77+
78+
return $this->createResource('broadcasts', $result);
79+
}
80+
}

src/Service/Service.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Resend\ApiKey;
66
use Resend\Audience;
7+
use Resend\Broadcast;
78
use Resend\Collection;
89
use Resend\Contact;
910
use Resend\Contracts\Transporter;
@@ -19,6 +20,7 @@ abstract class Service
1920
protected $mapping = [
2021
'api-keys' => ApiKey::class,
2122
'audiences' => Audience::class,
23+
'broadcasts' => Broadcast::class,
2224
'contacts' => Contact::class,
2325
'domains' => Domain::class,
2426
'emails' => Email::class,

src/Service/ServiceFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ServiceFactory
1515
'apiKeys' => ApiKey::class,
1616
'audiences' => Audience::class,
1717
'batch' => Batch::class,
18+
'broadcasts' => Broadcast::class,
1819
'contacts' => Contact::class,
1920
'domains' => Domain::class,
2021
'emails' => Email::class,

tests/Fixtures/Broadcast.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
function broadcast(): array
4+
{
5+
return [
6+
'id' => '559ac32e-9ef5-46fb-82a1-b76b840c0f7b',
7+
'object' => 'broadcast',
8+
'name' => 'Announcements',
9+
'audience_id' => '78261eea-8f8b-4381-83c6-79fa7120f1cf',
10+
'from' => 'Acme <[email protected]>',
11+
'subject' => 'hello world',
12+
'reply_to' => null,
13+
'preview_text' => 'Check out our latest announcements',
14+
'status' => 'draft',
15+
'created_at' => '2024-12-01T19:32:22.980Z',
16+
'scheduled_at' => null,
17+
'sent_at' => null,
18+
];
19+
}
20+
21+
function broadcasts(): array
22+
{
23+
return [
24+
'data' => [
25+
[
26+
'id' => '559ac32e-9ef5-46fb-82a1-b76b840c0f7b',
27+
'object' => 'broadcast',
28+
'name' => 'Announcements',
29+
'audience_id' => '78261eea-8f8b-4381-83c6-79fa7120f1cf',
30+
'from' => 'Acme <[email protected]>',
31+
'subject' => 'hello world',
32+
'reply_to' => null,
33+
'preview_text' => 'Check out our latest announcements',
34+
'status' => 'draft',
35+
'created_at' => '2024-12-01T19:32:22.980Z',
36+
'scheduled_at' => null,
37+
'sent_at' => null,
38+
],
39+
],
40+
];
41+
}

tests/Service/Broadcast.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Resend\Broadcast;
4+
use Resend\Collection;
5+
6+
it('can get a broadcase resource', function () {
7+
$client = mockClient('GET', 'broadcasts/559ac32e-9ef5-46fb-82a1-b76b840c0f7b', [], broadcast());
8+
9+
$result = $client->broadcasts->get('559ac32e-9ef5-46fb-82a1-b76b840c0f7b');
10+
11+
expect($result)->toBeInstanceOf(Broadcast::class)
12+
->id->toBe('559ac32e-9ef5-46fb-82a1-b76b840c0f7b');
13+
});
14+
15+
it('can create a broadcast resource', function () {
16+
$client = mockClient('POST', 'broadcasts', [
17+
'audience_id' => '78261eea-8f8b-4381-83c6-79fa7120f1cf',
18+
'from' => 'Acme <[email protected]>',
19+
'subject' => 'hello world',
20+
'html' => 'Hi {{{FIRST_NAME|there}}}, you can unsubscribe here: {{{RESEND_UNSUBSCRIBE_URL}}}',
21+
], broadcast());
22+
23+
$result = $client->broadcasts->create([
24+
'audience_id' => '78261eea-8f8b-4381-83c6-79fa7120f1cf',
25+
'from' => 'Acme <[email protected]>',
26+
'subject' => 'hello world',
27+
'html' => 'Hi {{{FIRST_NAME|there}}}, you can unsubscribe here: {{{RESEND_UNSUBSCRIBE_URL}}}',
28+
]);
29+
30+
expect($result)->toBeInstanceOf(Broadcast::class)
31+
->id->toBe('559ac32e-9ef5-46fb-82a1-b76b840c0f7b');
32+
});
33+
34+
it('can get a list of broadcast resources', function () {
35+
$client = mockClient('GET', 'broadcasts', [], broadcasts());
36+
37+
$result = $client->broadcasts->list();
38+
39+
expect($result)->toBeInstanceOf(Collection::class)
40+
->data->toBeArray();
41+
});
42+
43+
it('can remove a broadcast resource', function () {
44+
$client = mockClient('DELETE', 'broadcasts/559ac32e-9ef5-46fb-82a1-b76b840c0f7b', [], broadcast());
45+
46+
$result = $client->broadcasts->remove('559ac32e-9ef5-46fb-82a1-b76b840c0f7b');
47+
48+
expect($result)->toBeInstanceOf(Broadcast::class)
49+
->id->toBe('559ac32e-9ef5-46fb-82a1-b76b840c0f7b');
50+
});

tests/Transporters/HttpTransporter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Resend\ValueObjects\Transporter\Payload;
1616

1717
beforeEach(function () {
18+
/** @var ClientInterface|Mockery\MockInterface */
1819
$this->client = Mockery::mock(ClientInterface::class);
1920

2021
$apiKey = ApiKey::from('foo');

0 commit comments

Comments
 (0)