Skip to content

Commit

Permalink
feat: add track v2 support (#64)
Browse files Browse the repository at this point in the history
* feat: add track v2 support

* chore: code cleanup

* feat: update descriptions

* fix: add basic tests
  • Loading branch information
krzaczek authored Jan 21, 2025
1 parent 09666dc commit bea54ff
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class Client
/** @var Endpoint\Collections */
public $collection;

/** @var Endpoint\Track */
public $track;

/**
* Client constructor.
* @param string $apiKey Api Key
Expand All @@ -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;
Expand Down
54 changes: 54 additions & 0 deletions src/Endpoint/Track.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Customerio\Endpoint;

use GuzzleHttp\Exception\GuzzleException;

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'])) {
$this->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);
}

/**
* 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'])) {
$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);
}
}
4 changes: 2 additions & 2 deletions src/Region/RegionEu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Region/RegionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface RegionInterface
*
* @return string
*/
public function trackUri(): string;
public function trackUri(string $version = 'v1'): string;

/**
* API
Expand Down
4 changes: 2 additions & 2 deletions src/Region/RegionUs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions tests/TrackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Customerio\Tests;

use Customerio\Endpoint\Track;
use PHPUnit\Framework\TestCase;

class TrackTest extends TestCase
{
public function testSingleEntity()
{
$stub = $this->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',
]]]
));
}
}

0 comments on commit bea54ff

Please sign in to comment.