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

Commitfe96edf

Browse files
author
Gauvain Pocentek
committed
Refactor the Gitlab class
Make use of the _raw_* methods in the CRUD methods.
1 parente0d226b commitfe96edf

File tree

1 file changed

+25
-135
lines changed

1 file changed

+25
-135
lines changed

‎gitlab/__init__.py

Lines changed: 25 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,9 @@ def _construct_url(self, id_, obj, parameters, action=None):
276276
url=obj_url%args
277277

278278
ifid_isnotNone:
279-
url='%s%s/%s'% (self._url,url,str(id_))
279+
return'%s/%s'% (url,str(id_))
280280
else:
281-
url='%s%s'% (self._url,url)
282-
returnurl
281+
returnurl
283282

284283
def_create_headers(self,content_type=None,headers={}):
285284
request_headers=self.headers.copy()
@@ -325,7 +324,11 @@ def enable_debug(self):
325324
requests_log.propagate=True
326325

327326
def_raw_get(self,path,content_type=None,streamed=False,**kwargs):
328-
url='%s%s'% (self._url,path)
327+
ifpath.startswith('http://')orpath.startswith('https://'):
328+
url=path
329+
else:
330+
url='%s%s'% (self._url,path)
331+
329332
headers=self._create_headers(content_type)
330333
try:
331334
returnself.session.get(url,
@@ -342,23 +345,24 @@ def _raw_get(self, path, content_type=None, streamed=False, **kwargs):
342345
"Can't connect to GitLab server (%s)"%e)
343346

344347
def_raw_list(self,path,cls,extra_attrs={},**kwargs):
345-
r=self._raw_get(path,**kwargs)
346-
raise_error_from_response(r,GitlabListError)
347-
348-
cls_kwargs=extra_attrs.copy()
349-
cls_kwargs.update(kwargs.copy())
348+
params=extra_attrs.copy()
349+
params.update(kwargs.copy())
350350

351-
# Add _from_api manually, because we are not creating objects
352-
# through normal path
353-
cls_kwargs['_from_api']=True
354351
get_all_results=kwargs.get('all',False)
355352

356353
# Remove parameters from kwargs before passing it to constructor
357354
forkeyin ['all','page','per_page','sudo','next_url']:
358-
ifkeyincls_kwargs:
359-
delcls_kwargs[key]
355+
ifkeyinparams:
356+
delparams[key]
357+
358+
r=self._raw_get(path,**params)
359+
raise_error_from_response(r,GitlabListError)
360360

361-
results= [cls(self,item,**cls_kwargs)foriteminr.json()
361+
# Add _from_api manually, because we are not creating objects
362+
# through normal path
363+
params['_from_api']=True
364+
365+
results= [cls(self,item,**params)foriteminr.json()
362366
ifitemisnotNone]
363367
if ('next'inr.linksand'url'inr.links['next']
364368
andget_all_resultsisTrue):
@@ -439,52 +443,8 @@ def list(self, obj_class, **kwargs):
439443
", ".join(missing))
440444

441445
url=self._construct_url(id_=None,obj=obj_class,parameters=kwargs)
442-
headers=self._create_headers()
443-
444-
# Remove attributes that are used in url so that there is only
445-
# url-parameters left
446-
params=kwargs.copy()
447-
forattributeinobj_class.requiredUrlAttrs:
448-
delparams[attribute]
449-
450-
# Also remove the next-url attribute that make queries fail
451-
if'next_url'inparams:
452-
delparams['next_url']
453-
try:
454-
r=self.session.get(url,params=params,headers=headers,
455-
verify=self.ssl_verify,
456-
timeout=self.timeout,
457-
auth=requests.auth.HTTPBasicAuth(
458-
self.http_username,
459-
self.http_password))
460-
exceptExceptionase:
461-
raiseGitlabConnectionError(
462-
"Can't connect to GitLab server (%s)"%e)
463-
464-
raise_error_from_response(r,GitlabListError)
465446

466-
cls=obj_class
467-
cls_kwargs=kwargs.copy()
468-
469-
# Add _from_api manually, because we are not creating objects
470-
# through normal path
471-
cls_kwargs['_from_api']=True
472-
473-
get_all_results=params.get('all',False)
474-
475-
# Remove parameters from kwargs before passing it to constructor
476-
forkeyin ['all','page','per_page','sudo','next_url']:
477-
ifkeyincls_kwargs:
478-
delcls_kwargs[key]
479-
480-
results= [cls(self,item,**cls_kwargs)foriteminr.json()
481-
ifitemisnotNone]
482-
if ('next'inr.linksand'url'inr.links['next']
483-
andget_all_resultsisTrue):
484-
args=kwargs.copy()
485-
args['next_url']=r.links['next']['url']
486-
results.extend(self.list(obj_class,**args))
487-
returnresults
447+
returnself._raw_list(url,obj_class,**kwargs)
488448

489449
defget(self,obj_class,id=None,**kwargs):
490450
"""Request a GitLab resources.
@@ -510,27 +470,10 @@ def get(self, obj_class, id=None, **kwargs):
510470
raiseGitlabGetError('Missing attribute(s): %s'%
511471
", ".join(missing))
512472

513-
sanitized_id=_sanitize(id)
514-
url=self._construct_url(id_=sanitized_id,obj=obj_class,
473+
url=self._construct_url(id_=_sanitize(id),obj=obj_class,
515474
parameters=kwargs)
516-
headers=self._create_headers()
517-
518-
# Remove attributes that are used in url so that there is only
519-
# url-parameters left
520-
params=kwargs.copy()
521-
forattributeinobj_class.requiredUrlAttrs:
522-
delparams[attribute]
523-
524-
try:
525-
r=self.session.get(url,params=params,headers=headers,
526-
verify=self.ssl_verify,timeout=self.timeout,
527-
auth=requests.auth.HTTPBasicAuth(
528-
self.http_username,
529-
self.http_password))
530-
exceptExceptionase:
531-
raiseGitlabConnectionError(
532-
"Can't connect to GitLab server (%s)"%e)
533475

476+
r=self._raw_get(url,**kwargs)
534477
raise_error_from_response(r,GitlabGetError)
535478
returnr.json()
536479

@@ -572,30 +515,13 @@ def delete(self, obj, id=None, **kwargs):
572515

573516
obj_id=params[obj.idAttr]ifobj._id_in_delete_urlelseNone
574517
url=self._construct_url(id_=obj_id,obj=obj,parameters=params)
575-
headers=self._create_headers()
576518

577-
# Remove attributes that are used in url so that there is only
578-
# url-parameters left
579-
forattributeinobj.requiredUrlAttrs:
580-
delparams[attribute]
581519
ifobj._id_in_delete_url:
582520
# The ID is already built, no need to add it as extra key in query
583521
# string
584522
params.pop(obj.idAttr)
585523

586-
try:
587-
r=self.session.delete(url,
588-
params=params,
589-
headers=headers,
590-
verify=self.ssl_verify,
591-
timeout=self.timeout,
592-
auth=requests.auth.HTTPBasicAuth(
593-
self.http_username,
594-
self.http_password))
595-
exceptExceptionase:
596-
raiseGitlabConnectionError(
597-
"Can't connect to GitLab server (%s)"%e)
598-
524+
r=self._raw_delete(url,**params)
599525
raise_error_from_response(r,GitlabDeleteError)
600526
returnTrue
601527

@@ -630,23 +556,11 @@ def create(self, obj, **kwargs):
630556

631557
url=self._construct_url(id_=None,obj=obj,parameters=params,
632558
action='create')
633-
headers=self._create_headers(content_type="application/json")
634559

635560
# build data that can really be sent to server
636561
data=obj._data_for_gitlab(extra_parameters=kwargs)
637562

638-
try:
639-
r=self.session.post(url,data=data,
640-
headers=headers,
641-
verify=self.ssl_verify,
642-
timeout=self.timeout,
643-
auth=requests.auth.HTTPBasicAuth(
644-
self.http_username,
645-
self.http_password))
646-
exceptExceptionase:
647-
raiseGitlabConnectionError(
648-
"Can't connect to GitLab server (%s)"%e)
649-
563+
r=self._raw_post(url,data=data,content_type='application/json')
650564
raise_error_from_response(r,GitlabCreateError,201)
651565
returnr.json()
652566

@@ -683,34 +597,10 @@ def update(self, obj, **kwargs):
683597
", ".join(missing))
684598
obj_id=params[obj.idAttr]ifobj._id_in_update_urlelseNone
685599
url=self._construct_url(id_=obj_id,obj=obj,parameters=params)
686-
headers=self._create_headers(content_type="application/json")
687600

688601
# build data that can really be sent to server
689602
data=obj._data_for_gitlab(extra_parameters=kwargs,update=True)
690603

691-
try:
692-
r=self.session.put(url,data=data,
693-
headers=headers,
694-
verify=self.ssl_verify,
695-
timeout=self.timeout,
696-
auth=requests.auth.HTTPBasicAuth(
697-
self.http_username,
698-
self.http_password))
699-
exceptExceptionase:
700-
raiseGitlabConnectionError(
701-
"Can't connect to GitLab server (%s)"%e)
702-
604+
r=self._raw_put(url,data=data,content_type='application/json')
703605
raise_error_from_response(r,GitlabUpdateError)
704606
returnr.json()
705-
706-
def_list_projects(self,url,**kwargs):
707-
r=self._raw_get(url,**kwargs)
708-
raise_error_from_response(r,GitlabListError)
709-
710-
l= []
711-
foroinr.json():
712-
p=Project(self,o)
713-
p._from_api=True
714-
l.append(p)
715-
716-
returnl

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp