Skip to content

Commit

Permalink
feat: implement collections endpoint (#44) (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzaczek authored Aug 6, 2021
1 parent c595ee4 commit 80ad77d
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class Client
/** @var Endpoint\Send */
public $send;

/** @var Endpoint\Collections */
public $collection;

/**
* Client constructor.
* @param string $apiKey Api Key
Expand All @@ -88,6 +91,7 @@ public function __construct(string $apiKey, string $siteId, array $options = [])
$this->activities = new Endpoint\Activities($this);
$this->senderIdentities = new Endpoint\SenderIdentities($this);
$this->send = new Endpoint\Send($this);
$this->collection = new Endpoint\Collections($this);

$this->apiKey = $apiKey;
$this->siteId = $siteId;
Expand Down Expand Up @@ -171,6 +175,10 @@ public function get(string $endpoint, array $params = [])

$response = $this->httpClient->request('GET', $apiEndpoint.$endpoint, $options);

if (isset($params['raw'])) {
return (string)$response->getBody();
}

return $this->handleResponse($response);
}

Expand Down Expand Up @@ -235,7 +243,9 @@ protected function request(string $method, string $path, array $json): ResponseI
$options = $this->getDefaultParams($apiEndpoint);
$url = $apiEndpoint.$path;

$options['json'] = $json;
if (!empty($json)) {
$options['json'] = $json;
}

return $this->httpClient->request($method, $url, $options);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Endpoint/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ protected function activitiesPath($id = null, array $extra = []): string
return $this->generatePath('activities', $id, $extra);
}

/**
* @param int|null $id
* @param array $extras
* @return string
*/
protected function collectionsPath(?int $id = null, array $extras = []): string
{
return $this->generatePath('collections', $id, $extras);
}

/**
* @param null $id
* @param array $extra
Expand Down
149 changes: 149 additions & 0 deletions src/Endpoint/Collections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Customerio\Endpoint;

use GuzzleHttp\Exception\GuzzleException;

class Collections extends Base
{

/**
* List collections
* @see https://customer.io/docs/api/#operation/getCollections
* @param array $options
* @return mixed
* @throws GuzzleException
*/
public function search(array $options)
{
$path = $this->collectionsPath();

return $this->client->get($path, $options);
}

/**
* Get collection
* @see https://customer.io/docs/api/#operation/getCollection
* @param array $options
* @return mixed
* @throws GuzzleException
*/
public function get(array $options)
{
if (!isset($options['id'])) {
$this->mockException('Collection id is required!', 'GET');
} // @codeCoverageIgnore

$path = $this->collectionsPath($options['id']);
unset($options['id']);

return $this->client->get($path, $options);
}

/**
* Create collection
* @see https://customer.io/docs/api/#operation/addCollection
* @param array $options
* @return mixed
* @throws GuzzleException
*/
public function create(array $options)
{
if (!isset($options['name'])) {
$this->mockException('Collection name is required!', 'POST');
}

if (!isset($options['data']) && !isset($options['url'])) {
$this->mockException('Collection data or url is required!', 'POST');
} // @codeCoverageIgnore

$path = $this->collectionsPath();
$options['endpoint'] = $this->client->getRegion()->betaUri();

return $this->client->post($path, $options);
}

/**
* Delete a collection
* @see https://customer.io/docs/api/#operation/deleteCollection
* @param array $options
* @return mixed
* @throws GuzzleException
*/
public function delete(array $options)
{
if (!isset($options['collection_id'])) {
$this->mockException('Collection collection_id is required!', 'POST');
}

$path = $this->collectionsPath($options['collection_id']);
unset($options['collection_id']);
$options['endpoint'] = $this->client->getRegion()->betaUri();

return $this->client->delete($path, $options);
}

/**
* Update a collection name or data
* @see https://customer.io/docs/api/#operation/updateCollection
* @param array $options
* @return mixed
* @throws GuzzleException
*/
public function update(array $options)
{
if (!isset($options['collection_id'])) {
$this->mockException('Collection collection_id is required!', 'POST');
}

$path = $this->collectionsPath($options['collection_id']);
unset($options['collection_id']);
$options['endpoint'] = $this->client->getRegion()->betaUri();

return $this->client->put($path, $options);
}

/**
* Get collection content
* @see https://customer.io/docs/api/#operation/getCollectionContents
* @param array $options
* @return mixed
* @throws GuzzleException
*/
public function content(array $options)
{
if (!isset($options['collection_id'])) {
$this->mockException('Collection collection_id is required!', 'POST');
}

$path = $this->collectionsPath($options['collection_id'], ['content']);
unset($options['collection_id']);
$options['raw'] = true;

return $this->client->get($path, $options);
}

/**
* Update collection data
* @see https://customer.io/docs/api/#operation/updateCollectionContents
* @param array $options
* @return mixed
* @throws GuzzleException
*/
public function updateContent(array $options)
{
if (!isset($options['collection_id'])) {
$this->mockException('Collection collection_id is required!', 'POST');
}

if (!isset($options['data'])) {
$this->mockException('Collection data is required!', 'POST');
} // @codeCoverageIgnore

$path = $this->collectionsPath($options['collection_id'], ['content']);
$options = $options['data'];
$options['endpoint'] = $this->client->getRegion()->betaUri();

return $this->client->put($path, $options);
}
}
4 changes: 4 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testBasicClientUs()
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}"),
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}"),
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}"),
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}"),
]);
$container = [];
$history = Middleware::history($container);
Expand All @@ -35,6 +36,9 @@ public function testBasicClientUs()
$client->customers->get([
'email' => '[email protected]',
]);
$client->collection->content([
'collection_id' => 1,
]);
$client->customers->add([
'id' => 10,
'email' => '[email protected]',
Expand Down
Loading

0 comments on commit 80ad77d

Please sign in to comment.