Skip to content

Commit 221f418

Browse files
author
Gauvain Pocentek
committed
Merge pull request #42 from mjmaenpaa/constructUrl
Moved url-construction to separate function
2 parents 9736e0b + e14e3bf commit 221f418

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

gitlab.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ def setUrl(self, url):
126126
"""Updates the gitlab URL"""
127127
self._url = '%s/api/v3' % url
128128

129+
def constructUrl(self, id_, obj, parameters):
130+
args = _sanitize_dict(parameters)
131+
url = obj._url % args
132+
if id_ is not None:
133+
url = '%s%s/%s' % (self._url, url, str(id_))
134+
else:
135+
url = '%s%s' % (self._url, url)
136+
return url
137+
129138
def setToken(self, token):
130139
"""Sets the private token for authentication"""
131140
self.private_token = token if token else None
@@ -195,9 +204,8 @@ def list(self, obj_class, **kwargs):
195204
raise GitlabListError('Missing attribute(s): %s' %
196205
", ".join(missing))
197206

207+
url = self.constructUrl(id_=None, obj=obj_class, parameters=kwargs)
198208
args = _sanitize_dict(kwargs)
199-
url = obj_class._url % args
200-
url = '%s%s' % (self._url, url)
201209
if args:
202210
url += "?%s" % ("&".join(
203211
["%s=%s" % (k, v) for k, v in args.items()]))
@@ -235,11 +243,7 @@ def get(self, obj_class, id=None, **kwargs):
235243
raise GitlabGetError('Missing attribute(s): %s' %
236244
", ".join(missing))
237245

238-
url = obj_class._url % _sanitize_dict(kwargs)
239-
if id is not None:
240-
url = '%s%s/%s' % (self._url, url, str(id))
241-
else:
242-
url = '%s%s' % (self._url, url)
246+
url = self.constructUrl(id_=id, obj=obj_class, parameters=kwargs)
243247

244248
try:
245249
r = requests.get(url, headers=self.headers, verify=self.ssl_verify,
@@ -258,9 +262,7 @@ def get(self, obj_class, id=None, **kwargs):
258262
raise GitlabGetError('%d: %s' % (r.status_code, r.text))
259263

260264
def delete(self, obj):
261-
args = _sanitize_dict(obj.__dict__)
262-
url = obj._url % args
263-
url = '%s%s/%s' % (self._url, url, args['id'])
265+
url = self.constructUrl(id_=obj.id, obj=obj, parameters=obj.__dict__)
264266

265267
try:
266268
r = requests.delete(url,
@@ -288,9 +290,7 @@ def create(self, obj):
288290
raise GitlabCreateError('Missing attribute(s): %s' %
289291
", ".join(missing))
290292

291-
args = _sanitize_dict(obj.__dict__)
292-
url = obj._url % args
293-
url = '%s%s' % (self._url, url)
293+
url = self.constructUrl(id_=None, obj=obj, parameters=obj.__dict__)
294294

295295
for k, v in obj.__dict__.items():
296296
if type(v) == bool:
@@ -313,9 +313,7 @@ def create(self, obj):
313313
raise GitlabCreateError('%d: %s' % (r.status_code, r.text))
314314

315315
def update(self, obj):
316-
args = _sanitize_dict(obj.__dict__)
317-
url = obj._url % args
318-
url = '%s%s/%s' % (self._url, url, str(obj.id))
316+
url = self.constructUrl(id_=obj.id, obj=obj, parameters=obj.__dict__)
319317

320318
# build a dict of data that can really be sent to server
321319
d = {}

0 commit comments

Comments
 (0)