From e14e3bf0f675c54930af53c832ccd7ab98df89f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20M=C3=A4enp=C3=A4=C3=A4?= Date: Thu, 9 Oct 2014 12:01:42 +0300 Subject: [PATCH] Moved url-construction to separate function --- gitlab.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/gitlab.py b/gitlab.py index 93cc2dfdc..1f930addb 100644 --- a/gitlab.py +++ b/gitlab.py @@ -126,6 +126,15 @@ def setUrl(self, url): """Updates the gitlab URL""" self._url = '%s/api/v3' % url + def constructUrl(self, id_, obj, parameters): + args = _sanitize_dict(parameters) + url = obj._url % args + if id_ is not None: + url = '%s%s/%s' % (self._url, url, str(id_)) + else: + url = '%s%s' % (self._url, url) + return url + def setToken(self, token): """Sets the private token for authentication""" self.private_token = token if token else None @@ -195,9 +204,8 @@ def list(self, obj_class, **kwargs): raise GitlabListError('Missing attribute(s): %s' % ", ".join(missing)) + url = self.constructUrl(id_=None, obj=obj_class, parameters=kwargs) args = _sanitize_dict(kwargs) - url = obj_class._url % args - url = '%s%s' % (self._url, url) if args: url += "?%s" % ("&".join( ["%s=%s" % (k, v) for k, v in args.items()])) @@ -235,11 +243,7 @@ def get(self, obj_class, id=None, **kwargs): raise GitlabListError('Missing attribute(s): %s' % ", ".join(missing)) - url = obj_class._url % _sanitize_dict(kwargs) - if id is not None: - url = '%s%s/%s' % (self._url, url, str(id)) - else: - url = '%s%s' % (self._url, url) + url = self.constructUrl(id_=id, obj=obj_class, parameters=kwargs) try: r = requests.get(url, headers=self.headers, verify=self.ssl_verify, @@ -258,9 +262,7 @@ def get(self, obj_class, id=None, **kwargs): raise GitlabGetError('%d: %s' % (r.status_code, r.text)) def delete(self, obj): - args = _sanitize_dict(obj.__dict__) - url = obj._url % args - url = '%s%s/%s' % (self._url, url, args['id']) + url = self.constructUrl(id_=obj.id, obj=obj, parameters=obj.__dict__) try: r = requests.delete(url, @@ -288,9 +290,7 @@ def create(self, obj): raise GitlabCreateError('Missing attribute(s): %s' % ", ".join(missing)) - args = _sanitize_dict(obj.__dict__) - url = obj._url % args - url = '%s%s' % (self._url, url) + url = self.constructUrl(id_=None, obj=obj, parameters=obj.__dict__) for k, v in obj.__dict__.items(): if type(v) == bool: @@ -313,9 +313,7 @@ def create(self, obj): raise GitlabCreateError('%d: %s' % (r.status_code, r.text)) def update(self, obj): - args = _sanitize_dict(obj.__dict__) - url = obj._url % args - url = '%s%s/%s' % (self._url, url, str(obj.id)) + url = self.constructUrl(id_=obj.id, obj=obj, parameters=obj.__dict__) # build a dict of data that can really be sent to server d = {}