Skip to content

Commit d2f55a5

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 d2f55a5

File tree

5 files changed

+234
-34
lines changed

5 files changed

+234
-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: 66 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,92 @@
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.
23+
*
24+
* @deprecated 2.10.. Use `->getResources()` instead
1625
*
1726
* @return array
1827
*/
1928
public function getRateLimits()
2029
{
21-
return $this->get('/rate_limit');
30+
return $this->fetchLimits();
31+
}
32+
33+
/**
34+
* Gets the rate limit resource objects.
35+
*
36+
* @return RateLimitResource[]
37+
*/
38+
public function getResources()
39+
{
40+
$this->fetchLimits();
41+
42+
return $this->resources;
43+
}
44+
45+
/**
46+
* Returns a rate limit resource object by the given name.
47+
*
48+
* @param string $name
49+
*
50+
* @return RateLimitResource|false
51+
*/
52+
public function getResource($name)
53+
{
54+
// Fetch once per instance
55+
if (empty($this->resources)) {
56+
$this->fetchLimits();
57+
}
58+
59+
if (!isset($this->resources[$name])) {
60+
return false;
61+
}
62+
return $this->resources[$name];
63+
}
64+
65+
/**
66+
* Returns the data directly from the GitHub API endpoint.
67+
*
68+
* @return array
69+
*/
70+
protected function fetchLimits()
71+
{
72+
$result = $this->get('/rate_limit') ?: [];
73+
74+
// Assemble Limit instances
75+
foreach ($result['resources'] as $resourceName => $resource) {
76+
$this->resources[$resourceName] = new RateLimitResource($resourceName, $resource);
77+
}
78+
79+
return $result;
2280
}
2381

2482
/**
2583
* Get core rate limit.
2684
*
85+
* @deprecated 2.10.. Use `->getResource('core')->getLimit()` instead
86+
*
2787
* @return int
2888
*/
2989
public function getCoreLimit()
3090
{
31-
$response = $this->getRateLimits();
32-
33-
return $response['resources']['core']['limit'];
91+
return $this->getResource('core')->getLimit();
3492
}
3593

3694
/**
3795
* Get search rate limit.
3896
*
97+
* @deprecated 2.10.. Use `->getResource('core')->getLimit()` instead
98+
*
3999
* @return int
40100
*/
41101
public function getSearchLimit()
42102
{
43-
$response = $this->getRateLimits();
44-
45-
return $response['resources']['search']['limit'];
103+
return $this->getResource('search')->getLimit();
46104
}
47105
}
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)