@@ -83,7 +83,7 @@ def __init__(self, url, private_token=None, email=None, password=None, ssl_verif
83
83
password: the user password (associated with email)
84
84
"""
85
85
self ._url = '%s/api/v3' % url
86
- self .private_token = private_token
86
+ self .setToken ( private_token )
87
87
self .email = email
88
88
self .password = password
89
89
self .ssl_verify = ssl_verify
@@ -94,11 +94,9 @@ def auth(self):
94
94
95
95
The user attribute will hold a CurrentUser object on success.
96
96
"""
97
- r = False
98
97
if self .private_token :
99
- r = self .token_auth ()
100
-
101
- if not r :
98
+ self .token_auth ()
99
+ else :
102
100
self .credentials_auth ()
103
101
104
102
def credentials_auth (self ):
@@ -112,35 +110,33 @@ def credentials_auth(self):
112
110
else :
113
111
raise GitlabAuthenticationError (r .json ()['message' ])
114
112
115
- self .private_token = self .user .private_token
113
+ self .setToken ( self .user .private_token )
116
114
117
115
def token_auth (self ):
118
- try :
119
- self .user = CurrentUser (self )
120
- return True
121
- except :
122
- return False
116
+ self .user = CurrentUser (self )
123
117
124
118
def setUrl (self , url ):
125
119
"""Updates the gitlab URL"""
126
120
self ._url = '%s/api/v3' % url
127
121
128
122
def setToken (self , token ):
129
123
"""Sets the private token for authentication"""
130
- self .private_token = token
124
+ if token :
125
+ self .private_token = token
126
+ self .headers = {"PRIVATE-TOKEN" : token }
127
+ else :
128
+ self .private_token = None
129
+ self .headers = {}
131
130
132
131
def setCredentials (self , email , password ):
133
132
"""Sets the email/login and password for authentication"""
134
133
self .email = email
135
134
self .password = password
136
135
137
- def rawGet (self , path , with_token = False ):
136
+ def rawGet (self , path ):
138
137
url = '%s%s' % (self ._url , path )
139
- if with_token :
140
- url += "?private_token=%s" % self .private_token
141
-
142
138
try :
143
- r = requests .get (url , verify = self .ssl_verify )
139
+ r = requests .get (url , headers = self . headers , verify = self .ssl_verify )
144
140
except :
145
141
raise GitlabConnectionError (
146
142
"Can't connect to GitLab server (%s)" % self ._url )
@@ -150,20 +146,20 @@ def rawGet(self, path, with_token=False):
150
146
def rawPost (self , path , data ):
151
147
url = '%s%s' % (self ._url , path )
152
148
try :
153
- r = requests .post (url , data , verify = self .ssl_verify )
149
+ r = requests .post (url , data ,
150
+ headers = self .headers ,
151
+ verify = self .ssl_verify )
154
152
except :
155
153
raise GitlabConnectionError (
156
154
"Can't connect to GitLab server (%s)" % self ._url )
157
155
158
156
return r
159
157
160
- def rawPut (self , path , with_token = False ):
158
+ def rawPut (self , path ):
161
159
url = '%s%s' % (self ._url , path )
162
- if with_token :
163
- url += "?private_token=%s" % self .private_token
164
160
165
161
try :
166
- r = requests .put (url , verify = self .ssl_verify )
162
+ r = requests .put (url , headers = self . headers , verify = self .ssl_verify )
167
163
except :
168
164
raise GitlabConnectionError (
169
165
"Can't connect to GitLab server (%s)" % self ._url )
@@ -182,13 +178,13 @@ def list(self, obj_class, **kwargs):
182
178
url = obj_class ._url
183
179
if kwargs :
184
180
url = obj_class ._url % kwargs
185
- url = '%s%s?private_token=%s ' % (self ._url , url , self . private_token )
181
+ url = '%s%s' % (self ._url , url )
186
182
if kwargs :
187
- url += "& %s" % ("&" .join (
183
+ url += "? %s" % ("&" .join (
188
184
["%s=%s" % (k , v ) for k , v in kwargs .items ()]))
189
185
190
186
try :
191
- r = requests .get (url , verify = self .ssl_verify )
187
+ r = requests .get (url , headers = self . headers , verify = self .ssl_verify )
192
188
except :
193
189
raise GitlabConnectionError (
194
190
"Can't connect to GitLab server (%s)" % self ._url )
@@ -224,17 +220,17 @@ def get(self, obj_class, id=None, **kwargs):
224
220
url = obj_class ._url % kwargs
225
221
if id is not None :
226
222
try :
227
- url = '%s%s/%d?private_token=%s ' % \
228
- (self ._url , url , id , self . private_token )
223
+ url = '%s%s/%d' % \
224
+ (self ._url , url , id )
229
225
except TypeError : # id might be a str (ProjectBranch)
230
- url = '%s%s/%s?private_token=%s ' % \
231
- (self ._url , url , id , self . private_token )
226
+ url = '%s%s/%s' % \
227
+ (self ._url , url , id )
232
228
else :
233
- url = '%s%s?private_token=%s ' % \
234
- (self ._url , url , self . private_token )
229
+ url = '%s%s' % \
230
+ (self ._url , url )
235
231
236
232
try :
237
- r = requests .get (url , verify = self .ssl_verify )
233
+ r = requests .get (url , headers = self . headers , verify = self .ssl_verify )
238
234
except :
239
235
raise GitlabConnectionError (
240
236
"Can't connect to GitLab server (%s)" % self ._url )
@@ -250,11 +246,11 @@ def get(self, obj_class, id=None, **kwargs):
250
246
251
247
def delete (self , obj ):
252
248
url = obj ._url % obj .__dict__
253
- url = '%s%s/%d?private_token=%s ' % \
254
- (self ._url , url , obj .id , self . private_token )
249
+ url = '%s%s/%d' % \
250
+ (self ._url , url , obj .id )
255
251
256
252
try :
257
- r = requests .delete (url , verify = self .ssl_verify )
253
+ r = requests .delete (url , headers = self . headers , verify = self .ssl_verify )
258
254
except :
259
255
raise GitlabConnectionError (
260
256
"Can't connect to GitLab server (%s)" % self ._url )
@@ -277,12 +273,12 @@ def create(self, obj):
277
273
", " .join (missing ))
278
274
279
275
url = obj ._url % obj .__dict__
280
- url = '%s%s?private_token=%s ' % (self ._url , url , self . private_token )
276
+ url = '%s%s' % (self ._url , url )
281
277
282
278
try :
283
279
# TODO: avoid too much work on the server side by filtering the
284
280
# __dict__ keys
285
- r = requests .post (url , obj .__dict__ , verify = self .ssl_verify )
281
+ r = requests .post (url , obj .__dict__ , headers = self . headers , verify = self .ssl_verify )
286
282
except :
287
283
raise GitlabConnectionError (
288
284
"Can't connect to GitLab server (%s)" % self ._url )
@@ -296,8 +292,8 @@ def create(self, obj):
296
292
297
293
def update (self , obj ):
298
294
url = obj ._url % obj .__dict__
299
- url = '%s%s/%d?private_token=%s ' % \
300
- (self ._url , url , obj .id , self . private_token )
295
+ url = '%s%s/%d' % \
296
+ (self ._url , url , obj .id )
301
297
302
298
# build a dict of data that can really be sent to server
303
299
d = {}
@@ -306,7 +302,7 @@ def update(self, obj):
306
302
d [k ] = str (v )
307
303
308
304
try :
309
- r = requests .put (url , d , verify = self .ssl_verify )
305
+ r = requests .put (url , d , headers = self . headers , verify = self .ssl_verify )
310
306
except :
311
307
raise GitlabConnectionError (
312
308
"Can't connect to GitLab server (%s)" % self ._url )
@@ -588,8 +584,8 @@ class Group(GitlabObject):
588
584
shortPrintAttr = 'name'
589
585
590
586
def transfer_project (self , id ):
591
- url = '/groups/%d/projects/%d?private_token=%s ' % \
592
- (self .id , id , self . gitlab . private_token )
587
+ url = '/groups/%d/projects/%d' % \
588
+ (self .id , id )
593
589
r = self .gitlab .rawPost (url , None )
594
590
if r .status_code != 201 :
595
591
raise GitlabTransferProjectError ()
@@ -628,7 +624,7 @@ def protect(self, protect=True):
628
624
url = "%s/%s/protect" % (url , self .name )
629
625
else :
630
626
url = "%s/%s/unprotect" % (url , self .name )
631
- r = self .gitlab .rawPut (url , True )
627
+ r = self .gitlab .rawPut (url )
632
628
633
629
if r .status_code == 200 :
634
630
if protect :
@@ -785,7 +781,7 @@ class ProjectSnippet(GitlabObject):
785
781
def Content (self ):
786
782
url = "/projects/%(project_id)s/snippets/%(snippet_id)s/raw" % \
787
783
{'project_id' : self .project_id , 'snippet_id' : self .id }
788
- r = self .gitlab .rawGet (url , True )
784
+ r = self .gitlab .rawGet (url )
789
785
790
786
if r .status_code == 200 :
791
787
return r .content
0 commit comments