Skip to content

Commit 832b7be

Browse files
committed
Merge pull request KnpLabs#314 from quickliketurtle/add-rate-limit-endpoint
Add rate limit endpoint
2 parents 7b50381 + eca9e8c commit 832b7be

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed

doc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ APIs:
1515
* [Teams](organization/teams.md)
1616
* [Pull Requests](pull_requests.md)
1717
* [Comments](pull_request/comments.md)
18+
* [Rate Limits](rate_limits.md)
1819
* [Repositories](repos.md)
1920
* [Contents](repo/contents.md)
2021
* [Releases](repo/releases.md)

doc/rate_limits.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Rate Limit API
2+
[Back to the navigation](README.md)
3+
4+
Get Rate Limit
5+
Wraps [GitHub Rate Limit API](http://developer.github.com/v3/rate_limit/).
6+
7+
#### Get All Rate Limits.
8+
9+
```php
10+
$rateLimits = $github->api('rate_limit')->getRateLimits();
11+
```
12+
13+
#### Get Core Rate Limit
14+
15+
```php
16+
$coreLimit = $github->api('rate_limit')->getCoreLimit();
17+
```
18+
19+
#### Get Search Rate Limit
20+
21+
```php
22+
$searchLimit = $github->api('rate_limit)->getSearchLimit');
23+
```

lib/Github/Api/RateLimit.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Github\Api;
4+
5+
/**
6+
* Get rate limits
7+
*
8+
* @link https://developer.github.com/v3/rate_limit/
9+
* @author Jeff Finley <quickliketurtle@gmail.com>
10+
*/
11+
class RateLimit extends AbstractApi
12+
{
13+
/**
14+
* Get rate limits
15+
*
16+
* @return array
17+
*/
18+
public function getRateLimits()
19+
{
20+
return $this->get('rate_limit');
21+
}
22+
23+
/**
24+
* Get core rate limit
25+
*
26+
* @return integer
27+
*/
28+
public function getCoreLimit()
29+
{
30+
$response = $this->getRateLimits();
31+
32+
return $response['resources']['core']['limit'];
33+
}
34+
35+
/**
36+
* Get search rate limit
37+
*
38+
* @return integer
39+
*/
40+
public function getSearchLimit()
41+
{
42+
$response = $this->getRateLimits();
43+
44+
return $response['resources']['search']['limit'];
45+
}
46+
}

lib/Github/Client.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @method Api\PullRequest pr()
3030
* @method Api\PullRequest pullRequest()
3131
* @method Api\PullRequest pullRequests()
32+
* @method Api\RateLimit ratelimit()
3233
* @method Api\Repo repo()
3334
* @method Api\Repo repos()
3435
* @method Api\Repo repository()
@@ -168,6 +169,11 @@ public function api($name)
168169
$api = new Api\PullRequest($this);
169170
break;
170171

172+
case 'rateLimit':
173+
case 'rate_limit':
174+
$api = new Api\RateLimit($this);
175+
break;
176+
171177
case 'repo':
172178
case 'repos':
173179
case 'repository':
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Github\Tests\Api;
4+
5+
class RateLimitTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldReturnRateLimitArray()
11+
{
12+
$expectedArray = array(
13+
'resources' => array(
14+
'core' => array(
15+
'limit' => 5000,
16+
'remaining' => 4999,
17+
'reset' => 1372700873
18+
),
19+
'search' => array(
20+
'limit' => 30,
21+
'remaining' => 18,
22+
'reset' => 1372697452
23+
)
24+
)
25+
);
26+
27+
$api = $this->getApiMock();
28+
$api->expects($this->once())
29+
->method('get')
30+
->with('rate_limit')
31+
->will($this->returnValue($expectedArray));
32+
33+
$this->assertEquals($expectedArray, $api->getRateLimits());
34+
}
35+
36+
protected function getApiClass()
37+
{
38+
return 'Github\Api\RateLimit';
39+
}
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Github\Tests\Functional;
4+
5+
/**
6+
* @group functional
7+
*/
8+
class RateLimitTest extends TestCase
9+
{
10+
/**
11+
* @test
12+
*/
13+
public function shouldRetrievedRateLimits()
14+
{
15+
$response = $this->client->api('rate_limit')->getRateLimits();
16+
17+
$this->assertArrayHasKey('resources', $response);
18+
$this->assertArraySubset(array('resources' => array('core' => array('limit' => 60))), $response);
19+
}
20+
}

0 commit comments

Comments
 (0)