Skip to content

Commit 196b69a

Browse files
committed
Add ability to set the Intercom API region by injecting the base URI
1 parent 4ef6157 commit 196b69a

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ For more information about API Versioning, please check the [API Versioning Docu
9898

9999
**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.
100100

101+
## Regional Endpoint
102+
103+
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).
104+
105+
```php
106+
use Intercom\IntercomClient;
107+
108+
$client = new IntercomClient(
109+
'<insert_token_here>',
110+
null,
111+
[],
112+
IntercomClient::BASE_URI_EU
113+
);
114+
```
115+
101116
## Contacts
102117

103118
This resource is only available in API Versions 2.0 and above

src/IntercomClient.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ class IntercomClient
2323
{
2424
const SDK_VERSION = '4.4.0';
2525

26+
const BASE_URI_US = 'https://api.intercom.io';
27+
const BASE_URI_EU = 'https://api.eu.intercom.io';
28+
const BASE_URI_AU = 'https://api.au.intercom.io';
29+
30+
/**
31+
* @var string
32+
*/
33+
private $baseUri;
34+
2635
/**
2736
* @var HttpClient $httpClient
2837
*/
@@ -139,9 +148,14 @@ class IntercomClient
139148
* @param string $appIdOrToken App ID.
140149
* @param string|null $password Api Key.
141150
* @param array $extraRequestHeaders Extra request headers to be sent in every api request
142-
*/
143-
public function __construct(string $appIdOrToken, string $password = null, array $extraRequestHeaders = [])
144-
{
151+
* @param string $baseUri The Intercom API base URI
152+
*/
153+
public function __construct(
154+
string $appIdOrToken,
155+
string $password = null,
156+
array $extraRequestHeaders = [],
157+
string $baseUri = self::BASE_URI_US,
158+
) {
145159
$this->users = new IntercomUsers($this);
146160
$this->contacts = new IntercomContacts($this);
147161
$this->events = new IntercomEvents($this);
@@ -158,6 +172,7 @@ public function __construct(string $appIdOrToken, string $password = null, array
158172
$this->notes = new IntercomNotes($this);
159173
$this->teams = new IntercomTeams($this);
160174

175+
$this->baseUri = rtrim($baseUri, '/');
161176
$this->appIdOrToken = $appIdOrToken;
162177
$this->passwordPart = $password;
163178
$this->extraRequestHeaders = $extraRequestHeaders;
@@ -206,7 +221,7 @@ public function setUriFactory(UriFactory $uriFactory)
206221
*/
207222
public function post($endpoint, $json)
208223
{
209-
$response = $this->sendRequest('POST', "https://api.intercom.io/$endpoint", $json);
224+
$response = $this->sendRequest('POST', "{$this->baseUri}/$endpoint", $json);
210225
return $this->handleResponse($response);
211226
}
212227

@@ -219,7 +234,7 @@ public function post($endpoint, $json)
219234
*/
220235
public function put($endpoint, $json)
221236
{
222-
$response = $this->sendRequest('PUT', "https://api.intercom.io/$endpoint", $json);
237+
$response = $this->sendRequest('PUT', "{$this->baseUri}/$endpoint", $json);
223238
return $this->handleResponse($response);
224239
}
225240

@@ -232,7 +247,7 @@ public function put($endpoint, $json)
232247
*/
233248
public function delete($endpoint, $json)
234249
{
235-
$response = $this->sendRequest('DELETE', "https://api.intercom.io/$endpoint", $json);
250+
$response = $this->sendRequest('DELETE', "{$this->baseUri}/$endpoint", $json);
236251
return $this->handleResponse($response);
237252
}
238253

@@ -245,7 +260,7 @@ public function delete($endpoint, $json)
245260
*/
246261
public function get($endpoint, $queryParams = [])
247262
{
248-
$uri = $this->uriFactory->createUri("https://api.intercom.io/$endpoint");
263+
$uri = $this->uriFactory->createUri("{$this->baseUri}/$endpoint");
249264
if (!empty($queryParams)) {
250265
$uri = $uri->withQuery(http_build_query($queryParams));
251266
}

tests/IntercomClientTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function testBasicClient()
3737
foreach ($httpClient->getRequests() as $request) {
3838
$basic = $request->getHeaders()['Authorization'][0];
3939
$this->assertSame("Basic dTpw", $basic);
40+
$this->assertEquals('api.intercom.io', $request->getUri()->getHost());
4041
}
4142
}
4243

@@ -62,6 +63,27 @@ public function testClientWithExtraHeaders()
6263
}
6364
}
6465

66+
public function testClientWithDifferentBaseUri()
67+
{
68+
$httpClient = new Client();
69+
$httpClient->addResponse(
70+
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}")
71+
);
72+
73+
$client = new IntercomClient('u', 'p', [], 'https://example.com//');
74+
$client->setHttpClient($httpClient);
75+
76+
$client->users->create([
77+
'email' => '[email protected]'
78+
]);
79+
80+
foreach ($httpClient->getRequests() as $request) {
81+
$basic = $request->getHeaders()['Authorization'][0];
82+
$this->assertSame("Basic dTpw", $basic);
83+
$this->assertEquals('example.com', $request->getUri()->getHost());
84+
}
85+
}
86+
6587
public function testClientErrorHandling()
6688
{
6789
$httpClient = new Client();

0 commit comments

Comments
 (0)