summaryrefslogtreecommitdiff
path: root/gitlab
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain@pocentek.net>2017-05-28 11:40:44 +0200
committerGauvain Pocentek <gauvain@pocentek.net>2017-06-02 15:41:37 +0200
commita1c9e2bce1d0df0eff0468fabad4919d0565f09f (patch)
tree872a0effed6d8a36523439794f652bd11e239394 /gitlab
parenta50690288f9c03ec37ff374839d1f465c74ecf0a (diff)
downloadgitlab-a1c9e2bce1d0df0eff0468fabad4919d0565f09f.tar.gz
New API: handle gl.auth() and CurrentUser* classes
Diffstat (limited to 'gitlab')
-rw-r--r--gitlab/__init__.py20
-rw-r--r--gitlab/v4/objects.py53
2 files changed, 41 insertions, 32 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
index 50928ee..2ea5e14 100644
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -94,6 +94,7 @@ class Gitlab(object):
objects = importlib.import_module('gitlab.v%s.objects' %
self._api_version)
+ self._objects = objects
self.broadcastmessages = objects.BroadcastMessageManager(self)
self.deploykeys = objects.DeployKeyManager(self)
@@ -191,13 +192,16 @@ class Gitlab(object):
if not self.email or not self.password:
raise GitlabAuthenticationError("Missing email/password")
- data = json.dumps({'email': self.email, 'password': self.password})
- r = self._raw_post('/session', data, content_type='application/json')
- raise_error_from_response(r, GitlabAuthenticationError, 201)
- self.user = CurrentUser(self, r.json())
- """(gitlab.objects.CurrentUser): Object representing the user currently
- logged.
- """
+ if self.api_version == '3':
+ data = json.dumps({'email': self.email, 'password': self.password})
+ r = self._raw_post('/session', data,
+ content_type='application/json')
+ raise_error_from_response(r, GitlabAuthenticationError, 201)
+ self.user = objects.CurrentUser(self, r.json())
+ else:
+ manager = self._objects.CurrentUserManager()
+ self.user = credentials_auth(self.email, self.password)
+
self._set_token(self.user.private_token)
def token_auth(self):
@@ -207,7 +211,7 @@ class Gitlab(object):
self._token_auth()
def _token_auth(self):
- self.user = CurrentUser(self)
+ self.user = self._objects.CurrentUserManager(self).get()
def version(self):
"""Returns the version and revision of the gitlab server.
diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py
index 34100d8..62bb046 100644
--- a/gitlab/v4/objects.py
+++ b/gitlab/v4/objects.py
@@ -180,41 +180,46 @@ class UserManager(CRUDMixin, RESTManager):
return new_data
-class CurrentUserEmail(GitlabObject):
- _url = '/user/emails'
- canUpdate = False
- shortPrintAttr = 'email'
- requiredCreateAttrs = ['email']
+class CurrentUserEmail(RESTObject):
+ _short_print_attr = 'email'
-class CurrentUserEmailManager(BaseManager):
- obj_cls = CurrentUserEmail
+class CurrentUserEmailManager(RetrieveMixin, CreateMixin, DeleteMixin,
+ RESTManager):
+ _path = '/user/emails'
+ _obj_cls = CurrentUserEmail
+ _create_attrs = {'required': ('email', ), 'optional': tuple()}
-class CurrentUserKey(GitlabObject):
- _url = '/user/keys'
- canUpdate = False
- shortPrintAttr = 'title'
- requiredCreateAttrs = ['title', 'key']
+class CurrentUserKey(RESTObject):
+ _short_print_attr = 'title'
-class CurrentUserKeyManager(BaseManager):
- obj_cls = CurrentUserKey
+class CurrentUserKeyManager(RetrieveMixin, CreateMixin, DeleteMixin,
+ RESTManager):
+ _path = '/user/keys'
+ _obj_cls = CurrentUserKey
+ _create_attrs = {'required': ('title', 'key'), 'optional': tuple()}
-class CurrentUser(GitlabObject):
- _url = '/user'
- canList = False
- canCreate = False
- canUpdate = False
- canDelete = False
- shortPrintAttr = 'username'
- managers = (
- ('emails', 'CurrentUserEmailManager', [('user_id', 'id')]),
- ('keys', 'CurrentUserKeyManager', [('user_id', 'id')]),
+class CurrentUser(RESTObject):
+ _id_attr = None
+ _short_print_attr = 'username'
+ _managers = (
+ ('emails', 'CurrentUserEmailManager'),
+ ('keys', 'CurrentUserKeyManager'),
)
+class CurrentUserManager(GetWithoutIdMixin, RESTManager):
+ _path = '/user'
+ _obj_cls = CurrentUser
+
+ def credentials_auth(self, email, password):
+ data = {'email': email, 'password': password}
+ server_data = self.gitlab.http_post('/session', post_data=data)
+ return CurrentUser(self, server_data)
+
class ApplicationSettings(SaveMixin, RESTObject):
_id_attr = None