Skip to content

Commit

Permalink
drop Session() and add a Gitlab.authenticate() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauvain Pocentek committed Feb 11, 2013
1 parent bf25928 commit c4920ee
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions gitlab.py
Expand Up @@ -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
Expand Down Expand Up @@ -234,13 +254,16 @@ def getObject(self, k, v):

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__))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c4920ee

Please sign in to comment.