Skip to content

Commit

Permalink
Merge branch 'topic/PaginatedListPerPage' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquev6 committed Mar 14, 2013
2 parents 4f1780f + e25a6a4 commit 0f9bb5d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -12,10 +12,10 @@ What's new?

`Version 1.13.0 <https://github.com/jacquev6/PyGithub/issues?milestone=23&state=closed>`_ (March 15th, 2013) (`ksookocheff-va <https://github.com/ptwobrussell>`_'s edition)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

All issues solved in this version were reported by `ksookocheff-va <https://github.com/ptwobrussell>`_. Thank you!

* `Fix <https://github.com/jacquev6/PyGithub/issues/143>`_ for Python 3 on case-insensitive file-systems
* `Add <https://github.com/jacquev6/PyGithub/issues/145>`_ a property ``Github.per_page`` (and a parameter to the constructor) to change the number of items requested in paginated requests

Documentation
=============
Expand Down
17 changes: 15 additions & 2 deletions github/MainClass.py
Expand Up @@ -30,14 +30,15 @@

DEFAULT_BASE_URL = "https://api.github.com"
DEFAULT_TIMEOUT = 10
DEFAULT_PER_PAGE = 30


class Github(object):
"""
This is the main class you instanciate to access the Github API v3. Optional parameters allow different authentication methods.
"""

def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent=None):
def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent=None, per_page=DEFAULT_PER_PAGE):
"""
:param login_or_token: string
:param password: string
Expand All @@ -46,6 +47,7 @@ def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL
:param client_id: string
:param client_secret: string
:param user_agent: string
:param per_page: int
"""

assert login_or_token is None or isinstance(login_or_token, (str, unicode)), login_or_token
Expand All @@ -55,7 +57,7 @@ def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL
assert client_id is None or isinstance(client_id, (str, unicode)), client_id
assert client_secret is None or isinstance(client_secret, (str, unicode)), client_secret
assert user_agent is None or isinstance(user_agent, (str, unicode)), user_agent
self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent)
self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page)

def __get_FIX_REPO_GET_GIT_REF(self):
return self.__requester.FIX_REPO_GET_GIT_REF
Expand All @@ -65,6 +67,17 @@ def __set_FIX_REPO_GET_GIT_REF(self, value):

FIX_REPO_GET_GIT_REF = property(__get_FIX_REPO_GET_GIT_REF, __set_FIX_REPO_GET_GIT_REF)

def get_per_page(self):
"""
:type: int
"""
return self.__requester.per_page

def set_per_page(self, value):
self.__requester.per_page = value

per_page = property(get_per_page, set_per_page)

@property
def rate_limiting(self):
"""
Expand Down
4 changes: 4 additions & 0 deletions github/PaginatedList.py
Expand Up @@ -96,6 +96,8 @@ def __init__(self, contentClass, requester, firstUrl, firstParams):
self.__firstParams = firstParams or ()
self.__nextUrl = firstUrl
self.__nextParams = firstParams
if self.__requester.per_page != 30:
self.__nextParams["per_page"] = self.__requester.per_page

def _couldGrow(self):
return self.__nextUrl is not None
Expand Down Expand Up @@ -130,6 +132,8 @@ def get_page(self, page):
params = dict(self.__firstParams)
if page != 0:
params["page"] = page + 1
if self.__requester.per_page != 30:
params["per_page"] = self.__requester.per_page
headers, data = self.__requester.requestJsonAndCheck("GET", self.__firstUrl, params, None)

return [
Expand Down
3 changes: 2 additions & 1 deletion github/Requester.py
Expand Up @@ -45,7 +45,7 @@ def resetConnectionClasses(cls):
cls.__httpConnectionClass = httplib.HTTPConnection
cls.__httpsConnectionClass = httplib.HTTPSConnection

def __init__(self, login_or_token, password, base_url, timeout, client_id, client_secret, user_agent):
def __init__(self, login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page):
if password is not None:
login = login_or_token
if atLeastPython3:
Expand Down Expand Up @@ -73,6 +73,7 @@ def __init__(self, login_or_token, password, base_url, timeout, client_id, clien
assert False, "Unknown URL scheme" # pragma no cover
self.rate_limiting = (5000, 5000)
self.FIX_REPO_GET_GIT_REF = True
self.per_page = per_page

self.oauth_scopes = None

Expand Down
11 changes: 10 additions & 1 deletion github/tests/PaginatedList.py
Expand Up @@ -19,7 +19,8 @@
class PaginatedList(Framework.TestCase):
def setUp(self):
Framework.TestCase.setUp(self)
self.list = self.g.get_user("openframeworks").get_repo("openFrameworks").get_issues()
self.repo = self.g.get_user("openframeworks").get_repo("openFrameworks")
self.list = self.repo.get_issues()

def testIteration(self):
self.assertEqual(len(list(self.list)), 333)
Expand Down Expand Up @@ -80,3 +81,11 @@ def testInterruptedIterationInSlice(self):
l += 1
if l == 75:
break

def testCustomPerPage(self):
self.g.per_page = 100
self.assertEqual(len(list(self.repo.get_issues())), 456)

def testCustomPerPageWithGetPage(self):
self.g.per_page = 100
self.assertEqual(len(self.repo.get_issues().get_page(2)), 100)
25 changes: 25 additions & 0 deletions github/tests/ReplayData/PaginatedList.testCustomPerPage.txt

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit 0f9bb5d

Please sign in to comment.