Skip to content

Commit 1de78ab

Browse files
committed
I am far happier with this
1 parent 7a336ae commit 1de78ab

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

github3/github.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
from json import dumps
1010
from requests import session
1111
from github3.auths import Authorization
12+
from github3.decorators import requires_auth, requires_basic_auth
1213
from github3.events import Event
1314
from github3.gists import Gist
1415
from github3.issues import Issue, issue_params
1516
from github3.models import GitHubCore
1617
from github3.orgs import Organization
1718
from github3.repos import Repository
19+
from github3.structs import SearchIterator
1820
from github3.users import User, Key
19-
from github3.decorators import requires_auth, requires_basic_auth
2021
from github3.notifications import Thread
2122
from uritemplate import URITemplate
2223

@@ -985,7 +986,8 @@ def repository(self, owner, repository):
985986
return Repository(json, self) if json else None
986987

987988
def search_repositories(self, query, sort=None, order=None,
988-
per_page=None, text_match=False):
989+
per_page=None, text_match=False, number=-1,
990+
etag=None):
989991
"""Find repositories via various criteria.
990992
991993
The query can contain any combination of the following supported
@@ -1016,6 +1018,9 @@ def search_repositories(self, query, sort=None, order=None,
10161018
:param int per_page: (optional)
10171019
:param bool text_match: (optional), if True, return matching search
10181020
terms. See http://git.io/4ct1eQ for more information
1021+
:param int number: (optional), number of repositories to return.
1022+
Default: -1, returns all available repositories
1023+
:param str etag: (optional), previous ETag header value
10191024
:return: dict
10201025
"""
10211026
# TODO Describe the dictionary being returned
@@ -1034,12 +1039,7 @@ def search_repositories(self, query, sort=None, order=None,
10341039
}
10351040

10361041
url = self._build_url('search', 'repositories')
1037-
results = self._json(self._get(url, params=params, headers=headers),
1038-
200)
1039-
repos_dicts = results['items']
1040-
repos = [Repository(r) for r in repos_dicts]
1041-
results['repositories'] = repos
1042-
return results
1042+
return SearchIterator(number, url, Repository, self, params, etag)
10431043

10441044
def set_client_id(self, id, secret):
10451045
"""Allows the developer to set their client_id and client_secret for

github3/structs.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ def __init__(self, count, url, cls, session, params=None, etag=None):
3434
if etag:
3535
self.headers = {'If-None-Match': etag}
3636

37+
self.path = urlparse(self.url).path
38+
3739
def __repr__(self):
38-
path = urlparse(self.url).path
39-
return '<GitHubIterator [{0}, {1}]>'.format(self.count, path)
40+
return '<GitHubIterator [{0}, {1}]>'.format(self.count, self.path)
4041

4142
def __iter__(self):
4243
url, params, cls = self.url, self.params, self.cls
@@ -94,3 +95,23 @@ def refresh(self, conditional=False):
9495

9596
def next(self):
9697
return self.__next__()
98+
99+
100+
class SearchIterator(GitHubIterator):
101+
def __init__(self, count, url, cls, session, params=None, etag=None):
102+
super(SearchIterator, self).__init__(count, url, cls, session, params,
103+
etag)
104+
self.total_count = 0
105+
106+
def __repr__(self):
107+
return '<SearchIterator [{0}, {1}]>'.format(self.count, self.path)
108+
109+
def _json(self, response, status_code):
110+
json = super(SearchIterator, self)._json(response, status_code)
111+
# I'm not sure if another page will retain the total_count attribute,
112+
# so if it's not in the response, just set it back to what it used to
113+
# be
114+
self.total_count = json.get('total_count', self.total_count)
115+
self.items = json.get('items', [])
116+
# If we return None then it will short-circuit the while loop.
117+
return json.get('items')

0 commit comments

Comments
 (0)