Skip to content

Commit cd6dac7

Browse files
committed
MINOR Add structured Limit objects for rate limiting API, deprecate getRateLimits()
This adds structured link objects for each of the rate limiting resources that is returned from GitHub, including adding GraphQL. The previous methods are still backwards compatible and working, but are now deprecated in favour of the new methods.
1 parent 33d6c9e commit cd6dac7

File tree

5 files changed

+230
-34
lines changed

5 files changed

+230
-34
lines changed

doc/rate_limits.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
## Rate Limit API
22
[Back to the navigation](README.md)
33

4-
Get Rate Limit
5-
Wraps [GitHub Rate Limit API](http://developer.github.com/v3/rate_limit/).
4+
Get rate limit wrappers from [GitHub Rate Limit API](http://developer.github.com/v3/rate_limit/).
65

7-
#### Get All Rate Limits.
6+
#### Get All Rate Limits
87

98
```php
10-
$rateLimits = $client->api('rate_limit')->getRateLimits();
9+
/** @var \Github\Api\RateLimit\RateLimitResource[] $rateLimits
10+
$rateLimits = $client->api('rate_limit')->getLimits();
1111
```
1212

1313
#### Get Core Rate Limit
1414

1515
```php
16-
$coreLimit = $client->api('rate_limit')->getCoreLimit();
16+
$coreLimit = $client->api('rate_limit')->getResource('core')->getLimit();
17+
$remaining = $client->api('rate_limit')->getResource('core')->getRemaining();
18+
$reset = $client->api('rate_limit')->getResource('core')->getReset();
1719
```
1820

1921
#### Get Search Rate Limit
2022

2123
```php
22-
$searchLimit = $client->api('rate_limit')->getSearchLimit();
24+
$searchLimit = $client->api('rate_limit')->getResource('search')->getLimit();
25+
```
26+
27+
#### Get GraphQL Rate Limit
28+
29+
```php
30+
$searchLimit = $client->api('rate_limit')->getResource('graphql')->getLimit();
2331
```

lib/Github/Api/RateLimit.php

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Github\Api;
44

5+
use Github\Api\RateLimit\RateLimitResource;
6+
57
/**
68
* Get rate limits.
79
*
@@ -12,36 +14,88 @@
1214
class RateLimit extends AbstractApi
1315
{
1416
/**
15-
* Get rate limits.
17+
* @var RateLimitResource[]
18+
*/
19+
protected $resources = [];
20+
21+
/**
22+
* Get rate limits data in an array
1623
*
24+
* @deprecated 2.10.. Use `->getResources()` instead
1725
* @return array
1826
*/
1927
public function getRateLimits()
2028
{
21-
return $this->get('/rate_limit');
29+
return $this->fetchLimits();
30+
}
31+
32+
/**
33+
* Gets the rate limit resource objects
34+
*
35+
* @return RateLimitResource[]
36+
*/
37+
public function getResources()
38+
{
39+
$this->fetchLimits();
40+
41+
return $this->resources;
42+
}
43+
44+
/**
45+
* Returns a rate limit resource object by the given name
46+
*
47+
* @param string $name
48+
* @return RateLimitResource|false
49+
*/
50+
public function getResource($name)
51+
{
52+
// Fetch once per instance
53+
if (empty($this->resources)) {
54+
$this->fetchLimits();
55+
}
56+
57+
if (!isset($this->resources[$name])) {
58+
return false;
59+
}
60+
return $this->resources[$name];
61+
}
62+
63+
/**
64+
* Returns the data directly from the GitHub API endpoint
65+
*
66+
* @return array
67+
*/
68+
protected function fetchLimits()
69+
{
70+
$result = $this->get('/rate_limit') ?: [];
71+
72+
// Assemble Limit instances
73+
foreach ($result['resources'] as $resourceName => $resource) {
74+
$this->resources[$resourceName] = new RateLimitResource($resourceName, $resource);
75+
}
76+
77+
return $result;
2278
}
2379

2480
/**
2581
* Get core rate limit.
2682
*
83+
* @deprecated 2.10.. Use `->getResource('core')->getLimit()` instead
2784
* @return int
2885
*/
2986
public function getCoreLimit()
3087
{
31-
$response = $this->getRateLimits();
32-
33-
return $response['resources']['core']['limit'];
88+
return $this->getResource('core')->getLimit();
3489
}
3590

3691
/**
3792
* Get search rate limit.
3893
*
94+
* @deprecated 2.10.. Use `->getResource('core')->getLimit()` instead
3995
* @return int
4096
*/
4197
public function getSearchLimit()
4298
{
43-
$response = $this->getRateLimits();
44-
45-
return $response['resources']['search']['limit'];
99+
return $this->getResource('search')->getLimit();
46100
}
47101
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Github\Api\RateLimit;
4+
5+
/**
6+
* Represents the data block for a GitHub rate limit response, grouped by a name
7+
*/
8+
class RateLimitResource
9+
{
10+
/**
11+
* @param string $name
12+
* @param array $data
13+
*/
14+
public function __construct($name, array $data)
15+
{
16+
$this->name = $name;
17+
$this->limit = $data['limit'];
18+
$this->remaining = $data['remaining'];
19+
$this->reset = $data['reset'];
20+
}
21+
22+
/**
23+
* The name of the Limit, e.g. "core", "graphql", "search"
24+
*
25+
* @return string
26+
*/
27+
public function getName()
28+
{
29+
return $this->name;
30+
}
31+
32+
/**
33+
* The rate limit amount
34+
*
35+
* @return int
36+
*/
37+
public function getLimit()
38+
{
39+
return $this->limit;
40+
}
41+
42+
/**
43+
* Number of requests remaining in time period before hitting the rate limit
44+
*
45+
* @return int
46+
*/
47+
public function getRemaining()
48+
{
49+
return $this->remaining;
50+
}
51+
52+
/**
53+
* Timestamp for when the rate limit will be reset
54+
*
55+
* @return int
56+
*/
57+
public function getReset()
58+
{
59+
return $this->reset;
60+
}
61+
}

test/Github/Tests/Api/RateLimitTest.php

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,94 @@
22

33
namespace Github\Tests\Api;
44

5+
use Github\Api\RateLimit;
6+
57
class RateLimitTest extends TestCase
68
{
79
/**
8-
* @test
10+
* Used for common assertions in each test
11+
*
12+
* @var array
913
*/
10-
public function shouldReturnRateLimitArray()
11-
{
12-
$expectedArray = [
13-
'resources' => [
14-
'core' => [
15-
'limit' => 5000,
16-
'remaining' => 4999,
17-
'reset' => 1372700873,
18-
],
19-
'search' => [
20-
'limit' => 30,
21-
'remaining' => 18,
22-
'reset' => 1372697452,
23-
],
14+
protected $expectedArray = [
15+
'resources' => [
16+
'core' => [
17+
'limit' => 5000,
18+
'remaining' => 4999,
19+
'reset' => 1372700873,
20+
],
21+
'search' => [
22+
'limit' => 30,
23+
'remaining' => 18,
24+
'reset' => 1372697452,
25+
],
26+
'graphql' => [
27+
'limit' => 5000,
28+
'remaining' => 4030,
29+
'reset' => 1372697452,
2430
],
25-
];
31+
],
32+
];
33+
34+
/**
35+
* @var RateLimit
36+
*/
37+
protected $api;
38+
39+
/**
40+
* Used to construct common expectations for the API input data in each unit test
41+
*
42+
* {@inheritDoc}
43+
*/
44+
protected function setUp()
45+
{
46+
parent::setUp();
2647

27-
$api = $this->getApiMock();
28-
$api->expects($this->once())
48+
$this->api = $this->getApiMock();
49+
$this->api->expects($this->once())
2950
->method('get')
3051
->with('/rate_limit')
31-
->will($this->returnValue($expectedArray));
52+
->will($this->returnValue($this->expectedArray));
53+
}
3254

33-
$this->assertEquals($expectedArray, $api->getRateLimits());
55+
/**
56+
* @test
57+
*/
58+
public function shouldReturnRateLimitArray()
59+
{
60+
$this->assertSame($this->expectedArray, $this->api->getRateLimits());
61+
}
62+
63+
/**
64+
* @test
65+
*/
66+
public function shouldReturnArrayOfLimitInstances()
67+
{
68+
$this->assertContainsOnlyInstancesOf(RateLimit\RateLimitResource::class, $this->api->getResources());
69+
}
70+
71+
/**
72+
* @test
73+
*/
74+
public function shouldReturnRemainingGraphQLRequests()
75+
{
76+
$this->assertSame(4030, $this->api->getResource('graphql')->getRemaining());
77+
}
78+
79+
/**
80+
* @test
81+
*/
82+
public function shouldReturnResetForSearch()
83+
{
84+
$this->assertSame(1372697452, $this->api->getResource('search')->getReset());
85+
}
86+
87+
/**
88+
* @test
89+
*/
90+
public function shouldReturnFalseWhenResourceIsNotFound()
91+
{
92+
$this->assertFalse($this->api->getResource('non-exitent'));
3493
}
3594

3695
/**

test/Github/Tests/Integration/RateLimitTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Github\Tests\Integration;
44

5+
use Github\Api\RateLimit\RateLimitResource;
6+
57
/**
68
* @group integration
79
*/
@@ -17,4 +19,16 @@ public function shouldRetrievedRateLimits()
1719
$this->assertArrayHasKey('resources', $response);
1820
$this->assertArraySubset(['resources' => ['core' => ['limit' => 60]]], $response);
1921
}
22+
23+
/**
24+
* @test
25+
*/
26+
public function shouldRetrieveRateLimitsAndReturnLimitInstances()
27+
{
28+
$response = $this->client->api('rate_limit')->getLimits();
29+
30+
$this->assertInternalType('array', $response);
31+
$this->assertContainsOnlyInstancesOf(RateLimitResource::class, $response);
32+
$this->assertEquals(60, $response->getLimit('core')->getLimit());
33+
}
2034
}

0 commit comments

Comments
 (0)