summaryrefslogtreecommitdiff
path: root/gitlab.py
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain@pocentek.net>2013-02-11 15:34:35 +0100
committerGauvain Pocentek <gauvain@pocentek.net>2013-02-11 15:34:35 +0100
commit653843d7e5e426009f648730877919f1a9ff1758 (patch)
tree5c10624dca9bdcdac1f68656a503250f0ef2ddc9 /gitlab.py
parentd1f80da4cf7f0282cddba20038828b12e4e32c6d (diff)
downloadgitlab-653843d7e5e426009f648730877919f1a9ff1758.tar.gz
Rework the API
objects can be created using the usual syntax, with one optioanl argument (int to get one object, dict to create an object, nothing to get a list of items). A save() method creates/updates the object on the server. A delete() method removes it from the server.
Diffstat (limited to 'gitlab.py')
-rw-r--r--gitlab.py168
1 files changed, 81 insertions, 87 deletions
diff --git a/gitlab.py b/gitlab.py
index 7494332..e141211 100644
--- a/gitlab.py
+++ b/gitlab.py
@@ -30,6 +30,9 @@ class GitlabCreateError(Exception):
class GitlabUpdateError(Exception):
pass
+class GitlabDeleteError(Exception):
+ pass
+
class GitlabAuthenticationError(Exception):
pass
@@ -113,26 +116,15 @@ class Gitlab(object):
raise GitlabConnectionError('Can\'t connect to GitLab server (%s)'%self.url)
if r.status_code == 200:
- cls = objClass
- if objClass.returnClass:
- cls = objClass.returnClass
-
- obj = cls(self, r.json)
- if kwargs:
- for k,v in kwargs.items():
- obj.__dict__[k] = v
- return obj
+ return r.json
elif r.status_code == 401:
raise GitlabAuthenticationError(r.json['message'])
else:
raise GitlabGetError('%d: %s'%(r.status_code, r.text))
- def delete(self, objClass, id, **kwargs):
- # FIXME: not working for project members, gitlab bug or not?
- url = objClass.url
- if kwargs:
- url = objClass.url % kwargs
- url = '%s%s/%d?private_token=%s'%(self.url, url, id, self.private_token)
+ def delete(self, obj):
+ url = obj.url % obj.__dict__
+ url = '%s%s/%d?private_token=%s'%(self.url, url, obj.id, self.private_token)
try:
r = requests.delete(url)
@@ -145,55 +137,50 @@ class Gitlab(object):
raise GitlabAuthenticationError(r.json['message'])
return False
- def create(self, objClass, objData, **kwargs):
- url = objClass.url
- if kwargs:
- url = objClass.url % kwargs
+ def create(self, obj):
+ url = obj.url % obj.__dict__
url = '%s%s?private_token=%s'%(self.url, url, self.private_token)
try:
- r = requests.post(url, objData)
+ # TODO: avoid too much work on the server side by filtering the __dict__ keys
+ r = requests.post(url, obj.__dict__)
except:
raise GitlabConnectionError('Can\'t connect to GitLab server (%s)'%self.url)
if r.status_code == 201:
- cls = objClass
- if objClass.returnClass:
- cls = objClass.returnClass
-
- return cls(self, r.json)
+ return r.json
elif r.status_code == 401:
raise GitlabAuthenticationError(r.json['message'])
else:
raise GitlabCreateError('%d: %s'%(r.status_code, r.text))
- def update(self, objClass, id, objData, **kwargs):
- url = objClass.url
- if kwargs:
- url = objClass.url % kwargs
- url = '%s%s/%d?private_token=%s'%(self.url, url, id, self.private_token)
+ def update(self, obj):
+ url = obj.url % obj.__dict__
+ url = '%s%s/%d?private_token=%s'%(self.url, url, obj.id, self.private_token)
+
+ # build a dict of data that can really be sent to server
+ d = {}
+ for k,v in obj.__dict__.items():
+ if type(v) in (int, str, unicode, bool):
+ d[k] = v
try:
- r = requests.put(url, objData)
+ r = requests.put(url, d)
except:
raise GitlabConnectionError('Can\'t connect to GitLab server (%s)'%self.url)
if r.status_code == 200:
- cls = objClass
- if objClass.returnClass:
- cls = objClass.returnClass
-
- return cls(self, r.json)
+ return r.json
elif r.status_code == 401:
raise GitlabAuthenticationError(r.json['message'])
else:
- raise GitlabUpdateError('%d: %s'%(r.status_code, r.text))
+ raise GitlabUpdateError('%d: %s' % (r.status_code, r.text))
def getListOrObject(self, cls, id, **kwargs):
if id == None:
return cls.list(self, **kwargs)
else:
- return cls.get(self, id, **kwargs)
+ return cls(self, id, **kwargs)
def Project(self, id=None):
return self.getListOrObject(Project, id)
@@ -219,26 +206,6 @@ class GitlabObject(object):
canDelete = True
@classmethod
- def update(cls, gl, id, data, **kwargs):
- if not cls.canUpdate:
- raise NotImplementedError
-
- if not cls.url:
- raise NotImplementedError
-
- return gl.update(cls, id, data, **kwargs)
-
- @classmethod
- def create(cls, gl, data, **kwargs):
- if not cls.canCreate:
- raise NotImplementedError
-
- if not cls.url:
- raise NotImplementedError
-
- return gl.create(cls, data, **kwargs)
-
- @classmethod
def list(cls, gl, **kwargs):
if not cls.canGetList:
raise NotImplementedError
@@ -248,31 +215,17 @@ class GitlabObject(object):
return gl.list(cls, **kwargs)
- @classmethod
- def get(cls, gl, id, **kwargs):
- if not cls.canGet:
- raise NotImplementedError
-
- if not cls.url:
- raise NotImplementedError
-
- return gl.get(cls, id, **kwargs)
-
- @classmethod
- def delete(cls, gl, id, **kwargs):
- if not cls.canDelete:
- raise NotImplementedError
-
- if not cls.url:
- raise NotImplementedError
-
- return gl.delete(cls, id, **kwargs)
-
def getListOrObject(self, cls, id, **kwargs):
if id == None:
+ if not cls.canGetList:
+ raise GitlabGetError
+
return cls.list(self.gitlab, **kwargs)
else:
- return cls.get(self.gitlab, id, **kwargs)
+ if not cls.canGet:
+ raise GitlabGetError
+
+ return cls(self.gitlab, id, **kwargs)
def getObject(self, k, v):
if self.constructorTypes and k in self.constructorTypes:
@@ -280,9 +233,7 @@ class GitlabObject(object):
else:
return v
- def __init__(self, gl, data):
- self.gitlab = gl
-
+ def setFromDict(self, data):
for k, v in data.items():
if isinstance (v, list):
self.__dict__[k] = []
@@ -293,6 +244,47 @@ class GitlabObject(object):
else: # None object
self.__dict__[k] = None
+ def _create(self):
+ if not self.canCreate:
+ raise NotImplementedError
+
+ json = self.gitlab.create(self)
+ self.setFromDict(json)
+
+ def _update(self):
+ if not self.canUpdate:
+ raise NotImplementedError
+
+ json = self.gitlab.update(self)
+ self.setFromDict(json)
+
+ def save(self):
+ if hasattr(self, 'id'):
+ self._update()
+ else:
+ self._create()
+
+ def delete(self):
+ if not self.canDelete:
+ raise NotImplementedError
+
+ if not hasattr(self, 'id'):
+ raise GitlabDeleteError
+
+ return self.gitlab.delete(self)
+
+ def __init__(self, gl, data, **kwargs):
+ self.gitlab = gl
+
+ if isinstance(data, int):
+ data = self.gitlab.get(self.__class__, data, **kwargs)
+
+ self.setFromDict(data)
+
+ if kwargs:
+ for k,v in kwargs.items():
+ self.__dict__[k] = v
+
def __str__(self):
return '%s => %s'%(type(self), str(self.__dict__))
@@ -315,7 +307,7 @@ class CurrentUser(GitlabObject):
if id == None:
return CurrentUserKey.list(self.gitlab)
else:
- return CurrentUserKey.get(self.gitlab, id)
+ return CurrentUserKey(self.gitlab, id)
class Group(GitlabObject):
url = '/groups'
@@ -460,12 +452,14 @@ if __name__ == '__main__':
gl = Gitlab('http://192.168.123.107:8080', 'JVNSESs8EwWRx5yDxM5q')
# get a list of projects
- for p in Project.list(gl):
+ for p in gl.Project():
print p.name
# get associated issues
- issues = ProjectIssue.list(gl, project_id=p.id)
+ issues = p.Issue()
for issue in issues:
- print " %d => %s (closed: %d)"%(issue.id, issue.title, issue.closed)
+ closed = 0 if not issue.closed else 1
+ print " %d => %s (closed: %d)"%(issue.id, issue.title, closed)
# and close them all
- ProjectIssue.update(gl, issue.id, {'closed': 1}, project_id=p.id)
+ issue.closed = 1
+ issue.save()