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

Commit81b0a06

Browse files
author
Gauvain Pocentek
committed
Move request return_code tests in _raise_error_from_response
1 parent38f17c1 commit81b0a06

File tree

1 file changed

+62
-87
lines changed

1 file changed

+62
-87
lines changed

‎gitlab/__init__.py

Lines changed: 62 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,20 @@ class GitlabTransferProjectError(GitlabOperationError):
107107
pass
108108

109109

110-
def_raise_error_from_response(response,error):
110+
def_raise_error_from_response(response,error,expected_code=200):
111111
"""Tries to parse gitlab error message from response and raises error.
112112
113+
Do nothing if the response status is the expected one.
114+
113115
If response status code is 401, raises instead GitlabAuthenticationError.
114116
115117
response: requests response object
116118
error: Error-class to raise. Should be inherited from GitLabError
117119
"""
118120

121+
ifexpected_code==response.status_code:
122+
return
123+
119124
try:
120125
message=response.json()['message']
121126
except (KeyError,ValueError):
@@ -183,12 +188,8 @@ def credentials_auth(self):
183188

184189
data=json.dumps({'email':self.email,'password':self.password})
185190
r=self._raw_post('/session',data,content_type='application/json')
186-
187-
ifr.status_code==201:
188-
self.user=CurrentUser(self,r.json())
189-
else:
190-
_raise_error_from_response(r,GitlabAuthenticationError)
191-
191+
_raise_error_from_response(r,GitlabAuthenticationError,201)
192+
self.user=CurrentUser(self,r.json())
192193
self.set_token(self.user.private_token)
193194

194195
deftoken_auth(self):
@@ -342,34 +343,33 @@ def list(self, obj_class, **kwargs):
342343
raiseGitlabConnectionError(
343344
"Can't connect to GitLab server (%s)"%self._url)
344345

345-
ifr.status_code==200:
346-
cls=obj_class
347-
ifobj_class._returnClass:
348-
cls=obj_class._returnClass
346+
_raise_error_from_response(r,GitlabListError)
349347

350-
cls_kwargs=kwargs.copy()
348+
cls=obj_class
349+
ifobj_class._returnClass:
350+
cls=obj_class._returnClass
351351

352-
# Add _created manually, because we are not creating objects
353-
# through normal path
354-
cls_kwargs['_created']=True
352+
cls_kwargs=kwargs.copy()
355353

356-
get_all_results=params.get('all',False)
354+
# Add _created manually, because we are not creating objects
355+
# through normal path
356+
cls_kwargs['_created']=True
357357

358-
# Remove parameters from kwargs before passing it to constructor
359-
forkeyin ['all','page','per_page','sudo']:
360-
ifkeyincls_kwargs:
361-
delcls_kwargs[key]
358+
get_all_results=params.get('all',False)
362359

363-
results= [cls(self,item,**cls_kwargs)foriteminr.json()
364-
ifitemisnotNone]
365-
if ('next'inr.linksand'url'inr.links['next']
366-
andget_all_resultsisTrue):
367-
args=kwargs.copy()
368-
args['next_url']=r.links['next']['url']
369-
results.extend(self.list(obj_class,**args))
370-
returnresults
371-
else:
372-
_raise_error_from_response(r,GitlabListError)
360+
# Remove parameters from kwargs before passing it to constructor
361+
forkeyin ['all','page','per_page','sudo']:
362+
ifkeyincls_kwargs:
363+
delcls_kwargs[key]
364+
365+
results= [cls(self,item,**cls_kwargs)foriteminr.json()
366+
ifitemisnotNone]
367+
if ('next'inr.linksand'url'inr.links['next']
368+
andget_all_resultsisTrue):
369+
args=kwargs.copy()
370+
args['next_url']=r.links['next']['url']
371+
results.extend(self.list(obj_class,**args))
372+
returnresults
373373

374374
defget(self,obj_class,id=None,**kwargs):
375375
missing= []
@@ -399,10 +399,8 @@ def get(self, obj_class, id=None, **kwargs):
399399
raiseGitlabConnectionError(
400400
"Can't connect to GitLab server (%s)"%self._url)
401401

402-
ifr.status_code==200:
403-
returnr.json()
404-
else:
405-
_raise_error_from_response(r,GitlabGetError)
402+
_raise_error_from_response(r,GitlabGetError)
403+
returnr.json()
406404

407405
defdelete(self,obj,**kwargs):
408406
params=obj.__dict__.copy()
@@ -435,10 +433,8 @@ def delete(self, obj, **kwargs):
435433
raiseGitlabConnectionError(
436434
"Can't connect to GitLab server (%s)"%self._url)
437435

438-
ifr.status_code==200:
439-
returnTrue
440-
else:
441-
_raise_error_from_response(r,GitlabDeleteError)
436+
_raise_error_from_response(r,GitlabDeleteError)
437+
returnTrue
442438

443439
defcreate(self,obj,**kwargs):
444440
params=obj.__dict__.copy()
@@ -467,10 +463,8 @@ def create(self, obj, **kwargs):
467463
raiseGitlabConnectionError(
468464
"Can't connect to GitLab server (%s)"%self._url)
469465

470-
ifr.status_code==201:
471-
returnr.json()
472-
else:
473-
_raise_error_from_response(r,GitlabCreateError)
466+
_raise_error_from_response(r,GitlabCreateError,201)
467+
returnr.json()
474468

475469
defupdate(self,obj,**kwargs):
476470
params=obj.__dict__.copy()
@@ -498,10 +492,8 @@ def update(self, obj, **kwargs):
498492
raiseGitlabConnectionError(
499493
"Can't connect to GitLab server (%s)"%self._url)
500494

501-
ifr.status_code==200:
502-
returnr.json()
503-
else:
504-
_raise_error_from_response(r,GitlabUpdateError)
495+
_raise_error_from_response(r,GitlabUpdateError)
496+
returnr.json()
505497

506498
defHook(self,id=None,**kwargs):
507499
"""Creates/tests/lists system hook(s) known by the GitLab server.
@@ -539,8 +531,7 @@ def UserProject(self, id=None, **kwargs):
539531

540532
def_list_projects(self,url,**kwargs):
541533
r=self._raw_get(url,**kwargs)
542-
ifr.status_code!=200:
543-
_raise_error_from_response(r,GitlabListError)
534+
_raise_error_from_response(r,GitlabListError)
544535

545536
l= []
546537
foroinr.json():
@@ -923,8 +914,7 @@ def Member(self, id=None, **kwargs):
923914
deftransfer_project(self,id,**kwargs):
924915
url='/groups/%d/projects/%d'% (self.id,id)
925916
r=self.gitlab._raw_post(url,None,**kwargs)
926-
ifr.status_code!=201:
927-
_raise_error_from_response(r,GitlabTransferProjectError)
917+
_raise_error_from_response(r,GitlabTransferProjectError,201)
928918

929919

930920
classHook(GitlabObject):
@@ -960,14 +950,12 @@ def protect(self, protect=True, **kwargs):
960950
action='protect'ifprotectelse'unprotect'
961951
url="%s/%s/%s"% (url,self.name,action)
962952
r=self.gitlab._raw_put(url,data=None,content_type=None,**kwargs)
953+
_raise_error_from_response(r,GitlabProtectError)
963954

964-
ifr.status_code==200:
965-
ifprotect:
966-
self.protected=protect
967-
else:
968-
delself.protected
955+
ifprotect:
956+
self.protected=protect
969957
else:
970-
_raise_error_from_response(r,GitlabProtectError)
958+
delself.protected
971959

972960
defunprotect(self,**kwargs):
973961
self.protect(False,**kwargs)
@@ -985,20 +973,19 @@ def diff(self, **kwargs):
985973
url= ('/projects/%(project_id)s/repository/commits/%(commit_id)s/diff'
986974
% {'project_id':self.project_id,'commit_id':self.id})
987975
r=self.gitlab._raw_get(url,**kwargs)
988-
ifr.status_code==200:
989-
returnr.json()
990-
else:
991-
_raise_error_from_response(r,GitlabGetError)
976+
_raise_error_from_response(r,GitlabGetError)
977+
978+
returnr.json()
992979

993980
defblob(self,filepath,**kwargs):
994981
url= ('/projects/%(project_id)s/repository/blobs/%(commit_id)s'%
995982
{'project_id':self.project_id,'commit_id':self.id})
996983
url+='?filepath=%s'%filepath
997984
r=self.gitlab._raw_get(url,**kwargs)
998-
ifr.status_code==200:
999-
returnr.content
1000-
else:
1001-
_raise_error_from_response(r,GitlabGetError)
985+
986+
_raise_error_from_response(r,GitlabGetError)
987+
988+
returnr.content
1002989

1003990

1004991
classProjectKey(GitlabObject):
@@ -1173,11 +1160,8 @@ def Content(self, **kwargs):
11731160
url= ("/projects/%(project_id)s/snippets/%(snippet_id)s/raw"%
11741161
{'project_id':self.project_id,'snippet_id':self.id})
11751162
r=self.gitlab._raw_get(url,**kwargs)
1176-
1177-
ifr.status_code==200:
1178-
returnr.content
1179-
else:
1180-
_raise_error_from_response(r,GitlabGetError)
1163+
_raise_error_from_response(r,GitlabGetError)
1164+
returnr.content
11811165

11821166
defNote(self,id=None,**kwargs):
11831167
returnProjectSnippetNote._get_list_or_object(
@@ -1288,29 +1272,23 @@ def tree(self, path='', ref_name='', **kwargs):
12881272
url="%s/%s/repository/tree"% (self._url,self.id)
12891273
url+='?path=%s&ref_name=%s'% (path,ref_name)
12901274
r=self.gitlab._raw_get(url,**kwargs)
1291-
ifr.status_code==200:
1292-
returnr.json()
1293-
else:
1294-
_raise_error_from_response(r,GitlabGetError)
1275+
_raise_error_from_response(r,GitlabGetError)
1276+
returnr.json()
12951277

12961278
defblob(self,sha,filepath,**kwargs):
12971279
url="%s/%s/repository/blobs/%s"% (self._url,self.id,sha)
12981280
url+='?filepath=%s'% (filepath)
12991281
r=self.gitlab._raw_get(url,**kwargs)
1300-
ifr.status_code==200:
1301-
returnr.content
1302-
else:
1303-
_raise_error_from_response(r,GitlabGetError)
1282+
_raise_error_from_response(r,GitlabGetError)
1283+
returnr.content
13041284

13051285
defarchive(self,sha=None,**kwargs):
13061286
url='/projects/%s/repository/archive'%self.id
13071287
ifsha:
13081288
url+='?sha=%s'%sha
13091289
r=self.gitlab._raw_get(url,**kwargs)
1310-
ifr.status_code==200:
1311-
returnr.content
1312-
else:
1313-
_raise_error_from_response(r,GitlabGetError)
1290+
_raise_error_from_response(r,GitlabGetError)
1291+
returnr.content
13141292

13151293
defcreate_file(self,path,branch,content,message,**kwargs):
13161294
"""Creates file in project repository
@@ -1330,24 +1308,21 @@ def create_file(self, path, branch, content, message, **kwargs):
13301308
url+= ("?file_path=%s&branch_name=%s&content=%s&commit_message=%s"%
13311309
(path,branch,content,message))
13321310
r=self.gitlab._raw_post(url,data=None,content_type=None,**kwargs)
1333-
ifr.status_code!=201:
1334-
_raise_error_from_response(r,GitlabCreateError)
1311+
_raise_error_from_response(r,GitlabCreateError,201)
13351312

13361313
defupdate_file(self,path,branch,content,message,**kwargs):
13371314
url="/projects/%s/repository/files"%self.id
13381315
url+= ("?file_path=%s&branch_name=%s&content=%s&commit_message=%s"%
13391316
(path,branch,content,message))
13401317
r=self.gitlab._raw_put(url,data=None,content_type=None,**kwargs)
1341-
ifr.status_code!=200:
1342-
_raise_error_from_response(r,GitlabUpdateError)
1318+
_raise_error_from_response(r,GitlabUpdateError)
13431319

13441320
defdelete_file(self,path,branch,message,**kwargs):
13451321
url="/projects/%s/repository/files"%self.id
13461322
url+= ("?file_path=%s&branch_name=%s&commit_message=%s"%
13471323
(path,branch,message))
13481324
r=self.gitlab._raw_delete(url,**kwargs)
1349-
ifr.status_code!=200:
1350-
_raise_error_from_response(r,GitlabDeleteError)
1325+
_raise_error_from_response(r,GitlabDeleteError)
13511326

13521327

13531328
classTeamMember(GitlabObject):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp