summaryrefslogtreecommitdiff
path: root/gitlab.py
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain@pocentek.net>2013-02-11 07:24:47 +0100
committerGauvain Pocentek <gauvain@pocentek.net>2013-02-11 07:24:47 +0100
commitc4920ee8ee91c27fa9603c36b4e98dfe5ec14244 (patch)
tree22022ba1e8606523ef2f5aafe3c76b50b1dfa8f0 /gitlab.py
parentbf2592814963a8ce356beeff1709afe60838174b (diff)
downloadgitlab-c4920ee8ee91c27fa9603c36b4e98dfe5ec14244.tar.gz
drop Session() and add a Gitlab.authenticate() method
Diffstat (limited to 'gitlab.py')
-rw-r--r--gitlab.py36
1 files changed, 26 insertions, 10 deletions
diff --git a/gitlab.py b/gitlab.py
index 942ada0..7d9d693 100644
--- a/gitlab.py
+++ b/gitlab.py
@@ -30,13 +30,33 @@ class GitlabCreateError(Exception):
class GitlabUpdateError(Exception):
pass
-class GitlabSessionError(Exception):
+class GitlabAuthenticationError(Exception):
pass
class Gitlab(object):
- def __init__(self, url, private_token):
+ def __init__(self, url, private_token=None, email=None, password=None):
self.url = '%s/api/v3'%url
self.private_token = private_token
+ self.email = email
+ self.password = password
+
+ if not self.private_token:
+ self.authenticate
+
+ def authenticate(self, email=None, password=None):
+ self.email = self.email or email
+ self.password = self.password or password
+
+ if not self.email or not self.password:
+ raise GitlabAuthenticationError("Missing email/password")
+
+ r = self.rawPost('/session', {'email': email, 'password': password})
+ if r.status_code == 201:
+ self.user = User(self, r.json)
+ else:
+ raise GitlabAuthenticationError()
+
+ self.private_token = self.user.private_token
def setUrl(self, url):
self.url = '%s/api/v3'%url
@@ -234,13 +254,16 @@ class GitlabObject(object):
def __init__(self, gl, data):
self.gitlab = gl
+
for k, v in data.items():
if isinstance (v, list):
self.__dict__[k] = []
for i in v:
self.__dict__[k].append(self.getObject(k,i))
- else:
+ elif v:
self.__dict__[k] = self.getObject(k,v)
+ else: # None object
+ self.__dict__[k] = None
def __str__(self):
return '%s => %s'%(type(self), str(self.__dict__))
@@ -275,13 +298,6 @@ class Issue(GitlabObject):
canUpdate = False
canCreate = False
-def Session(gl, email, password):
- r = gl.rawPost('/session', {'email': email, 'password': password})
- if r.status_code == 201:
- return User(gl, r.json)
- else:
- raise GitlabSessionError()
-
class ProjectBranch(GitlabObject):
url = '/projects/%(project_id)d/repository/branches'
canDelete = False