-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from resend/feat-broadcasts
feat: Add Broadcasts API
- Loading branch information
Showing
9 changed files
with
194 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Resend; | ||
|
||
class Broadcast extends Resource | ||
{ | ||
// | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Resend\Service; | ||
|
||
use Resend\ValueObjects\Transporter\Payload; | ||
|
||
class Broadcast extends Service | ||
{ | ||
/** | ||
* Retrieve a single broadcast. | ||
* | ||
* @see https://resend.com/docs/api-reference/broadcasts/get-broadcast | ||
*/ | ||
public function get(string $id): \Resend\Broadcast | ||
{ | ||
$payload = Payload::get('broadcasts', $id); | ||
|
||
$result = $this->transporter->request($payload); | ||
|
||
return $this->createResource('broadcasts', $result); | ||
} | ||
|
||
/** | ||
* Create a new broadcast to send to your audience. | ||
* | ||
* @see https://resend.com/docs/api-reference/broadcasts/create-broadcast | ||
*/ | ||
public function create(array $parameters): \Resend\Broadcast | ||
{ | ||
$payload = Payload::create('broadcasts', $parameters); | ||
|
||
$result = $this->transporter->request($payload); | ||
|
||
return $this->createResource('broadcasts', $result); | ||
} | ||
|
||
/** | ||
* List all domains. | ||
* | ||
* @return \Resend\Collection<\Resend\Broadcast> | ||
* | ||
* @see https://resend.com/docs/api-reference/broadcasts/list-broadcasts | ||
*/ | ||
public function list(): \Resend\Collection | ||
{ | ||
$payload = Payload::list('broadcasts'); | ||
|
||
$result = $this->transporter->request($payload); | ||
|
||
return $this->createResource('broadcasts', $result); | ||
} | ||
|
||
/** | ||
* Start sending broadcasts to your audience. | ||
* | ||
* @see https://resend.com/docs/api-reference/broadcasts/send-broadcast | ||
*/ | ||
public function send(string $broadcastId, array $parameters): \Resend\Broadcast | ||
{ | ||
$payload = Payload::create("broadcasts/{$broadcastId}/send", $parameters); | ||
|
||
$result = $this->transporter->request($payload); | ||
|
||
return $this->createResource('broadcasts', $result); | ||
} | ||
|
||
/** | ||
* Remove an existing broadcast. | ||
* | ||
* @see https://resend.com/docs/api-reference/broadcasts/delete-broadcast | ||
*/ | ||
public function remove(string $id): \Resend\Broadcast | ||
{ | ||
$payload = Payload::delete('broadcasts', $id); | ||
|
||
$result = $this->transporter->request($payload); | ||
|
||
return $this->createResource('broadcasts', $result); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
function broadcast(): array | ||
{ | ||
return [ | ||
'id' => '559ac32e-9ef5-46fb-82a1-b76b840c0f7b', | ||
'object' => 'broadcast', | ||
'name' => 'Announcements', | ||
'audience_id' => '78261eea-8f8b-4381-83c6-79fa7120f1cf', | ||
'from' => 'Acme <[email protected]>', | ||
'subject' => 'hello world', | ||
'reply_to' => null, | ||
'preview_text' => 'Check out our latest announcements', | ||
'status' => 'draft', | ||
'created_at' => '2024-12-01T19:32:22.980Z', | ||
'scheduled_at' => null, | ||
'sent_at' => null, | ||
]; | ||
} | ||
|
||
function broadcasts(): array | ||
{ | ||
return [ | ||
'data' => [ | ||
[ | ||
'id' => '559ac32e-9ef5-46fb-82a1-b76b840c0f7b', | ||
'object' => 'broadcast', | ||
'name' => 'Announcements', | ||
'audience_id' => '78261eea-8f8b-4381-83c6-79fa7120f1cf', | ||
'from' => 'Acme <[email protected]>', | ||
'subject' => 'hello world', | ||
'reply_to' => null, | ||
'preview_text' => 'Check out our latest announcements', | ||
'status' => 'draft', | ||
'created_at' => '2024-12-01T19:32:22.980Z', | ||
'scheduled_at' => null, | ||
'sent_at' => null, | ||
], | ||
], | ||
]; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
use Resend\Broadcast; | ||
use Resend\Collection; | ||
|
||
it('can get a broadcase resource', function () { | ||
$client = mockClient('GET', 'broadcasts/559ac32e-9ef5-46fb-82a1-b76b840c0f7b', [], broadcast()); | ||
|
||
$result = $client->broadcasts->get('559ac32e-9ef5-46fb-82a1-b76b840c0f7b'); | ||
|
||
expect($result)->toBeInstanceOf(Broadcast::class) | ||
->id->toBe('559ac32e-9ef5-46fb-82a1-b76b840c0f7b'); | ||
}); | ||
|
||
it('can create a broadcast resource', function () { | ||
$client = mockClient('POST', 'broadcasts', [ | ||
'audience_id' => '78261eea-8f8b-4381-83c6-79fa7120f1cf', | ||
'from' => 'Acme <[email protected]>', | ||
'subject' => 'hello world', | ||
'html' => 'Hi {{{FIRST_NAME|there}}}, you can unsubscribe here: {{{RESEND_UNSUBSCRIBE_URL}}}', | ||
], broadcast()); | ||
|
||
$result = $client->broadcasts->create([ | ||
'audience_id' => '78261eea-8f8b-4381-83c6-79fa7120f1cf', | ||
'from' => 'Acme <[email protected]>', | ||
'subject' => 'hello world', | ||
'html' => 'Hi {{{FIRST_NAME|there}}}, you can unsubscribe here: {{{RESEND_UNSUBSCRIBE_URL}}}', | ||
]); | ||
|
||
expect($result)->toBeInstanceOf(Broadcast::class) | ||
->id->toBe('559ac32e-9ef5-46fb-82a1-b76b840c0f7b'); | ||
}); | ||
|
||
it('can get a list of broadcast resources', function () { | ||
$client = mockClient('GET', 'broadcasts', [], broadcasts()); | ||
|
||
$result = $client->broadcasts->list(); | ||
|
||
expect($result)->toBeInstanceOf(Collection::class) | ||
->data->toBeArray(); | ||
}); | ||
|
||
it('can remove a broadcast resource', function () { | ||
$client = mockClient('DELETE', 'broadcasts/559ac32e-9ef5-46fb-82a1-b76b840c0f7b', [], broadcast()); | ||
|
||
$result = $client->broadcasts->remove('559ac32e-9ef5-46fb-82a1-b76b840c0f7b'); | ||
|
||
expect($result)->toBeInstanceOf(Broadcast::class) | ||
->id->toBe('559ac32e-9ef5-46fb-82a1-b76b840c0f7b'); | ||
}); |
This file contains 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