Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit24d5035

Browse files
author
Gauvain Pocentek
committed
Merge pull request#64 from jantman/issues/63
python-gitlab Issue#63 - implement pagination for list()
2 parentsadbe0a4 +719526d commit24d5035

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

‎gitlab/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ def set_url(self, url):
189189
self._url='%s/api/v3'%url
190190

191191
def_construct_url(self,id_,obj,parameters):
192+
if'next_url'inparameters:
193+
returnparameters['next_url']
192194
args=_sanitize_dict(parameters)
193195
ifid_isNoneandobj._urlPluralisnotNone:
194196
url=obj._urlPlural%args
@@ -346,8 +348,13 @@ def list(self, obj_class, **kwargs):
346348
ifkeyincls_kwargs:
347349
delcls_kwargs[key]
348350

349-
return [cls(self,item,**cls_kwargs)foriteminr.json()
350-
ifitemisnotNone]
351+
results= [cls(self,item,**cls_kwargs)foriteminr.json()
352+
ifitemisnotNone]
353+
if'next'inr.linksand'url'inr.links['next']:
354+
args=kwargs.copy()
355+
args['next_url']=r.links['next']['url']
356+
results.extend(self.list(obj_class,**args))
357+
returnresults
351358
else:
352359
_raise_error_from_response(r,GitlabListError)
353360

‎gitlab/tests/test_gitlab.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
importunittest
2323
exceptImportError:
2424
importunittest2asunittest
25+
importjson
2526

2627
fromhttmockimportHTTMock# noqa
2728
fromhttmockimportresponse# noqa
@@ -178,6 +179,56 @@ def resp_cont(url, request):
178179
self.assertEqual(data.project_id,1)
179180
self.assertEqual(data.ref,"a")
180181

182+
deftest_list_next_link(self):
183+
@urlmatch(scheme="http",netloc="localhost",
184+
path='/api/v3/projects/1/repository/branches',method="get",
185+
query=r'per_page=1')
186+
defresp_one(url,request):
187+
"""
188+
First request:
189+
http://localhost/api/v3/projects/1/repository/branches?per_page=1
190+
"""
191+
headers= {
192+
'content-type':'application/json',
193+
'link':'<http://localhost/api/v3/projects/1/repository/branc' \
194+
'hes?page=2&per_page=0>; rel="next", <http://localhost/api/v3' \
195+
'/projects/1/repository/branches?page=2&per_page=0>; rel="las' \
196+
't", <http://localhost/api/v3/projects/1/repository/branches?' \
197+
'page=1&per_page=0>; rel="first"'
198+
}
199+
content= ('[{"branch_name": "otherbranch", '
200+
'"project_id": 1, "ref": "b"}]').encode("utf-8")
201+
resp=response(200,content,headers,None,5,request)
202+
returnresp
203+
204+
@urlmatch(scheme="http",netloc="localhost",
205+
path='/api/v3/projects/1/repository/branches',method="get",
206+
query=r'.*page=2.*')
207+
defresp_two(url,request):
208+
headers= {
209+
'content-type':'application/json',
210+
'link':'<http://localhost/api/v3/projects/1/repository/branc' \
211+
'hes?page=1&per_page=0>; rel="prev", <http://localhost/api/v3' \
212+
'/projects/1/repository/branches?page=2&per_page=0>; rel="las' \
213+
't", <http://localhost/api/v3/projects/1/repository/branches?' \
214+
'page=1&per_page=0>; rel="first"'
215+
}
216+
content= ('[{"branch_name": "testbranch", '
217+
'"project_id": 1, "ref": "a"}]').encode("utf-8")
218+
resp=response(200,content,headers,None,5,request)
219+
returnresp
220+
221+
withHTTMock(resp_one,resp_two):
222+
data=self.gl.list(ProjectBranch,project_id=1,
223+
per_page=1)
224+
self.assertEqual(data[1].branch_name,"testbranch")
225+
self.assertEqual(data[1].project_id,1)
226+
self.assertEqual(data[1].ref,"a")
227+
self.assertEqual(data[0].branch_name,"otherbranch")
228+
self.assertEqual(data[0].project_id,1)
229+
self.assertEqual(data[0].ref,"b")
230+
self.assertEqual(len(data),2)
231+
181232
deftest_list_401(self):
182233
@urlmatch(scheme="http",netloc="localhost",
183234
path="/api/v3/projects/1/repository/branches",method="get")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp