@@ -236,13 +236,6 @@ def _construct_url(self, id_, obj, parameters, action=None):
236236 else :
237237 return url
238238
239- def _create_headers (self , content_type = None , headers = {}):
240- request_headers = self .headers .copy ()
241- request_headers .update (headers )
242- if content_type is not None :
243- request_headers ['Content-type' ] = content_type
244- return request_headers
245-
246239 def set_token (self , token ):
247240 """Sets the private token for authentication.
248241
@@ -279,23 +272,36 @@ def enable_debug(self):
279272 requests_log .setLevel (logging .DEBUG )
280273 requests_log .propagate = True
281274
275+ def _create_headers (self , content_type = None ):
276+ request_headers = self .headers .copy ()
277+ if content_type is not None :
278+ request_headers ['Content-type' ] = content_type
279+ return request_headers
280+
281+ def _create_auth (self ):
282+ if self .http_username and self .http_password :
283+ return requests .auth .HTTPBasicAuth (self .http_username ,
284+ self .http_password )
285+ return None
286+
287+ def _get_session_opts (self , content_type ):
288+ return {
289+ 'headers' : self ._create_headers (content_type ),
290+ 'auth' : self ._create_auth (),
291+ 'timeout' : self .timeout ,
292+ 'verify' : self .ssl_verify
293+ }
294+
282295 def _raw_get (self , path_ , content_type = None , streamed = False , ** kwargs ):
283296 if path_ .startswith ('http://' ) or path_ .startswith ('https://' ):
284297 url = path_
285298 else :
286299 url = '%s%s' % (self ._url , path_ )
287300
288- headers = self ._create_headers (content_type )
301+ opts = self ._get_session_opts (content_type )
289302 try :
290- return self .session .get (url ,
291- params = kwargs ,
292- headers = headers ,
293- verify = self .ssl_verify ,
294- timeout = self .timeout ,
295- stream = streamed ,
296- auth = requests .auth .HTTPBasicAuth (
297- self .http_username ,
298- self .http_password ))
303+ return self .session .get (url , params = kwargs , stream = streamed ,
304+ ** opts )
299305 except Exception as e :
300306 raise GitlabConnectionError (
301307 "Can't connect to GitLab server (%s)" % e )
@@ -335,48 +341,27 @@ def _raw_list(self, path_, cls, extra_attrs={}, **kwargs):
335341
336342 def _raw_post (self , path_ , data = None , content_type = None , ** kwargs ):
337343 url = '%s%s' % (self ._url , path_ )
338- headers = self ._create_headers (content_type )
344+ opts = self ._get_session_opts (content_type )
339345 try :
340- return self .session .post (url , params = kwargs , data = data ,
341- headers = headers ,
342- verify = self .ssl_verify ,
343- timeout = self .timeout ,
344- auth = requests .auth .HTTPBasicAuth (
345- self .http_username ,
346- self .http_password ))
346+ return self .session .post (url , params = kwargs , data = data , ** opts )
347347 except Exception as e :
348348 raise GitlabConnectionError (
349349 "Can't connect to GitLab server (%s)" % e )
350350
351351 def _raw_put (self , path_ , data = None , content_type = None , ** kwargs ):
352352 url = '%s%s' % (self ._url , path_ )
353- headers = self ._create_headers (content_type )
354-
353+ opts = self ._get_session_opts (content_type )
355354 try :
356- return self .session .put (url , data = data , params = kwargs ,
357- headers = headers ,
358- verify = self .ssl_verify ,
359- timeout = self .timeout ,
360- auth = requests .auth .HTTPBasicAuth (
361- self .http_username ,
362- self .http_password ))
355+ return self .session .put (url , data = data , params = kwargs , ** opts )
363356 except Exception as e :
364357 raise GitlabConnectionError (
365358 "Can't connect to GitLab server (%s)" % e )
366359
367360 def _raw_delete (self , path_ , content_type = None , ** kwargs ):
368361 url = '%s%s' % (self ._url , path_ )
369- headers = self ._create_headers (content_type )
370-
362+ opts = self ._get_session_opts (content_type )
371363 try :
372- return self .session .delete (url ,
373- params = kwargs ,
374- headers = headers ,
375- verify = self .ssl_verify ,
376- timeout = self .timeout ,
377- auth = requests .auth .HTTPBasicAuth (
378- self .http_username ,
379- self .http_password ))
364+ return self .session .delete (url , params = kwargs , ** opts )
380365 except Exception as e :
381366 raise GitlabConnectionError (
382367 "Can't connect to GitLab server (%s)" % e )
0 commit comments