Skip to content

Commit

Permalink
Batch API (#6)
Browse files Browse the repository at this point in the history
- Create or update a batch of subscribers
- Unsubscribe a batch of subscribers
- Record a batch of events
  • Loading branch information
glorand committed Dec 19, 2018
1 parent 756e7ad commit 85d7cc5
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to `glorand/drip-php` will be documented in this file

## 1.0.3 - 2018-12-18
### Added
- Batch API
- Create or update a batch of subscribers
- Unsubscribe a batch of subscribers
- Record a batch of events

## 1.0.2 - 2018-12-11
### Added
- Events & Subscribers
Expand Down
78 changes: 71 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@
<img src="https://user-images.githubusercontent.com/883989/49755957-17ec0980-fcc2-11e8-9e04-0339714f979b.png">
</p>

[![Latest Stable Version](https://poser.pugx.org/glorand/drip-php/v/stable)](https://packagist.org/packages/glorand/drip-php)
[![Build Status](https://travis-ci.com/glorand/drip.svg?branch=master)](https://travis-ci.com/glorand/drip)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE.md)
[![CodeFactor](https://www.codefactor.io/repository/github/glorand/drip/badge/master)](https://www.codefactor.io/repository/github/glorand/drip/overview/master)
[![StyleCI](https://github.styleci.io/repos/160333136/shield?branch=master)](https://github.styleci.io/repos/160333136)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/glorand/drip/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/glorand/drip/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/glorand/drip/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/glorand/drip/?branch=master)
<h6 align="center">
A PHP wrapper for Drip's REST API
</h6>

<p align="center">
<a href="https://packagist.org/packages/glorand/drip-php">
<img src="https://poser.pugx.org/glorand/drip-php/v/stable" alt="Latest Stable Version">
</a>
<a href="https://travis-ci.com/glorand/drip">
<img src="https://travis-ci.com/glorand/drip.svg?branch=master" alt="Build Status">
</a>
<a href="LICENSE.md">
<img src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat" alt="Software License">
</a>
<a href="https://www.codefactor.io/repository/github/glorand/drip/overview/master">
<img src="https://www.codefactor.io/repository/github/glorand/drip/badge/master" alt="CodeFactor">
</a>
<a href="https://github.styleci.io/repos/160333136">
<img src="https://github.styleci.io/repos/160333136/shield?branch=master" alt="StyleCI">
</a>
<a href="https://scrutinizer-ci.com/g/glorand/drip/?branch=master">
<img src="https://scrutinizer-ci.com/g/glorand/drip/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality">
</a>
<a href="https://scrutinizer-ci.com/g/glorand/drip/?branch=master">
<img src="https://scrutinizer-ci.com/g/glorand/drip/badges/coverage.png?b=master" alt="Code Coverage">
</a>
</p>

# Drip PHP
A PHP wrapper for Drip's REST API v2.0 for PHP 7.1+

Expand All @@ -23,6 +43,7 @@ Author: Gombos Lorand
- [Accounts](#accounts)
- [Events](#events)
- [Subscribers](#subscribers)
- [Batch Operations](#batch_operations)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -140,6 +161,49 @@ $drip->subscribers()->store($subscriber);
$events = $drip->subscribers()->list();
```

### Batch Operations <a name="batch_operations"></a>

**Create or update a batch of subscribers**
```php
$testData = [
[
"email" => "john@acme.com",
"time_zone" => "America/Los_Angeles",
],
(new Subscriber())->setEmail('joe@acme.com')->setTimeZone('America/Los_Angeles'),
];

/** boolean */
$drip->subscribers()->batchStore($testData);
```

**Unsubscribe a batch of subscribers**
```php
$testData = [
[
"email" => "john@acme.com",
],
(new Subscriber())->setEmail('joe@acme.com'),
];

/** boolean */
$drip->subscribers()->batchUnsubscribe($testData);
```

**Record a batch of events**
```php
$testData = [
[
"email" => "john@acme.com",
"action" => "Opened a door",
],
(new Event())->setEmail('joe@acme.com')->setAction('Closed a door'),
];

/** boolean */
$drip->events()->batchStore($testData);
```

## Changelog <a name="changelog"></a>
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Expand Down
32 changes: 32 additions & 0 deletions src/Api/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,36 @@ public function store(Event $event): bool

return $response->isSuccess();
}

/**
* @param array $events
*
* @return bool
*/
public function batchStore(array $events)
{
$data = [];
foreach ($events as $event) {
if (is_array($event)) {
$data[] = $event;
} elseif ($event instanceof Event) {
$data[] = $event->jsonSerialize();
}
}

$postData = [
'batches' => [
[
'events' => $data,
],
],
];

$response = $this->sendPost(
':account_id:/events/batches',
$postData
);

return $response->isSuccess();
}
}
7 changes: 6 additions & 1 deletion src/Api/Response/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ public function getContents(): array

public function getErrors()
{
if ($this->isSuccess()) {
return [];
}
if (!empty($this->body['errors'])) {
return array_map(function ($rec) {
return new Error($rec['message'], $rec['code']);
}, $this->body['errors']);
} else {
return [];
return [
new Error($this->getHttpMessage(), $this->getStatusCode()),
];
}
}
}
59 changes: 59 additions & 0 deletions src/Api/Subscribers.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,63 @@ public function store(Subscriber $subscriber): bool

return $response->isSuccess();
}

/**
* @param array $subscribers
*
* @return bool
*/
public function batchStore(array $subscribers)
{
$data = [];
foreach ($subscribers as $subscriber) {
if (is_array($subscriber)) {
$data[] = $subscriber;
} elseif ($subscriber instanceof Subscriber) {
$data[] = $subscriber->jsonSerialize();
}
}

$postData = [
'batches' => [
[
'subscribers' => $data,
],
],
];

$response = $this->sendPost(
':account_id:/subscribers/batches',
$postData
);

return $response->isSuccess();
}

public function batchUnsubscribe(array $subscribers)
{
$data = [];
foreach ($subscribers as $subscriber) {
if (is_array($subscriber)) {
$data[] = $subscriber;
} elseif ($subscriber instanceof Subscriber) {
$data[] = $subscriber->getEmail();
}
}

$postData = [
'batches' => [
[
'subscribers' => $data,
],
],
];

$response = $this->sendPost(
':account_id:/unsubscribes/batches',
$postData
);

return $response->isSuccess();
}
}
8 changes: 8 additions & 0 deletions src/Models/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public function setEmail(string $email): self
return $this;
}

/**
* @return string
*/
public function getEmail(): string
{
return $this->email;
}

/**
* @param string $new_email
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Api/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function canListEvents()
$events = $drip->events()->list();
$this->assertFalse($events->isSuccess());
$this->assertEquals('Unauthorized', $events->getHttpMessage());
$this->assertIsArray($events->getErrors());

$this->expectException(DripException::class);
$events = $drip->events()->list(1, 1001);
Expand Down Expand Up @@ -64,4 +65,26 @@ public function canCreateEvent()
$response = $drip->events()->store($event);
$this->assertTrue($response);
}

public function testBatchStore()
{
$mock = new MockHandler([
new Response(403, []),
new Response(201, []),
]);
$drip = new Drip($this->accountId, $this->apiToken, $this->userAgent);
$drip->setHandler($mock);
$testData = [
[
"email" => "john@acme.com",
"action" => "Opened a door",
],
(new Event())->setEmail('joe@acme.com')->setAction('Closed a door'),
];
$response = $drip->events()->batchStore($testData);
$this->assertFalse($response);

$response = $drip->events()->batchStore($testData);
$this->assertTrue($response);
}
}
45 changes: 45 additions & 0 deletions tests/Api/SubscribersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,49 @@ public function testStore()
$response = $drip->subscribers()->store($subscriber);
$this->assertTrue($response);
}

public function testBatchStore()
{
$mock = new MockHandler([
new Response(403, []),
new Response(201, []),
]);
$drip = new Drip($this->accountId, $this->apiToken, $this->userAgent);
$drip->setHandler($mock);
$testData = [
[
"email" => "john@acme.com",
"time_zone" => "America/Los_Angeles",
],
(new Subscriber())->setEmail('joe@acme.com')->setTimeZone('America/Los_Angeles'),
];

$response = $drip->subscribers()->batchStore($testData);
$this->assertFalse($response);

$response = $drip->subscribers()->batchStore($testData);
$this->assertTrue($response);
}

public function testBatchUnsubscribe()
{
$mock = new MockHandler([
new Response(403, []),
new Response(204, []),
]);
$drip = new Drip($this->accountId, $this->apiToken, $this->userAgent);
$drip->setHandler($mock);
$testData = [
[
"email" => "john@acme.com",
],
(new Subscriber())->setEmail('joe@acme.com'),
];

$response = $drip->subscribers()->batchUnsubscribe($testData);
$this->assertFalse($response);

$response = $drip->subscribers()->batchUnsubscribe($testData);
$this->assertTrue($response);
}
}

0 comments on commit 85d7cc5

Please sign in to comment.