Skip to content

Commit c4920ee

Browse files
author
Gauvain Pocentek
committed
drop Session() and add a Gitlab.authenticate() method
1 parent bf25928 commit c4920ee

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

gitlab.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,33 @@ class GitlabCreateError(Exception):
3030
class GitlabUpdateError(Exception):
3131
pass
3232

33-
class GitlabSessionError(Exception):
33+
class GitlabAuthenticationError(Exception):
3434
pass
3535

3636
class Gitlab(object):
37-
def __init__(self, url, private_token):
37+
def __init__(self, url, private_token=None, email=None, password=None):
3838
self.url = '%s/api/v3'%url
3939
self.private_token = private_token
40+
self.email = email
41+
self.password = password
42+
43+
if not self.private_token:
44+
self.authenticate
45+
46+
def authenticate(self, email=None, password=None):
47+
self.email = self.email or email
48+
self.password = self.password or password
49+
50+
if not self.email or not self.password:
51+
raise GitlabAuthenticationError("Missing email/password")
52+
53+
r = self.rawPost('/session', {'email': email, 'password': password})
54+
if r.status_code == 201:
55+
self.user = User(self, r.json)
56+
else:
57+
raise GitlabAuthenticationError()
58+
59+
self.private_token = self.user.private_token
4060

4161
def setUrl(self, url):
4262
self.url = '%s/api/v3'%url
@@ -234,13 +254,16 @@ def getObject(self, k, v):
234254

235255
def __init__(self, gl, data):
236256
self.gitlab = gl
257+
237258
for k, v in data.items():
238259
if isinstance (v, list):
239260
self.__dict__[k] = []
240261
for i in v:
241262
self.__dict__[k].append(self.getObject(k,i))
242-
else:
263+
elif v:
243264
self.__dict__[k] = self.getObject(k,v)
265+
else: # None object
266+
self.__dict__[k] = None
244267

245268
def __str__(self):
246269
return '%s => %s'%(type(self), str(self.__dict__))
@@ -275,13 +298,6 @@ class Issue(GitlabObject):
275298
canUpdate = False
276299
canCreate = False
277300

278-
def Session(gl, email, password):
279-
r = gl.rawPost('/session', {'email': email, 'password': password})
280-
if r.status_code == 201:
281-
return User(gl, r.json)
282-
else:
283-
raise GitlabSessionError()
284-
285301
class ProjectBranch(GitlabObject):
286302
url = '/projects/%(project_id)d/repository/branches'
287303
canDelete = False

0 commit comments

Comments
 (0)