Skip to content

Commit

Permalink
Merge pull request #62 from resend/feat-broadcasts
Browse files Browse the repository at this point in the history
feat: Add Broadcasts API
  • Loading branch information
jayanratna authored Dec 19, 2024
2 parents d790075 + 046edf2 commit e692904
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ on:

jobs:
tests:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [8.1, 8.2, 8.3]
php: [8.1, 8.2, 8.3, 8.4]

name: PHP ${{ matrix.php }}

Expand Down Expand Up @@ -45,4 +45,4 @@ jobs:
run: composer update --no-interaction --prefer-dist

- name: Execute tests
run: vendor/bin/pest
run: vendor/bin/pest --display-deprecations --fail-on-deprecation
8 changes: 8 additions & 0 deletions src/Broadcast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Resend;

class Broadcast extends Resource
{
//
}
15 changes: 8 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
/**
* Client used to send requests to the Resend API.
*
* @property \Resend\Service\ApiKey $apiKeys
* @property \Resend\Service\Audience $audiences
* @property \Resend\Service\Batch $batch
* @property \Resend\Service\Contact $contacts
* @property \Resend\Service\Domain $domains
* @property \Resend\Service\Email $emails
* @property Service\ApiKey $apiKeys
* @property Service\Audience $audiences
* @property Service\Batch $batch
* @property Service\Broadcast $broadcasts
* @property Service\Contact $contacts
* @property Service\Domain $domains
* @property Service\Email $emails
*/
class Client implements ClientContract
{
Expand All @@ -36,7 +37,7 @@ public function __construct(
* Send an email with the given parameters.
*
* @deprecated
* @see @see https://resend.com/docs/api-reference/emails/send-email#body-parameters
* @see https://resend.com/docs/api-reference/emails/send-email#body-parameters
*/
public function sendEmail(array $parameters): Email
{
Expand Down
80 changes: 80 additions & 0 deletions src/Service/Broadcast.php
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);
}
}
2 changes: 2 additions & 0 deletions src/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Resend\ApiKey;
use Resend\Audience;
use Resend\Broadcast;
use Resend\Collection;
use Resend\Contact;
use Resend\Contracts\Transporter;
Expand All @@ -19,6 +20,7 @@ abstract class Service
protected $mapping = [
'api-keys' => ApiKey::class,
'audiences' => Audience::class,
'broadcasts' => Broadcast::class,
'contacts' => Contact::class,
'domains' => Domain::class,
'emails' => Email::class,
Expand Down
1 change: 1 addition & 0 deletions src/Service/ServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ServiceFactory
'apiKeys' => ApiKey::class,
'audiences' => Audience::class,
'batch' => Batch::class,
'broadcasts' => Broadcast::class,
'contacts' => Contact::class,
'domains' => Domain::class,
'emails' => Email::class,
Expand Down
41 changes: 41 additions & 0 deletions tests/Fixtures/Broadcast.php
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,
],
],
];
}
50 changes: 50 additions & 0 deletions tests/Service/Broadcast.php
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');
});
1 change: 1 addition & 0 deletions tests/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Resend\ValueObjects\Transporter\Payload;

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

$apiKey = ApiKey::from('foo');
Expand Down

0 comments on commit e692904

Please sign in to comment.