From 0f7480e996e42ca6fe7c5201fe6eb365215b5ed8 Mon Sep 17 00:00:00 2001 From: Tejas Kumthekar Date: Thu, 14 Jan 2021 08:31:07 -0600 Subject: [PATCH 1/2] Add Brands API --- README.md | 7 +++ src/Courier.php | 97 +++++++++++++++++++++++++++++++++++++- tests/NotificationTest.php | 60 +++++++++++++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a73cf0..2e589f2 100755 --- a/README.md +++ b/README.md @@ -68,6 +68,13 @@ For a full description of request and response payloads and properties, please s * ```subscribeRecipientToList(string $list_id, string $recipient_id): object``` [[?]](https://docs.courier.com/reference/lists-api#putlistsubscription) * ```deleteListSubscription(string $list_id, string $recipient_id): object``` [[?]](https://docs.courier.com/reference/lists-api#deletelistsubscription) +### Brands API +* ```getBrands(string $cursor = NULL): object``` [[?]](https://docs.courier.com/reference/brands-api#getbrands) +* ```createBrand(string $id = NULL, string $name, object $settings, object $snippets = NULL, string $idempotency_key = NULL): object``` [[?]](https://docs.courier.com/reference/brands-api#createbrand) +* ```getBrand(string $brand_id): object``` [[?]](https://docs.courier.com/reference/brands-api#getbrand) +* ```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) + ### 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 00c5989..653abec 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, @@ -487,6 +487,101 @@ public function deleteListSubscription(string $list_id, string $recipient_id): o ); } + /** + * Get the list of brands + * @param string|null $cursor + * @return object + * @throws CourierRequestException + */ + public function getBrands(string $cursor = NULL): object + { + $query_params = array( + 'cursor' => $cursor + ); + + return $this->doRequest( + $this->buildRequest("get", "brands?" . http_build_query($query_params, null, '&', PHP_QUERY_RFC3986)) + ); + } + + /** + * Create a new brand + * + * @param string|null $id + * @param string $name + * @param object $settings + * @param object|null $snippets + * @param string|null $idempotency_key + * @return object + * @throws CourierRequestException + * @throws \Psr\Http\Client\ClientExceptionInterface + */ + public function createBrand(string $id = NULL, string $name, object $settings, object $snippets = NULL, string $idempotency_key = NULL): object + { + $params = array( + 'id' => $id, + 'name' => $name, + 'settings' => $settings, + 'snippets' => $snippets + ); + + return $this->doRequest( + $idempotency_key ? $this->buildIdempotentRequest("post", "brands", $params, $idempotency_key) + : $this->buildRequest("post", "brands", $params) + ); + } + + /** + * Fetch a specific brand by brand ID. + * + * @param string $brand_id + * @return object + * @throws CourierRequestException + */ + public function getBrand(string $brand_id): object + { + return $this->doRequest( + $this->buildRequest("get", "brands/" . $brand_id) + ); + } + + /** + * Replace an existing brand with the supplied values. + * + * @param string $brand_id + * @param string $name + * @param object $settings + * @param object|null $snippets + * @return object + * @throws CourierRequestException + */ + public function replaceBrand(string $brand_id, string $name, object $settings, object $snippets = NULL): object + { + $params = array( + 'name' => $name, + 'settings' => $settings, + 'snippets' => $snippets + ); + + return $this->doRequest( + $this->buildRequest("put", "brands/" . $brand_id, $params) + ); + } + + /** + * Delete a brand by brand ID. + * + * @param string $brand_id + * @return object + * @throws CourierRequestException + */ + public function deleteBrand(string $brand_id): object + { + return $this->doRequest( + $this->buildRequest("delete", "brands/" . $brand_id) + ); + } + /** * Get the profile stored under the specified recipient ID. * diff --git a/tests/NotificationTest.php b/tests/NotificationTest.php index ad62ecb..808d8a1 100755 --- a/tests/NotificationTest.php +++ b/tests/NotificationTest.php @@ -155,4 +155,64 @@ public function test_delete_list_subscription() $this->assertEquals("application/json", $response->content); $this->assertEquals("/lists/list001/subscriptions/recipient001", $response->path); } + + public function test_get_brands() + { + $response = $this->getCourierClient()->getBrands(); + + $this->assertEquals("GET", $response->method); + $this->assertEquals("application/json", $response->content); + $this->assertEquals("/brands", $response->path); + } + + public function test_create_brand() + { + $settings = (object) []; + + $response = $this->getCourierClient()->createBrand(NULL, "myBrand", $settings); + + $this->assertEquals("POST", $response->method); + $this->assertEquals("application/json", $response->content); + $this->assertEquals("/brands", $response->path); + } + + public function test_create_idempotent_brand() + { + $settings = (object) []; + + $response = $this->getCourierClient()->createBrand(NULL, "myBrand", $settings, NULL, "idempotent"); + + $this->assertEquals("POST", $response->method); + $this->assertEquals("application/json", $response->content); + $this->assertEquals("/brands", $response->path); + } + + public function test_get_brand() + { + $response = $this->getCourierClient()->getBrand("brand001"); + + $this->assertEquals("GET", $response->method); + $this->assertEquals("application/json", $response->content); + $this->assertEquals("/brands/brand001", $response->path); + } + + public function test_replace_brand() + { + $settings = (object) []; + + $response = $this->getCourierClient()->replaceBrand("brand001", "myBrand", $settings); + + $this->assertEquals("PUT", $response->method); + $this->assertEquals("application/json", $response->content); + $this->assertEquals("/brands/brand001", $response->path); + } + + public function test_delete_brand() + { + $response = $this->getCourierClient()->deleteBrand("brand001"); + + $this->assertEquals("DELETE", $response->method); + $this->assertEquals("application/json", $response->content); + $this->assertEquals("/brands/brand001", $response->path); + } } From 7800e5f304db3b7f693efd1638590036fa2e1cb3 Mon Sep 17 00:00:00 2001 From: Tejas Kumthekar Date: Thu, 14 Jan 2021 08:48:09 -0600 Subject: [PATCH 2/2] 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); + } }