@@ -43,30 +43,43 @@ def __init__(self, url, private_token=None, email=None, password=None):
43
43
self .email = email
44
44
self .password = password
45
45
46
- if not self .private_token :
47
- self .authenticate
46
+ def auth (self ):
47
+ r = False
48
+ if self .private_token :
49
+ r = self .token_auth ()
48
50
49
- def authenticate (self , email = None , password = None ):
50
- self .email = self .email or email
51
- self .password = self .password or password
51
+ if not r :
52
+ self .credentials_auth ()
52
53
54
+ def credentials_auth (self ):
53
55
if not self .email or not self .password :
54
56
raise GitlabAuthenticationError ("Missing email/password" )
55
57
56
- r = self .rawPost ('/session' , {'email' : email , 'password' : password })
58
+ r = self .rawPost ('/session' , {'email' : self . email , 'password' : self . password })
57
59
if r .status_code == 201 :
58
60
self .user = CurrentUser (self , r .json )
59
61
else :
60
62
raise GitlabAuthenticationError (r .json ['message' ])
61
63
62
64
self .private_token = self .user .private_token
63
65
66
+ def token_auth (self ):
67
+ try :
68
+ self .user = self .get (CurrentUser )
69
+ return True
70
+ except :
71
+ return False
72
+
64
73
def setUrl (self , url ):
65
74
self .url = '%s/api/v3' % url
66
75
67
76
def setToken (self , token ):
68
77
self .private_token = token
69
78
79
+ def setCredentials (self , email , password ):
80
+ self .email = email
81
+ self .password = password
82
+
70
83
def rawPost (self , path , data ):
71
84
url = '%s%s' % (self .url , path )
72
85
try :
@@ -104,11 +117,14 @@ def list(self, objClass, **kwargs):
104
117
else :
105
118
raise GitlabGetError ('%d: %s' % (r .status_code , r .text ))
106
119
107
- def get (self , objClass , id , ** kwargs ):
120
+ def get (self , objClass , id = None , ** kwargs ):
108
121
url = objClass .url
109
122
if kwargs :
110
123
url = objClass .url % kwargs
111
- url = '%s%s/%d?private_token=%s' % (self .url , url , id , self .private_token )
124
+ if id != None :
125
+ url = '%s%s/%d?private_token=%s' % (self .url , url , id , self .private_token )
126
+ else :
127
+ url = '%s%s?private_token=%s' % (self .url , url , self .private_token )
112
128
113
129
try :
114
130
r = requests .get (url )
@@ -273,10 +289,10 @@ def delete(self):
273
289
274
290
return self .gitlab .delete (self )
275
291
276
- def __init__ (self , gl , data , ** kwargs ):
292
+ def __init__ (self , gl , data = None , ** kwargs ):
277
293
self .gitlab = gl
278
294
279
- if isinstance (data , int ):
295
+ if data is None or isinstance (data , int ):
280
296
data = self .gitlab .get (self .__class__ , data , ** kwargs )
281
297
282
298
self .setFromDict (data )
@@ -442,14 +458,16 @@ def Tag(self, id=None):
442
458
return self .getListOrObject (ProjectTag , id , project_id = self .id )
443
459
444
460
if __name__ == '__main__' :
445
- # quick "doc"
461
+ # Quick "doc"
446
462
#
447
463
# See https://github.com/gitlabhq/gitlabhq/tree/master/doc/api for the
448
464
# source
449
465
450
- # register a connection to a gitlab instance, using its URL and a user
466
+ # Register a connection to a gitlab instance, using its URL and a user
451
467
# private token
452
468
gl = Gitlab ('http://192.168.123.107:8080' , 'JVNSESs8EwWRx5yDxM5q' )
469
+ # Connect to get the current user (as gl.user)
470
+ gl .auth ()
453
471
454
472
# get a list of projects
455
473
for p in gl .Project ():
0 commit comments