Skip to content

Commit 81b0a06

Browse files
author
Gauvain Pocentek
committed
Move request return_code tests in _raise_error_from_response
1 parent 38f17c1 commit 81b0a06

File tree

1 file changed

+62
-87
lines changed

1 file changed

+62
-87
lines changed

gitlab/__init__.py

+62-87
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+
if expected_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-
if r.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
def token_auth(self):
@@ -342,34 +343,33 @@ def list(self, obj_class, **kwargs):
342343
raise GitlabConnectionError(
343344
"Can't connect to GitLab server (%s)" % self._url)
344345

345-
if r.status_code == 200:
346-
cls = obj_class
347-
if obj_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+
if obj_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-
for key in ['all', 'page', 'per_page', 'sudo']:
360-
if key in cls_kwargs:
361-
del cls_kwargs[key]
358+
get_all_results = params.get('all', False)
362359

363-
results = [cls(self, item, **cls_kwargs) for item in r.json()
364-
if item is not None]
365-
if ('next' in r.links and 'url' in r.links['next']
366-
and get_all_results is True):
367-
args = kwargs.copy()
368-
args['next_url'] = r.links['next']['url']
369-
results.extend(self.list(obj_class, **args))
370-
return results
371-
else:
372-
_raise_error_from_response(r, GitlabListError)
360+
# Remove parameters from kwargs before passing it to constructor
361+
for key in ['all', 'page', 'per_page', 'sudo']:
362+
if key in cls_kwargs:
363+
del cls_kwargs[key]
364+
365+
results = [cls(self, item, **cls_kwargs) for item in r.json()
366+
if item is not None]
367+
if ('next' in r.links and 'url' in r.links['next']
368+
and get_all_results is True):
369+
args = kwargs.copy()
370+
args['next_url'] = r.links['next']['url']
371+
results.extend(self.list(obj_class, **args))
372+
return results
373373

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

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

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

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

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

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

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

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

506498
def Hook(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-
if r.status_code != 200:
543-
_raise_error_from_response(r, GitlabListError)
534+
_raise_error_from_response(r, GitlabListError)
544535

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

929919

930920
class Hook(GitlabObject):
@@ -960,14 +950,12 @@ def protect(self, protect=True, **kwargs):
960950
action = 'protect' if protect else '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-
if r.status_code == 200:
965-
if protect:
966-
self.protected = protect
967-
else:
968-
del self.protected
955+
if protect:
956+
self.protected = protect
969957
else:
970-
_raise_error_from_response(r, GitlabProtectError)
958+
del self.protected
971959

972960
def unprotect(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-
if r.status_code == 200:
989-
return r.json()
990-
else:
991-
_raise_error_from_response(r, GitlabGetError)
976+
_raise_error_from_response(r, GitlabGetError)
977+
978+
return r.json()
992979

993980
def blob(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-
if r.status_code == 200:
999-
return r.content
1000-
else:
1001-
_raise_error_from_response(r, GitlabGetError)
985+
986+
_raise_error_from_response(r, GitlabGetError)
987+
988+
return r.content
1002989

1003990

1004991
class ProjectKey(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-
if r.status_code == 200:
1178-
return r.content
1179-
else:
1180-
_raise_error_from_response(r, GitlabGetError)
1163+
_raise_error_from_response(r, GitlabGetError)
1164+
return r.content
11811165

11821166
def Note(self, id=None, **kwargs):
11831167
return ProjectSnippetNote._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-
if r.status_code == 200:
1292-
return r.json()
1293-
else:
1294-
_raise_error_from_response(r, GitlabGetError)
1275+
_raise_error_from_response(r, GitlabGetError)
1276+
return r.json()
12951277

12961278
def blob(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-
if r.status_code == 200:
1301-
return r.content
1302-
else:
1303-
_raise_error_from_response(r, GitlabGetError)
1282+
_raise_error_from_response(r, GitlabGetError)
1283+
return r.content
13041284

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

13151293
def create_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-
if r.status_code != 201:
1334-
_raise_error_from_response(r, GitlabCreateError)
1311+
_raise_error_from_response(r, GitlabCreateError, 201)
13351312

13361313
def update_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-
if r.status_code != 200:
1342-
_raise_error_from_response(r, GitlabUpdateError)
1318+
_raise_error_from_response(r, GitlabUpdateError)
13431319

13441320
def delete_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-
if r.status_code != 200:
1350-
_raise_error_from_response(r, GitlabDeleteError)
1325+
_raise_error_from_response(r, GitlabDeleteError)
13511326

13521327

13531328
class TeamMember(GitlabObject):

0 commit comments

Comments
 (0)