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

Events api #12

Merged
merged 2 commits into from
Jan 14, 2021
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ 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)

### 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)
Expand Down
142 changes: 142 additions & 0 deletions src/Courier.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,148 @@ 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)
);
}

/**
* 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.
*
Expand Down
87 changes: 87 additions & 0 deletions tests/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,91 @@ 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);
}

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);
}
}