Skip to content

Commit

Permalink
feat: support for EU region (#36) (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzaczek authored May 7, 2021
1 parent 36f0f31 commit 1b0019b
Show file tree
Hide file tree
Showing 22 changed files with 325 additions and 74 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

PHP bindings for the Customer.io API.

[API Documentation](https://learn.customer.io/api/)
[API Documentation](https://customer.io/docs/api/)

[![Build Status](https://travis-ci.org/printu/customerio.svg?branch=master)](https://travis-ci.org/printu/customerio)
[![Code Climate](https://codeclimate.com/github/printu/customerio/badges/gpa.svg)](https://codeclimate.com/github/printu/customerio)
Expand Down Expand Up @@ -72,6 +72,17 @@ $client->setAppAPIKey('APP_KEY');
?>
```

Change region to EU

```php
<?php
use Customerio\Client;

$client = new Client('YOUR_API_KEY', 'YOUR_SITE_ID', ['region' => 'eu']);

?>
```

### Local Testing

Run `phpunit` from the project root to start all tests.
Expand Down
41 changes: 31 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Customerio;

use Customerio\Region\RegionInterface;
use GuzzleHttp\Client as BaseClient;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Utils;
Expand All @@ -12,10 +13,6 @@

class Client
{
const API_ENDPOINT_TRACK = 'https://track.customer.io/api/v1/';
const API_ENDPOINT = 'https://api.customer.io/v1/';
const API_ENDPOINT_BETA = 'https://beta-api.customer.io/v1/api/';

/** @var BaseClient $httpClient */
private $httpClient;

Expand All @@ -28,6 +25,9 @@ class Client
/** @var string App API key */
protected $appKey;

/** @var RegionInterface */
protected $region;

/** @var bool Assoc mode for response */
protected $assocResponse;

Expand Down Expand Up @@ -71,8 +71,9 @@ class Client
* Client constructor.
* @param string $apiKey Api Key
* @param string $siteId Site ID.
* @param array $options client options
*/
public function __construct(string $apiKey, string $siteId)
public function __construct(string $apiKey, string $siteId, array $options = [])
{
$this->setDefaultClient();
$this->events = new Endpoint\Events($this);
Expand All @@ -91,6 +92,8 @@ public function __construct(string $apiKey, string $siteId)
$this->apiKey = $apiKey;
$this->siteId = $siteId;
$this->assocResponse = false;

$this->region = Region::factory($options['region'] ?? 'us');
}

/**
Expand All @@ -109,6 +112,22 @@ public function setSiteId(string $siteId): void
$this->siteId = $siteId;
}

/**
* @param string $region
*/
public function setRegion(string $region): void
{
$this->region = Region::factory($region);
}

/**
* @return RegionInterface
*/
public function getRegion(): RegionInterface
{
return $this->region;
}

/**
* @param bool $assoc
*/
Expand Down Expand Up @@ -143,12 +162,14 @@ public function setClient(BaseClient $client): void
*/
public function get(string $endpoint, array $params = [])
{
$options = $this->getDefaultParams(self::API_ENDPOINT_BETA);
$apiEndpoint = $this->getRegion()->betaUri();

$options = $this->getDefaultParams($apiEndpoint);
if (!empty($params)) {
$options['query'] = $params;
}

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

return $this->handleResponse($response);
}
Expand Down Expand Up @@ -204,7 +225,7 @@ public function put(string $endpoint, array $json)
*/
protected function request(string $method, string $path, array $json): ResponseInterface
{
$apiEndpoint = self::API_ENDPOINT_TRACK;
$apiEndpoint = $this->region->trackUri();

if (isset($json['endpoint'])) {
$apiEndpoint = $json['endpoint'];
Expand Down Expand Up @@ -259,8 +280,8 @@ private function handleResponse(ResponseInterface $response)
protected function getDefaultParams($endpoint): array
{
switch ($endpoint) {
case self::API_ENDPOINT_BETA:
case self::API_ENDPOINT:
case $this->region->apiUri():
case $this->region->betaUri():
return [
'headers' => [
'Authorization' => 'Bearer '.$this->getToken(),
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoint/Activities.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Customerio\Endpoint;

use Customerio\Client;
use GuzzleHttp\Exception\GuzzleException;

class Activities extends Base
{
/**
* Get data about activities
* @param array $options
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function search(array $options)
{
Expand Down
23 changes: 12 additions & 11 deletions src/Endpoint/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Customerio\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Client\RequestExceptionInterface;

class Base
{
Expand All @@ -25,7 +26,7 @@ public function __construct($client)
* @param array $extra
* @return string
*/
protected function customerPath($id = null, array $extra = [])
protected function customerPath($id = null, array $extra = []): string
{
return $this->generatePath('customers', $id, $extra);
}
Expand All @@ -35,7 +36,7 @@ protected function customerPath($id = null, array $extra = [])
* @param array $extra
* @return string
*/
protected function campaignPath($id = null, array $extra = [])
protected function campaignPath($id = null, array $extra = []): string
{
return $this->generatePath('campaigns', $id, $extra);
}
Expand All @@ -45,7 +46,7 @@ protected function campaignPath($id = null, array $extra = [])
* @param array $extra
* @return string
*/
protected function messagesPath($id = null, array $extra = [])
protected function messagesPath($id = null, array $extra = []): string
{
return $this->generatePath('messages', $id, $extra);
}
Expand All @@ -55,7 +56,7 @@ protected function messagesPath($id = null, array $extra = [])
* @param array $extra
* @return string
*/
protected function messagesTemplatesPath($id, array $extra = [])
protected function messagesTemplatesPath($id, array $extra = []): string
{
return $this->generatePath('msg_templates', $id, $extra);
}
Expand All @@ -65,7 +66,7 @@ protected function messagesTemplatesPath($id, array $extra = [])
* @param array $extra
* @return string
*/
protected function newslettersPath($id = null, array $extra = [])
protected function newslettersPath($id = null, array $extra = []): string
{
return $this->generatePath('newsletters', $id, $extra);
}
Expand All @@ -75,7 +76,7 @@ protected function newslettersPath($id = null, array $extra = [])
* @param array $extra
* @return string
*/
protected function segmentsPath($id = null, array $extra = [])
protected function segmentsPath($id = null, array $extra = []): string
{
return $this->generatePath('segments', $id, $extra);
}
Expand All @@ -85,7 +86,7 @@ protected function segmentsPath($id = null, array $extra = [])
* @param array $extra
* @return string
*/
protected function exportsPath($id = null, array $extra = [])
protected function exportsPath($id = null, array $extra = []): string
{
return $this->generatePath('exports', $id, $extra);
}
Expand All @@ -95,7 +96,7 @@ protected function exportsPath($id = null, array $extra = [])
* @param array $extra
* @return string
*/
protected function activitiesPath($id = null, array $extra = [])
protected function activitiesPath($id = null, array $extra = []): string
{
return $this->generatePath('activities', $id, $extra);
}
Expand All @@ -105,7 +106,7 @@ protected function activitiesPath($id = null, array $extra = [])
* @param array $extra
* @return string
*/
protected function senderIdentitiesPath($id = null, array $extra = [])
protected function senderIdentitiesPath($id = null, array $extra = []): string
{
return $this->generatePath('sender_identities', $id, $extra);
}
Expand All @@ -114,7 +115,7 @@ protected function senderIdentitiesPath($id = null, array $extra = [])
* @param $message
* @param $method
*/
protected function mockException($message, $method)
protected function mockException($message, $method): RequestExceptionInterface
{
throw new RequestException($message, (new Request($method, '/')));
}
Expand All @@ -125,7 +126,7 @@ protected function mockException($message, $method)
* @param array $extra
* @return string
*/
private function generatePath($prefix, $id = null, array $extra = [])
private function generatePath($prefix, $id = null, array $extra = []): string
{
$path = [
$prefix,
Expand Down
22 changes: 11 additions & 11 deletions src/Endpoint/Campaigns.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Customerio\Endpoint;

use Customerio\Client;
use GuzzleHttp\Exception\GuzzleException;

class Campaigns extends Base
{
Expand All @@ -11,7 +11,7 @@ class Campaigns extends Base
* @see https://learn.customer.io/api/#apibeta-apicampaignscampaigns_list
* @param array $options
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function search(array $options)
{
Expand All @@ -25,7 +25,7 @@ public function search(array $options)
* @see https://learn.customer.io/api/#apibeta-apicampaignscampaigns_get
* @param array $options
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function get(array $options)
{
Expand All @@ -45,7 +45,7 @@ public function get(array $options)
* @see https://learn.customer.io/api/#apibeta-apicampaignscampaigns_get_triggers_metrics
* @param array $options
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function metrics(array $options)
{
Expand All @@ -71,7 +71,7 @@ public function metrics(array $options)
* @see https://learn.customer.io/api/#apibeta-apicampaignscampaigns_get_triggers
* @param array $options
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function triggers(array $options)
{
Expand All @@ -91,7 +91,7 @@ public function triggers(array $options)
* @see https://learn.customer.io/documentation/api-triggered-data-format.html
* @param array $options
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function trigger(array $options)
{
Expand All @@ -102,7 +102,7 @@ public function trigger(array $options)
$path = $this->campaignPath($options['id'], ['triggers']);
unset($options['id']);

$options['endpoint'] = Client::API_ENDPOINT;
$options['endpoint'] = $this->client->getRegion()->apiUri();

return $this->client->post($path, $options);
}
Expand All @@ -112,7 +112,7 @@ public function trigger(array $options)
* @see https://learn.customer.io/api/#apibeta-apicampaignscampaigns_messages
* @param array $options
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function messages(array $options)
{
Expand All @@ -131,7 +131,7 @@ public function messages(array $options)
* @see https://customer.io/docs/api/#apibeta-apicampaignscampaigns_index_actions
* @param array $options
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function actions(array $options)
{
Expand All @@ -150,7 +150,7 @@ public function actions(array $options)
* @see https://customer.io/docs/api/#apibeta-apicampaignscampaigns_get_action
* @param array $options
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function getAction(array $options)
{
Expand All @@ -174,7 +174,7 @@ public function getAction(array $options)
* @see https://customer.io/docs/api/#apibeta-apicampaignscampaign_action_metrics
* @param array $options
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function getActionMetrics(array $options)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Endpoint/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Customerio\Endpoint;

use Customerio\Client;
use GuzzleHttp\Exception\GuzzleException;

class Customers extends Base
Expand Down Expand Up @@ -114,7 +113,7 @@ public function search(array $options)
} // @codeCoverageIgnore

$path = $this->customerPath();
$options['endpoint'] = Client::API_ENDPOINT_BETA;
$options['endpoint'] = $this->client->getRegion()->betaUri();

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

0 comments on commit 1b0019b

Please sign in to comment.