Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for Send API enhanced features #31

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased][unreleased]

## [v1.6.0] - 2022-02-02

- Add support for enhanced Send API

## [v1.5.0] - 2022-01-26

- Add Bulk API
Expand All @@ -31,7 +35,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Rename src/laravel -> src/Laravel
- Bump version in `CourierClient`

[unreleased]: https://github.com/trycourier/courier-php/compare/v1.5.0...HEAD
[unreleased]: https://github.com/trycourier/courier-php/compare/v1.6.0...HEAD
[v1.6.0]: https://github.com/trycourier/courier-php/compare/v1.5.0...v1.6.0
[v1.5.0]: https://github.com/trycourier/courier-php/compare/v1.4.0...v1.5.0
[v1.4.0]: https://github.com/trycourier/courier-php/compare/v1.3.0...v1.4.0
[v1.3.0]: https://github.com/trycourier/courier-php/compare/v1.2.1...v1.3.0
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ For a full description of request and response payloads and properties, please s

### Send API

- `sendNotification(string $event, string $recipient, string $brand = NULL, object $profile = NULL, object $data = NULL, object $preferences = NULL, object $override = NULL, string $idempotency_key = NULL): object` [[?]](https://docs.courier.com/reference/send-api#sendmessage)
- `sendNotificationToList(string $event, string $list = NULL, string $pattern = NULL, string $brand = NULL, object $data = NULL, object $override = NULL, string $idempotency_key = NULL): object` [[?]](https://docs.courier.com/reference/send-api#sendlist)
- `sendNotification(string $event, string $recipient, string $brand = NULL, object $profile = NULL, object $data = NULL, object $preferences = NULL, object $override = NULL, string $idempotency_key = NULL): object` [(Send API)](https://www.courier.com/docs/reference/send/message/)
- `sendEnhancedNotification(object $message, string $idempotency_key = NULL): object` [(Send API)](https://www.courier.com/docs/reference/send/message/)
- `sendNotificationToList(string $event, string $list = NULL, string $pattern = NULL, string $brand = NULL, object $data = NULL, object $override = NULL, string $idempotency_key = NULL): object` [(Send list API)](https://www.courier.com/docs/reference/send/list/)

### Messages API

Expand Down
25 changes: 24 additions & 1 deletion src/CourierClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class CourierClient implements CourierClientInterface
/**
* @var string Library version, used for setting User-Agent
*/
private $version = '1.5.0';
private $version = '1.6.0';

/**
* Courier API base url.
Expand Down Expand Up @@ -236,6 +236,29 @@ public function sendNotification(string $event, string $recipient, string $brand
);
}

/**
* Send an enhanced notification
*
* @param object $message
* @param string|null $idempotency_key
* @return object
* @throws CourierRequestException
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function sendEnhancedNotification(object $message, string $idempotency_key = null): object
{
$params = array(
'message' => $message
);

$params = array_filter($params);

return $this->doRequest(
$idempotency_key ? $this->buildIdempotentRequest("post", "send", $params, $idempotency_key)
: $this->buildRequest("post", "send", $params)
);
}

/**
* Send a notification to list(s) subscribers
*
Expand Down
1 change: 1 addition & 0 deletions src/CourierClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface CourierClientInterface
{
public function setHttpClient(ClientInterface $clientInterface): void;
public function sendNotification(string $event, string $recipient, string $brand = null, object $profile = null, object $data = null, object $preferences = null, object $override = null, string $idempotency_key = null): object;
public function sendEnhancedNotification(object $message, string $idempotency_key = null): object;
public function sendNotificationToList(string $event, string $list = null, string $pattern = null, string $brand = null, object $data = null, object $override = null, string $idempotency_key = null): object;
public function getMessages(string $cursor = null, string $event = null, string $list = null, string $message_id = null, string $notification = null, string $recipient = null): object;
public function getMessage(string $message_id): object;
Expand Down
20 changes: 20 additions & 0 deletions tests/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ public function test_send_idempotent_notification()
$this->assertEquals("/send", $response->path);
}

public function test_send_enhanced_notification()
{
$message = (object) [];
$response = $this->getCourierClient()->sendEnhancedNotification($message, NULL);

$this->assertEquals("POST", $response->method);
$this->assertEquals("application/json", $response->content);
$this->assertEquals("/send", $response->path);
}

public function test_send_enhanced_idempotent_notification()
{
$message = (object) [];
$response = $this->getCourierClient()->sendEnhancedNotification($message, "idempotent");

$this->assertEquals("POST", $response->method);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to have a check to make sure buildIdempotentRequest was called and not buildRequest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather move fast on this change over super exhaustive testing of a deeper if condition. that being said if we know a quick way to spy and test it happy to add that but didn't think it was necessary

$this->assertEquals("application/json", $response->content);
$this->assertEquals("/send", $response->path);
}

public function test_send_notification_to_list()
{
$response = $this->getCourierClient()->sendNotificationToList("event", "myList");
Expand Down