From fc0df4d795713d995a953ae14f3e391700a2a6a8 Mon Sep 17 00:00:00 2001 From: Joris Berthelot Date: Fri, 28 Oct 2022 11:51:19 +0200 Subject: [PATCH] Add ability to set the Intercom API region by injecting the base URI --- README.md | 15 +++++++++++++++ src/IntercomClient.php | 29 ++++++++++++++++++++++------- tests/IntercomClientTest.php | 22 ++++++++++++++++++++++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3fd8ad3..20cb462 100644 --- a/README.md +++ b/README.md @@ -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( + '', + null, + [], + IntercomClient::BASE_URI_EU +); +``` + ## Contacts This resource is only available in API Versions 2.0 and above diff --git a/src/IntercomClient.php b/src/IntercomClient.php index 076f9ed..0401cb0 100644 --- a/src/IntercomClient.php +++ b/src/IntercomClient.php @@ -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 */ @@ -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); @@ -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; @@ -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); } @@ -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); } @@ -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); } @@ -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)); } diff --git a/tests/IntercomClientTest.php b/tests/IntercomClientTest.php index c04abac..0a80f54 100644 --- a/tests/IntercomClientTest.php +++ b/tests/IntercomClientTest.php @@ -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()); } } @@ -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' => 'test@intercom.io' + ]); + + 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();