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

Add ability to set the Intercom API region by injecting the base URI #356

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ For more information about API Versioning, please check the [API Versioning Docu

**Important**: Not all the resources supported by this API are supported by all API versions. See the notes below or the [API Reference](https://developers.intercom.com/intercom-api-reference/reference) for more information about the resources supported by each API version.

## Regional Endpoint

This SDK allows to work in a different region than the US. Intercom provides [different endpoints](https://developers.intercom.com/building-apps/docs/rest-apis#available-endpoints).

```php
use Intercom\IntercomClient;

$client = new IntercomClient(
'<insert_token_here>',
null,
[],
IntercomClient::BASE_URI_EU
);
```

## Contacts

This resource is only available in API Versions 2.0 and above
Expand Down
29 changes: 22 additions & 7 deletions src/IntercomClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class IntercomClient
{
const SDK_VERSION = '4.4.0';

const BASE_URI_US = 'https://api.intercom.io';
const BASE_URI_EU = 'https://api.eu.intercom.io';
const BASE_URI_AU = 'https://api.au.intercom.io';

/**
* @var string
*/
private $baseUri;

/**
* @var HttpClient $httpClient
*/
Expand Down Expand Up @@ -139,9 +148,14 @@ class IntercomClient
* @param string $appIdOrToken App ID.
* @param string|null $password Api Key.
* @param array $extraRequestHeaders Extra request headers to be sent in every api request
*/
public function __construct(string $appIdOrToken, string $password = null, array $extraRequestHeaders = [])
{
* @param string $baseUri The Intercom API base URI
*/
public function __construct(
string $appIdOrToken,
string $password = null,
array $extraRequestHeaders = [],
string $baseUri = self::BASE_URI_US
) {
$this->users = new IntercomUsers($this);
$this->contacts = new IntercomContacts($this);
$this->events = new IntercomEvents($this);
Expand All @@ -158,6 +172,7 @@ public function __construct(string $appIdOrToken, string $password = null, array
$this->notes = new IntercomNotes($this);
$this->teams = new IntercomTeams($this);

$this->baseUri = rtrim($baseUri, '/');
$this->appIdOrToken = $appIdOrToken;
$this->passwordPart = $password;
$this->extraRequestHeaders = $extraRequestHeaders;
Expand Down Expand Up @@ -206,7 +221,7 @@ public function setUriFactory(UriFactory $uriFactory)
*/
public function post($endpoint, $json)
{
$response = $this->sendRequest('POST', "https://api.intercom.io/$endpoint", $json);
$response = $this->sendRequest('POST', "{$this->baseUri}/$endpoint", $json);
return $this->handleResponse($response);
}

Expand All @@ -219,7 +234,7 @@ public function post($endpoint, $json)
*/
public function put($endpoint, $json)
{
$response = $this->sendRequest('PUT', "https://api.intercom.io/$endpoint", $json);
$response = $this->sendRequest('PUT', "{$this->baseUri}/$endpoint", $json);
return $this->handleResponse($response);
}

Expand All @@ -232,7 +247,7 @@ public function put($endpoint, $json)
*/
public function delete($endpoint, $json)
{
$response = $this->sendRequest('DELETE', "https://api.intercom.io/$endpoint", $json);
$response = $this->sendRequest('DELETE', "{$this->baseUri}/$endpoint", $json);
return $this->handleResponse($response);
}

Expand All @@ -245,7 +260,7 @@ public function delete($endpoint, $json)
*/
public function get($endpoint, $queryParams = [])
{
$uri = $this->uriFactory->createUri("https://api.intercom.io/$endpoint");
$uri = $this->uriFactory->createUri("{$this->baseUri}/$endpoint");
if (!empty($queryParams)) {
$uri = $uri->withQuery(http_build_query($queryParams));
}
Expand Down
22 changes: 22 additions & 0 deletions tests/IntercomClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testBasicClient()
foreach ($httpClient->getRequests() as $request) {
$basic = $request->getHeaders()['Authorization'][0];
$this->assertSame("Basic dTpw", $basic);
$this->assertEquals('api.intercom.io', $request->getUri()->getHost());
}
}

Expand All @@ -62,6 +63,27 @@ public function testClientWithExtraHeaders()
}
}

public function testClientWithDifferentBaseUri()
{
$httpClient = new Client();
$httpClient->addResponse(
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}")
);

$client = new IntercomClient('u', 'p', [], 'https://example.com//');
$client->setHttpClient($httpClient);

$client->users->create([
'email' => '[email protected]'
]);

foreach ($httpClient->getRequests() as $request) {
$basic = $request->getHeaders()['Authorization'][0];
$this->assertSame("Basic dTpw", $basic);
$this->assertEquals('example.com', $request->getUri()->getHost());
}
}

public function testClientErrorHandling()
{
$httpClient = new Client();
Expand Down