Skip to content

Commit 15044db

Browse files
authored
Lucianocn master (#232)
* improved .gitignore * added extraGuzzleRequestsOptions (optional) to IntercomClient * avoid $extraGuzzleRequestsOptions errors * typo fixes * Adding unit test for PR #206 and fixing unit test from previous PR * Removing misc print and setting test timezone to UTC * Fixing issue with CI test failing
1 parent 2eeae7c commit 15044db

File tree

3 files changed

+78
-15
lines changed

3 files changed

+78
-15
lines changed

.gitignore

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
build/
2-
composer.lock
3-
vendor/*
4-
*.swp
1+
# OSX
2+
.DS_Store
3+
4+
# PhpStorm
5+
.idea
6+
7+
# Composer
58
composer.phar
9+
/vendor
10+
11+
# PhpUnit
12+
phpunit.phar
13+
/phpunit.xml
14+
15+
# Extras
16+
build/
17+
*.swp

src/IntercomClient.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class IntercomClient
1818
/** @var string API password authentication */
1919
protected $passwordPart;
2020

21+
/** @var string Extra Guzzle Requests Options */
22+
protected $extraGuzzleRequestsOptions;
23+
2124
/** @var IntercomUsers $users */
2225
public $users;
2326

@@ -62,7 +65,7 @@ class IntercomClient
6265
* @param string $usernamePart App ID.
6366
* @param string $passwordPart Api Key.
6467
*/
65-
public function __construct($usernamePart, $passwordPart)
68+
public function __construct($usernamePart, $passwordPart, $extraGuzzleRequestsOptions = [])
6669
{
6770
$this->setDefaultClient();
6871
$this->users = new IntercomUsers($this);
@@ -81,6 +84,7 @@ public function __construct($usernamePart, $passwordPart)
8184

8285
$this->usernamePart = $usernamePart;
8386
$this->passwordPart = $passwordPart;
87+
$this->extraGuzzleRequestsOptions = $extraGuzzleRequestsOptions;
8488
}
8589

8690
private function setDefaultClient()
@@ -106,13 +110,14 @@ public function setClient($client)
106110
*/
107111
public function post($endpoint, $json)
108112
{
109-
$response = $this->http_client->request('POST', "https://api.intercom.io/$endpoint", [
113+
$guzzleRequestOptions = $this->getGuzzleRequestOptions([
110114
'json' => $json,
111115
'auth' => $this->getAuth(),
112116
'headers' => [
113117
'Accept' => 'application/json'
114-
]
118+
],
115119
]);
120+
$response = $this->http_client->request('POST', "https://api.intercom.io/$endpoint", $guzzleRequestOptions);
116121
return $this->handleResponse($response);
117122
}
118123

@@ -125,13 +130,15 @@ public function post($endpoint, $json)
125130
*/
126131
public function put($endpoint, $json)
127132
{
128-
$response = $this->http_client->request('PUT', "https://api.intercom.io/$endpoint", [
133+
$guzzleRequestOptions = $this->getGuzzleRequestOptions([
129134
'json' => $json,
130135
'auth' => $this->getAuth(),
131136
'headers' => [
132137
'Accept' => 'application/json'
133-
]
138+
],
134139
]);
140+
141+
$response = $this->http_client->request('PUT', "https://api.intercom.io/$endpoint", $guzzleRequestOptions);
135142
return $this->handleResponse($response);
136143
}
137144

@@ -144,13 +151,15 @@ public function put($endpoint, $json)
144151
*/
145152
public function delete($endpoint, $json)
146153
{
147-
$response = $this->http_client->request('DELETE', "https://api.intercom.io/$endpoint", [
154+
$guzzleRequestOptions = $this->getGuzzleRequestOptions([
148155
'json' => $json,
149156
'auth' => $this->getAuth(),
150157
'headers' => [
151158
'Accept' => 'application/json'
152-
]
159+
],
153160
]);
161+
162+
$response = $this->http_client->request('DELETE', "https://api.intercom.io/$endpoint", $guzzleRequestOptions);
154163
return $this->handleResponse($response);
155164
}
156165

@@ -162,13 +171,15 @@ public function delete($endpoint, $json)
162171
*/
163172
public function get($endpoint, $query)
164173
{
165-
$response = $this->http_client->request('GET', "https://api.intercom.io/$endpoint", [
174+
$guzzleRequestOptions = $this->getGuzzleRequestOptions([
166175
'query' => $query,
167176
'auth' => $this->getAuth(),
168177
'headers' => [
169178
'Accept' => 'application/json'
170-
]
179+
],
171180
]);
181+
182+
$response = $this->http_client->request('GET', "https://api.intercom.io/$endpoint", $guzzleRequestOptions);
172183
return $this->handleResponse($response);
173184
}
174185

@@ -180,15 +191,27 @@ public function get($endpoint, $query)
180191
*/
181192
public function nextPage($pages)
182193
{
183-
$response = $this->http_client->request('GET', $pages->next, [
194+
$guzzleRequestOptions = $this->getGuzzleRequestOptions([
184195
'auth' => $this->getAuth(),
185196
'headers' => [
186197
'Accept' => 'application/json'
187-
]
198+
],
188199
]);
200+
201+
$response = $this->http_client->request('GET', $pages->next, $guzzleRequestOptions);
189202
return $this->handleResponse($response);
190203
}
191204

205+
/**
206+
* Returns Guzzle Requests Options Array
207+
* @param array $defaultGuzzleRequestsOptions
208+
* @return array
209+
*/
210+
public function getGuzzleRequestOptions($defaultGuzzleRequestOptions = [])
211+
{
212+
return array_replace_recursive($this->extraGuzzleRequestsOptions, $defaultGuzzleRequestOptions);
213+
}
214+
192215
/**
193216
* Returns authentication parameters.
194217
* @return array

test/IntercomClientTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,33 @@ public function testBasicClient()
3434
}
3535
}
3636

37+
public function testExtendedClient()
38+
{
39+
$mock = new MockHandler([
40+
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}")
41+
]);
42+
43+
$container = [];
44+
$history = Middleware::history($container);
45+
$stack = HandlerStack::create($mock);
46+
$stack->push($history);
47+
48+
$http_client = new Client(['handler' => $stack]);
49+
50+
$client = new IntercomClient('u', 'p', ['connect_timeout' => 10]);
51+
$client->setClient($http_client);
52+
53+
$client->users->create([
54+
'email' => '[email protected]'
55+
]);
56+
57+
foreach ($container as $transaction) {
58+
$basic = $client->getGuzzleRequestOptions()['connect_timeout'];
59+
$this->assertTrue($basic == 10);
60+
}
61+
}
62+
63+
3764
public function testPaginationHelper()
3865
{
3966
$mock = new MockHandler([
@@ -63,6 +90,7 @@ public function testPaginationHelper()
6390

6491
public function testRateLimitDetails()
6592
{
93+
date_default_timezone_set('UTC');
6694
$time = time() + 7;
6795
$mock = new MockHandler([
6896
new Response(200, ['X-RateLimit-Limit' => '83', 'X-RateLimit-Remaining' => '2', 'X-RateLimit-Reset' => $time], "{\"foo\":\"bar\"}")

0 commit comments

Comments
 (0)