Calcuate totalCount without iterating PaginatedList (#820)

This PR #596 seems stale, so I have opened up this one with the requested changes from @sfdye.

This is the code from @Tommos0's [comment][1] with some modification.

There are 2 tests failing. I think this is due to the change in the query string and the cached test data just needs to be refreshed for those tests.

[1]: https://github.com/PyGithub/PyGithub/pull/596#issuecomment-315013949
This commit is contained in:
Joel Koglin
2018-07-08 10:43:48 +08:00
committed by Wan Liuyang
parent c6802b515f
commit e6eabe9ad0
4 changed files with 23 additions and 7 deletions
+20 -2
View File
@@ -36,6 +36,11 @@
# #
################################################################################
try:
from urllib.parse import parse_qs
except ImportError:
from urlparse import parse_qs
import github.GithubObject
@@ -134,8 +139,21 @@ class PaginatedList(PaginatedListBase):
@property
def totalCount(self):
if not self.__totalCount:
self._grow()
params = {} if self.__nextParams is None else self.__nextParams.copy()
# set per_page = 1 so the totalCount is just the number of pages
params.update({"per_page": 1})
headers, data = self.__requester.requestJsonAndCheck(
"GET",
self.__firstUrl,
parameters=params,
headers=self.__headers
)
if 'link' not in headers:
self.__totalCount = len(data)
else:
links = self.__parseLinkHeader(headers)
lastUrl = links.get("last")
self.__totalCount = int(parse_qs(lastUrl)['page'][0])
return self.__totalCount
def _getLastPageUrl(self):
@@ -2,7 +2,7 @@ https
GET
api.github.com
None
/search/commits?q=hash%3A5b0224e868cc9242c9450ef02efbe3097abd7ba2
/search/commits?q=hash%3A5b0224e868cc9242c9450ef02efbe3097abd7ba2&per_page=1
{'Authorization': 'Basic login_and_password_removed', 'Accept': 'application/vnd.github.cloak-preview', 'User-Agent': 'PyGithub/Python'}
null
200
File diff suppressed because one or more lines are too long
-2
View File
@@ -38,8 +38,6 @@ class Search(Framework.TestCase):
def testSearchUsers(self):
users = self.g.search_users("vincent", sort="followers", order="desc")
self.assertEqual(users.totalCount, 2781)
self.assertEqual(users[0].login, "nvie")
self.assertEqual(users[14].login, "Vayn")
def testPaginateSearchUsers(self):
users = self.g.search_users("", location="Berlin")