mirror of
https://github.com/status-im/PyGithub.git
synced 2026-08-31 19:01:15 +00:00
Implement legacy pagination
This commit is contained in:
+71
-11
@@ -11,17 +11,77 @@
|
||||
|
||||
# You should have received a copy of the GNU Lesser General Public License along with PyGithub. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
def PaginatedList( url, args, requester, key, convert, contentClass ):
|
||||
headers, data = requester.requestAndCheck(
|
||||
"GET",
|
||||
url,
|
||||
args,
|
||||
None
|
||||
)
|
||||
return [
|
||||
contentClass( requester, convert( element ), completed = False )
|
||||
for element in data[ key ]
|
||||
]
|
||||
class PaginatedList:
|
||||
def __init__( self, url, args, requester, key, convert, contentClass ):
|
||||
self.__url = url
|
||||
self.__args = args
|
||||
self.__requester = requester
|
||||
self.__key = key
|
||||
self.__convert = convert
|
||||
self.__contentClass = contentClass
|
||||
self.__nextPage = 1
|
||||
self.__continue = True
|
||||
self.__elements = list()
|
||||
|
||||
class __Slice:
|
||||
def __init__( self, theList, theSlice ):
|
||||
self.__list = theList
|
||||
self.__start = theSlice.start or 0
|
||||
self.__stop = theSlice.stop
|
||||
self.__step = theSlice.step or 1
|
||||
|
||||
def __iter__( self ):
|
||||
index = self.__start
|
||||
while not self.__finished( index ) :
|
||||
if self.__list._isBiggerThan( index ):
|
||||
yield self.__list[ index ]
|
||||
index += self.__step
|
||||
else:
|
||||
return
|
||||
|
||||
def __finished( self, index ):
|
||||
return self.__stop is not None and index >= self.__stop
|
||||
|
||||
def __iter__( self ):
|
||||
for element in self.__elements:
|
||||
yield element
|
||||
while self.__continue:
|
||||
newElements = self.__fetchNextPage()
|
||||
self.__continue = len( newElements ) > 0
|
||||
for element in newElements:
|
||||
yield element
|
||||
|
||||
def __fetchNextPage( self ):
|
||||
if self.__nextPage != 1:
|
||||
self.__args[ "start_page" ] = self.__nextPage
|
||||
self.__nextPage += 1
|
||||
headers, data = self.__requester.requestAndCheck(
|
||||
"GET",
|
||||
self.__url,
|
||||
self.__args,
|
||||
None
|
||||
)
|
||||
newElements = [
|
||||
self.__contentClass( self.__requester, self.__convert( element ), completed = False )
|
||||
for element in data[ self.__key ]
|
||||
]
|
||||
self.__elements += newElements
|
||||
return newElements
|
||||
|
||||
def __getitem__( self, index ):
|
||||
assert isinstance( index, ( int, slice ) )
|
||||
if isinstance( index, int ):
|
||||
self.__fetchToIndex( index )
|
||||
return self.__elements[ index ]
|
||||
else:
|
||||
return self.__Slice( self, index )
|
||||
|
||||
def _isBiggerThan( self, index ):
|
||||
return len( self.__elements ) > index or self.__continue
|
||||
|
||||
def __fetchToIndex( self, index ):
|
||||
while len( self.__elements ) <= index and self.__continue:
|
||||
self.__fetchNextPage()
|
||||
|
||||
def convertUser( attributes ):
|
||||
login = attributes[ "login" ]
|
||||
|
||||
@@ -1006,14 +1006,16 @@ class Repository( GithubObject.GithubObject ):
|
||||
def legacy_search_issues( self, state, keyword ):
|
||||
assert state in [ "open", "closed" ], state
|
||||
assert isinstance( keyword, ( str, unicode ) ), keyword
|
||||
return Legacy.PaginatedList(
|
||||
headers, data = self._requester.requestAndCheck(
|
||||
"GET",
|
||||
"https://api.github.com/legacy/issues/search/" + self.owner.login + "/" + self.name + "/" + state + "/" + keyword,
|
||||
{},
|
||||
self._requester,
|
||||
"issues",
|
||||
Legacy.convertIssue,
|
||||
Issue.Issue,
|
||||
None
|
||||
)
|
||||
return [
|
||||
Issue.Issue( self._requester, Legacy.convertIssue( element ), completed = False )
|
||||
for element in data[ "issues" ]
|
||||
]
|
||||
|
||||
@property
|
||||
def _identity( self ):
|
||||
|
||||
Reference in New Issue
Block a user