From 7800e5f304db3b7f693efd1638590036fa2e1cb3 Mon Sep 17 00:00:00 2001 From: Tejas Kumthekar Date: Thu, 14 Jan 2021 08:48:09 -0600 Subject: [PATCH] Add Events API --- README.md | 5 ++++ src/Courier.php | 49 +++++++++++++++++++++++++++++++++++++- tests/NotificationTest.php | 27 +++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e589f2..ce85623 100755 --- a/README.md +++ b/README.md @@ -75,6 +75,11 @@ For a full description of request and response payloads and properties, please s * ```replaceBrand(string $brand_id, string $name, object $settings, object $snippets = NULL): object``` [[?]](https://docs.courier.com/reference/brands-api#replacebrand) * ```deleteBrand(string $brand_id): object``` [[?]](https://docs.courier.com/reference/brands-api#deletebrand) +### Events API +* ```getEvents(): object``` [[?]](https://docs.courier.com/reference/events-api#getevents) +* ```getEvent(string $event_id): object``` [[?]](https://docs.courier.com/reference/events-api#geteventbyid) +* ```putEvent(string $event_id, string $id, string $type): object``` [[?]](https://docs.courier.com/reference/events-api#replaceeventbyid) + ### Profiles API * ```getProfile(string $recipient_id): object``` [[?]](https://docs.trycourier.com/reference#get-preferencesrecipient_id) diff --git a/src/Courier.php b/src/Courier.php index 653abec..9048745 100755 --- a/src/Courier.php +++ b/src/Courier.php @@ -168,7 +168,7 @@ private function doRequest(RequestInterface $request): object * @return RequestInterface|Request */ private function buildRequest(string $method, string $path, array $params = []): RequestInterface - { + { return new Request( $method, $this->base_url . $path, @@ -582,6 +582,53 @@ public function deleteBrand(string $brand_id): object ); } + /** + * Fetch the list of events + * @return object + * @throws CourierRequestException + */ + public function getEvents(): object + { + return $this->doRequest( + $this->buildRequest("get", "events") + ); + } + + /** + * Fetch a specific event by event ID. + * + * @param string $event_id + * @return object + * @throws CourierRequestException + */ + public function getEvent(string $event_id): object + { + return $this->doRequest( + $this->buildRequest("get", "events/" . $event_id) + ); + } + + /** + * Replace an existing event with the supplied values or create a new event if one does not already exist. + * + * @param string $event_id + * @param string $id + * @param string $type + * @return object + * @throws CourierRequestException + */ + public function putEvent(string $event_id, string $id, string $type): object + { + $params = array( + 'id' => $id, + 'type' => $type + ); + + return $this->doRequest( + $this->buildRequest("put", "events/" . $event_id, $params) + ); + } + /** * Get the profile stored under the specified recipient ID. * diff --git a/tests/NotificationTest.php b/tests/NotificationTest.php index 808d8a1..601885e 100755 --- a/tests/NotificationTest.php +++ b/tests/NotificationTest.php @@ -215,4 +215,31 @@ public function test_delete_brand() $this->assertEquals("application/json", $response->content); $this->assertEquals("/brands/brand001", $response->path); } + + public function test_get_events() + { + $response = $this->getCourierClient()->getEvents(); + + $this->assertEquals("GET", $response->method); + $this->assertEquals("application/json", $response->content); + $this->assertEquals("/events", $response->path); + } + + public function test_get_event() + { + $response = $this->getCourierClient()->getEvent("event001"); + + $this->assertEquals("GET", $response->method); + $this->assertEquals("application/json", $response->content); + $this->assertEquals("/events/event001", $response->path); + } + + public function test_put_event() + { + $response = $this->getCourierClient()->putEvent("event001", "notification001", "notification"); + + $this->assertEquals("PUT", $response->method); + $this->assertEquals("application/json", $response->content); + $this->assertEquals("/events/event001", $response->path); + } }