Skip to content

Commit fb983cb

Browse files
committed
Fix sigmavirus24#775: Wrong rate limit returned for search
This change adds a field which indicates against what rate limit API calls from an object are counted against. GitHub API v3 enforces different rate limits [1] for subsets of API calls. The `search` API has custom rate limts.[2] `GitHubCore` has a property `remaining_ratelimit` which requests rate limit information from endpoint `GET /rate_limit` [3] and caches the number in `GitHubCore._remaining`. The API call returns information about all rate limits but previously `GitHubCore.remaining_ratelimit` only exposed the one for the `core` resources. With this change the resource still defaults to `core` but objects sending search request change this to `search`. [1]: https://developer.github.com/v3/#rate-limiting [2]: https://developer.github.com/v3/search/#rate-limit [3]: https://developer.github.com/v3/rate_limit/
1 parent 785562d commit fb983cb

5 files changed

Lines changed: 15 additions & 7 deletions

File tree

github3/models.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __init__(self, json, session=None):
4242

4343
# set a sane default
4444
self._github_url = 'https://api.github.com'
45+
self._ratelimit_resource = 'core'
4546

4647
if json is not None:
4748
self.etag = json.pop('ETag', None)
@@ -305,7 +306,7 @@ def ratelimit_remaining(self):
305306
:returns: int
306307
"""
307308
json = self._json(self._get(self._github_url + '/rate_limit'), 200)
308-
core = json.get('resources', {}).get('core', {})
309+
core = json.get('resources', {}).get(self._ratelimit_resource, {})
309310
self._remaining = core.get('remaining', 0)
310311
return self._remaining
311312

@@ -502,3 +503,10 @@ def _update_attributes(self, acct):
502503

503504
def _repr(self):
504505
return '<{s.type} [{s.login}:{s.name}]>'.format(s=self)
506+
507+
508+
class GitHubSearch(GitHubCore):
509+
"""The base object for all search objects."""
510+
def __init__(self, json, session=None):
511+
super(GitHubSearch, self).__init__(json, session)
512+
self._ratelimit_resource = 'search'

github3/search/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .. import repos
66

77

8-
class CodeSearchResult(models.GitHubCore):
8+
class CodeSearchResult(models.GitHubSearch):
99

1010
def _update_attributes(self, data):
1111
self._api = self._get_attribute(data, 'url')

github3/search/issue.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4-
from ..models import GitHubCore
4+
from ..models import GitHubSearch
55
from ..issues import ShortIssue
66

77

8-
class IssueSearchResult(GitHubCore):
8+
class IssueSearchResult(GitHubSearch):
99
def _update_attributes(self, data):
1010
result = data.copy()
1111

github3/search/repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .. import repos
66

77

8-
class RepositorySearchResult(models.GitHubCore):
8+
class RepositorySearchResult(models.GitHubSearch):
99
def _update_attributes(self, data):
1010
result = data.copy()
1111

github3/search/user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from __future__ import unicode_literals
33

44
from .. import users
5-
from ..models import GitHubCore
5+
from ..models import GitHubSearch
66

77

8-
class UserSearchResult(GitHubCore):
8+
class UserSearchResult(GitHubSearch):
99
def _update_attributes(self, data):
1010
result = data.copy()
1111

0 commit comments

Comments
 (0)