From c10799c3e55eeb38258d0ce43d2ed49390ca502a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Krzaczkowski?= Date: Fri, 17 Jan 2025 17:09:12 +0100 Subject: [PATCH 1/4] feat: add track v2 support --- src/Client.php | 4 ++++ src/Endpoint/Track.php | 41 ++++++++++++++++++++++++++++++++++ src/Region/RegionEu.php | 4 ++-- src/Region/RegionInterface.php | 2 +- src/Region/RegionUs.php | 4 ++-- 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/Endpoint/Track.php diff --git a/src/Client.php b/src/Client.php index 959617e..790693a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -70,6 +70,9 @@ class Client /** @var Endpoint\Collections */ public $collection; + /** @var Endpoint\Track */ + public $track; + /** * Client constructor. * @param string $apiKey Api Key @@ -92,6 +95,7 @@ public function __construct(string $apiKey, string $siteId, array $options = []) $this->senderIdentities = new Endpoint\SenderIdentities($this); $this->send = new Endpoint\Send($this); $this->collection = new Endpoint\Collections($this); + $this->track = new Endpoint\Track($this); $this->apiKey = $apiKey; $this->siteId = $siteId; diff --git a/src/Endpoint/Track.php b/src/Endpoint/Track.php new file mode 100644 index 0000000..302e4da --- /dev/null +++ b/src/Endpoint/Track.php @@ -0,0 +1,41 @@ +mockException('Operation type is required!', 'POST'); + } // @codeCoverageIgnore + + if (!isset($options['action'])) { + $this->mockException('An event action is required!', 'POST'); + } // @codeCoverageIgnore + + if (!isset($options['identifiers'])) { + $this->mockException('Object identifiers is required!', 'POST'); + } // @codeCoverageIgnore + + $path = $this->generatePath('entity'); + $options['endpoint'] = $this->client->getRegion()->trackUri('v2'); + + return $this->client->post($path, $options); + + } + + public function batch(array $options) + { + if (!isset($options['batch'])) { + $this->mockException('Batch paremeter is required!', 'POST'); + } // @codeCoverageIgnore + + $path = $this->generatePath('batch'); + $options['endpoint'] = $this->client->getRegion()->trackUri('v2'); + + return $this->client->post($path, $options); + } +} diff --git a/src/Region/RegionEu.php b/src/Region/RegionEu.php index 86c1dc3..4ab90df 100644 --- a/src/Region/RegionEu.php +++ b/src/Region/RegionEu.php @@ -6,9 +6,9 @@ class RegionEu implements RegionInterface { - public function trackUri(): string + public function trackUri(string $version = 'v1'): string { - return 'https://track-eu.customer.io/api/v1/'; + return 'https://track-eu.customer.io/api/'.$version.'/'; } public function apiUri(): string diff --git a/src/Region/RegionInterface.php b/src/Region/RegionInterface.php index 745148b..c36a254 100644 --- a/src/Region/RegionInterface.php +++ b/src/Region/RegionInterface.php @@ -11,7 +11,7 @@ interface RegionInterface * * @return string */ - public function trackUri(): string; + public function trackUri(string $version = 'v1'): string; /** * API diff --git a/src/Region/RegionUs.php b/src/Region/RegionUs.php index cf196ee..54846eb 100644 --- a/src/Region/RegionUs.php +++ b/src/Region/RegionUs.php @@ -6,9 +6,9 @@ class RegionUs implements RegionInterface { - public function trackUri(): string + public function trackUri(string $version = 'v1'): string { - return 'https://track.customer.io/api/v1/'; + return 'https://track.customer.io/api/'.$version.'/'; } public function apiUri(): string From ce8abe37842c58065e53d7e3e25affac2e263958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Krzaczkowski?= Date: Fri, 17 Jan 2025 17:11:38 +0100 Subject: [PATCH 2/4] chore: code cleanup --- src/Endpoint/Track.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Endpoint/Track.php b/src/Endpoint/Track.php index 302e4da..0587431 100644 --- a/src/Endpoint/Track.php +++ b/src/Endpoint/Track.php @@ -13,7 +13,7 @@ public function entity(array $options) } // @codeCoverageIgnore if (!isset($options['action'])) { - $this->mockException('An event action is required!', 'POST'); + $this->mockException('An event action is required!', 'POST'); } // @codeCoverageIgnore if (!isset($options['identifiers'])) { @@ -24,7 +24,6 @@ public function entity(array $options) $options['endpoint'] = $this->client->getRegion()->trackUri('v2'); return $this->client->post($path, $options); - } public function batch(array $options) From a214fe7e15bb1d74895d52ef0dfc33054dab60ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Krzaczkowski?= Date: Fri, 17 Jan 2025 17:14:10 +0100 Subject: [PATCH 3/4] feat: update descriptions --- src/Endpoint/Track.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Endpoint/Track.php b/src/Endpoint/Track.php index 0587431..a3b1365 100644 --- a/src/Endpoint/Track.php +++ b/src/Endpoint/Track.php @@ -6,6 +6,13 @@ class Track extends Base { + /** + * Make a single request + * @see https://docs.customer.io/api/track/#operation/entity + * @param array $options + * @return mixed + * @throws GuzzleException + */ public function entity(array $options) { if (!isset($options['type'])) { @@ -26,6 +33,13 @@ public function entity(array $options) return $this->client->post($path, $options); } + /** + * Send multiple requests + * @see https://docs.customer.io/api/track/#operation/batch + * @param array $options + * @return mixed + * @throws GuzzleException + */ public function batch(array $options) { if (!isset($options['batch'])) { From e27e6c8c8f93b9a23fd96f957120886a9c961f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Krzaczkowski?= Date: Fri, 17 Jan 2025 17:38:21 +0100 Subject: [PATCH 4/4] fix: add basic tests --- tests/TrackTest.php | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/TrackTest.php diff --git a/tests/TrackTest.php b/tests/TrackTest.php new file mode 100644 index 0000000..6dbc429 --- /dev/null +++ b/tests/TrackTest.php @@ -0,0 +1,49 @@ +getMockBuilder('Customerio\Client')->disableOriginalConstructor()->getMock(); + $stub->method('post')->willReturn('foo'); + $events = new Track($stub); + $this->assertEquals('foo', $events->entity([ + 'type' => 'person', + 'identifiers' => [ + 'id' => '12345', + ], + 'action' => 'event', + 'name' => 'test new event', + ])); + } + + public function testBatchEntity() + { + $stub = $this->getMockBuilder('Customerio\Client')->disableOriginalConstructor()->getMock(); + $stub->method('post')->willReturn('foo'); + $events = new Track($stub); + $this->assertEquals('foo', $events->batch( + ["batch" => [[ + 'type' => 'person', + 'identifiers' => [ + 'id' => 'ziT8aeAcSdYBKvYCzx', + ], + 'action' => 'event', + 'name' => 'test new event', + ], + [ + 'type' => 'person', + 'identifiers' => [ + 'id' => 'ziT8aeAcSdYBKvYCzx', + ], + 'action' => 'event', + 'name' => 'test new event 2', + ]]] + )); + } +}