Skip to content

Moved url-construction to separate function #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions gitlab.py
Original file line number Diff line number Diff line change
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