@@ -83,7 +83,7 @@ def __init__(self, url, private_token=None, email=None, password=None, ssl_verif
8383 password: the user password (associated with email)
8484 """
8585 self ._url = '%s/api/v3' % url
86- self .private_token = private_token
86+ self .setToken ( private_token )
8787 self .email = email
8888 self .password = password
8989 self .ssl_verify = ssl_verify
@@ -94,11 +94,9 @@ def auth(self):
9494
9595 The user attribute will hold a CurrentUser object on success.
9696 """
97- r = False
9897 if self .private_token :
99- r = self .token_auth ()
100-
101- if not r :
98+ self .token_auth ()
99+ else :
102100 self .credentials_auth ()
103101
104102 def credentials_auth (self ):
@@ -112,35 +110,33 @@ def credentials_auth(self):
112110 else :
113111 raise GitlabAuthenticationError (r .json ()['message' ])
114112
115- self .private_token = self .user .private_token
113+ self .setToken ( self .user .private_token )
116114
117115 def token_auth (self ):
118- try :
119- self .user = CurrentUser (self )
120- return True
121- except :
122- return False
116+ self .user = CurrentUser (self )
123117
124118 def setUrl (self , url ):
125119 """Updates the gitlab URL"""
126120 self ._url = '%s/api/v3' % url
127121
128122 def setToken (self , token ):
129123 """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 = {}
131130
132131 def setCredentials (self , email , password ):
133132 """Sets the email/login and password for authentication"""
134133 self .email = email
135134 self .password = password
136135
137- def rawGet (self , path , with_token = False ):
136+ def rawGet (self , path ):
138137 url = '%s%s' % (self ._url , path )
139- if with_token :
140- url += "?private_token=%s" % self .private_token
141-
142138 try :
143- r = requests .get (url , verify = self .ssl_verify )
139+ r = requests .get (url , headers = self . headers , verify = self .ssl_verify )
144140 except :
145141 raise GitlabConnectionError (
146142 "Can't connect to GitLab server (%s)" % self ._url )
@@ -150,20 +146,20 @@ def rawGet(self, path, with_token=False):
150146 def rawPost (self , path , data ):
151147 url = '%s%s' % (self ._url , path )
152148 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 )
154152 except :
155153 raise GitlabConnectionError (
156154 "Can't connect to GitLab server (%s)" % self ._url )
157155
158156 return r
159157
160- def rawPut (self , path , with_token = False ):
158+ def rawPut (self , path ):
161159 url = '%s%s' % (self ._url , path )
162- if with_token :
163- url += "?private_token=%s" % self .private_token
164160
165161 try :
166- r = requests .put (url , verify = self .ssl_verify )
162+ r = requests .put (url , headers = self . headers , verify = self .ssl_verify )
167163 except :
168164 raise GitlabConnectionError (
169165 "Can't connect to GitLab server (%s)" % self ._url )
@@ -182,13 +178,13 @@ def list(self, obj_class, **kwargs):
182178 url = obj_class ._url
183179 if kwargs :
184180 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 )
186182 if kwargs :
187- url += "& %s" % ("&" .join (
183+ url += "? %s" % ("&" .join (
188184 ["%s=%s" % (k , v ) for k , v in kwargs .items ()]))
189185
190186 try :
191- r = requests .get (url , verify = self .ssl_verify )
187+ r = requests .get (url , headers = self . headers , verify = self .ssl_verify )
192188 except :
193189 raise GitlabConnectionError (
194190 "Can't connect to GitLab server (%s)" % self ._url )
@@ -224,17 +220,17 @@ def get(self, obj_class, id=None, **kwargs):
224220 url = obj_class ._url % kwargs
225221 if id is not None :
226222 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 )
229225 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 )
232228 else :
233- url = '%s%s?private_token=%s ' % \
234- (self ._url , url , self . private_token )
229+ url = '%s%s' % \
230+ (self ._url , url )
235231
236232 try :
237- r = requests .get (url , verify = self .ssl_verify )
233+ r = requests .get (url , headers = self . headers , verify = self .ssl_verify )
238234 except :
239235 raise GitlabConnectionError (
240236 "Can't connect to GitLab server (%s)" % self ._url )
@@ -250,11 +246,11 @@ def get(self, obj_class, id=None, **kwargs):
250246
251247 def delete (self , obj ):
252248 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 )
255251
256252 try :
257- r = requests .delete (url , verify = self .ssl_verify )
253+ r = requests .delete (url , headers = self . headers , verify = self .ssl_verify )
258254 except :
259255 raise GitlabConnectionError (
260256 "Can't connect to GitLab server (%s)" % self ._url )
@@ -277,12 +273,12 @@ def create(self, obj):
277273 ", " .join (missing ))
278274
279275 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 )
281277
282278 try :
283279 # TODO: avoid too much work on the server side by filtering the
284280 # __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 )
286282 except :
287283 raise GitlabConnectionError (
288284 "Can't connect to GitLab server (%s)" % self ._url )
@@ -296,8 +292,8 @@ def create(self, obj):
296292
297293 def update (self , obj ):
298294 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 )
301297
302298 # build a dict of data that can really be sent to server
303299 d = {}
@@ -306,7 +302,7 @@ def update(self, obj):
306302 d [k ] = str (v )
307303
308304 try :
309- r = requests .put (url , d , verify = self .ssl_verify )
305+ r = requests .put (url , d , headers = self . headers , verify = self .ssl_verify )
310306 except :
311307 raise GitlabConnectionError (
312308 "Can't connect to GitLab server (%s)" % self ._url )
@@ -588,8 +584,8 @@ class Group(GitlabObject):
588584 shortPrintAttr = 'name'
589585
590586 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 )
593589 r = self .gitlab .rawPost (url , None )
594590 if r .status_code != 201 :
595591 raise GitlabTransferProjectError ()
@@ -628,7 +624,7 @@ def protect(self, protect=True):
628624 url = "%s/%s/protect" % (url , self .name )
629625 else :
630626 url = "%s/%s/unprotect" % (url , self .name )
631- r = self .gitlab .rawPut (url , True )
627+ r = self .gitlab .rawPut (url )
632628
633629 if r .status_code == 200 :
634630 if protect :
@@ -785,7 +781,7 @@ class ProjectSnippet(GitlabObject):
785781 def Content (self ):
786782 url = "/projects/%(project_id)s/snippets/%(snippet_id)s/raw" % \
787783 {'project_id' : self .project_id , 'snippet_id' : self .id }
788- r = self .gitlab .rawGet (url , True )
784+ r = self .gitlab .rawGet (url )
789785
790786 if r .status_code == 200 :
791787 return r .content
0 commit comments