Skip to content

Commit 7193034

Browse files
author
Gauvain Pocentek
committed
New API: handle gl.auth() and CurrentUser* classes
1 parent 5319d0d commit 7193034

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

gitlab/__init__.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def __init__(self, url, private_token=None, email=None, password=None,
9494

9595
objects = importlib.import_module('gitlab.v%s.objects' %
9696
self._api_version)
97+
self._objects = objects
9798

9899
self.broadcastmessages = objects.BroadcastMessageManager(self)
99100
self.deploykeys = objects.DeployKeyManager(self)
@@ -191,13 +192,16 @@ def _credentials_auth(self):
191192
if not self.email or not self.password:
192193
raise GitlabAuthenticationError("Missing email/password")
193194

194-
data = json.dumps({'email': self.email, 'password': self.password})
195-
r = self._raw_post('/session', data, content_type='application/json')
196-
raise_error_from_response(r, GitlabAuthenticationError, 201)
197-
self.user = CurrentUser(self, r.json())
198-
"""(gitlab.objects.CurrentUser): Object representing the user currently
199-
logged.
200-
"""
195+
if self.api_version == '3':
196+
data = json.dumps({'email': self.email, 'password': self.password})
197+
r = self._raw_post('/session', data,
198+
content_type='application/json')
199+
raise_error_from_response(r, GitlabAuthenticationError, 201)
200+
self.user = objects.CurrentUser(self, r.json())
201+
else:
202+
manager = self._objects.CurrentUserManager()
203+
self.user = credentials_auth(self.email, self.password)
204+
201205
self._set_token(self.user.private_token)
202206

203207
def token_auth(self):
@@ -207,7 +211,7 @@ def token_auth(self):
207211
self._token_auth()
208212

209213
def _token_auth(self):
210-
self.user = CurrentUser(self)
214+
self.user = self._objects.CurrentUserManager(self).get()
211215

212216
def version(self):
213217
"""Returns the version and revision of the gitlab server.

gitlab/v4/objects.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -180,41 +180,46 @@ def _sanitize_data(self, data, action):
180180
return new_data
181181

182182

183-
class CurrentUserEmail(GitlabObject):
184-
_url = '/user/emails'
185-
canUpdate = False
186-
shortPrintAttr = 'email'
187-
requiredCreateAttrs = ['email']
183+
class CurrentUserEmail(RESTObject):
184+
_short_print_attr = 'email'
188185

189186

190-
class CurrentUserEmailManager(BaseManager):
191-
obj_cls = CurrentUserEmail
187+
class CurrentUserEmailManager(RetrieveMixin, CreateMixin, DeleteMixin,
188+
RESTManager):
189+
_path = '/user/emails'
190+
_obj_cls = CurrentUserEmail
191+
_create_attrs = {'required': ('email', ), 'optional': tuple()}
192192

193193

194-
class CurrentUserKey(GitlabObject):
195-
_url = '/user/keys'
196-
canUpdate = False
197-
shortPrintAttr = 'title'
198-
requiredCreateAttrs = ['title', 'key']
194+
class CurrentUserKey(RESTObject):
195+
_short_print_attr = 'title'
199196

200197

201-
class CurrentUserKeyManager(BaseManager):
202-
obj_cls = CurrentUserKey
198+
class CurrentUserKeyManager(RetrieveMixin, CreateMixin, DeleteMixin,
199+
RESTManager):
200+
_path = '/user/keys'
201+
_obj_cls = CurrentUserKey
202+
_create_attrs = {'required': ('title', 'key'), 'optional': tuple()}
203203

204204

205-
class CurrentUser(GitlabObject):
206-
_url = '/user'
207-
canList = False
208-
canCreate = False
209-
canUpdate = False
210-
canDelete = False
211-
shortPrintAttr = 'username'
212-
managers = (
213-
('emails', 'CurrentUserEmailManager', [('user_id', 'id')]),
214-
('keys', 'CurrentUserKeyManager', [('user_id', 'id')]),
205+
class CurrentUser(RESTObject):
206+
_id_attr = None
207+
_short_print_attr = 'username'
208+
_managers = (
209+
('emails', 'CurrentUserEmailManager'),
210+
('keys', 'CurrentUserKeyManager'),
215211
)
216212

217213

214+
class CurrentUserManager(GetWithoutIdMixin, RESTManager):
215+
_path = '/user'
216+
_obj_cls = CurrentUser
217+
218+
def credentials_auth(self, email, password):
219+
data = {'email': email, 'password': password}
220+
server_data = self.gitlab.http_post('/session', post_data=data)
221+
return CurrentUser(self, server_data)
222+
218223
class ApplicationSettings(SaveMixin, RESTObject):
219224
_id_attr = None
220225

0 commit comments

Comments
 (0)