Skip to content

Commit e7dd6d1

Browse files
authored
Merge pull request #224 from asknicely/rate-limiter-header-access
Allow access to rate limiter headers
2 parents 7c47e22 + 84a78e2 commit e7dd6d1

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/IntercomClient.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class IntercomClient
5555
/** @var IntercomNotes $notes */
5656
public $notes;
5757

58+
/** @var int[] $rateLimitDetails */
59+
protected $rateLimitDetails = [];
60+
5861
/**
5962
* IntercomClient constructor.
6063
* @param string $usernamePart App ID.
@@ -202,8 +205,37 @@ public function getAuth()
202205
*/
203206
private function handleResponse(Response $response)
204207
{
208+
$this->setRateLimitDetails($response);
209+
205210
$stream = stream_for($response->getBody());
206211
$data = json_decode($stream);
207212
return $data;
208213
}
214+
215+
/**
216+
* @param Response $response
217+
* @return void
218+
*/
219+
private function setRateLimitDetails(Response $response)
220+
{
221+
$this->rateLimitDetails = [
222+
'limit' => $response->hasHeader('X-RateLimit-Limit')
223+
? (int)$response->getHeader('X-RateLimit-Limit')[0]
224+
: null,
225+
'remaining' => $response->hasHeader('X-RateLimit-Remaining')
226+
? (int)$response->getHeader('X-RateLimit-Remaining')[0]
227+
: null,
228+
'reset_at' => $response->hasHeader('X-RateLimit-Reset')
229+
? (new \DateTimeImmutable())->setTimestamp((int)$response->getHeader('X-RateLimit-Reset')[0])
230+
: null,
231+
];
232+
}
233+
234+
/**
235+
* @return int[]
236+
*/
237+
public function getRateLimitDetails()
238+
{
239+
return $this->rateLimitDetails;
240+
}
209241
}

test/IntercomClientTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,34 @@ public function testPaginationHelper()
6161
}
6262
}
6363

64+
public function testRateLimitDetails()
65+
{
66+
$time = time() + 7;
67+
$mock = new MockHandler([
68+
new Response(200, ['X-RateLimit-Limit' => '83', 'X-RateLimit-Remaining' => '2', 'X-RateLimit-Reset' => $time], "{\"foo\":\"bar\"}")
69+
]);
70+
71+
$container = [];
72+
$history = Middleware::history($container);
73+
$stack = HandlerStack::create($mock);
74+
$stack->push($history);
75+
76+
$http_client = new Client(['handler' => $stack]);
77+
78+
$client = new IntercomClient('u', 'p');
79+
$client->setClient($http_client);
80+
81+
$client->users->create([
82+
'email' => 'test@intercom.io'
83+
]);
84+
85+
$rateLimitDetails = $client->getRateLimitDetails();
86+
$this->assertInternalType('array', $rateLimitDetails);
87+
$this->assertArrayHasKey('limit', $rateLimitDetails);
88+
$this->assertArrayHasKey('remaining', $rateLimitDetails);
89+
$this->assertArrayHasKey('reset_at', $rateLimitDetails);
90+
$this->assertEquals(83, $rateLimitDetails['limit']);
91+
$this->assertEquals(2, $rateLimitDetails['remaining']);
92+
$this->assertEquals((new DateTimeImmutable)->setTimestamp($time), $rateLimitDetails['reset_at']);
93+
}
6494
}

0 commit comments

Comments
 (0)