Skip to content

Commit

Permalink
Moved url-construction to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
mjmaenpaa committed Oct 14, 2014
1 parent 9f134fc commit e14e3bf
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions gitlab.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()]))
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -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 = {}
Expand Down

0 comments on commit e14e3bf

Please sign in to comment.