Skip to content

Commit

Permalink
Rework requests arguments
Browse files Browse the repository at this point in the history
* Factorize the code
* Don't send empty auth information to requests (Fixes #188)
  • Loading branch information
Gauvain Pocentek committed Dec 1, 2016
1 parent 764d3ca commit 6e5734b
Showing 1 changed file with 29 additions and 44 deletions.
73 changes: 29 additions & 44 deletions gitlab/__init__.py
Expand Up @@ -236,13 +236,6 @@ def _construct_url(self, id_, obj, parameters, action=None):
else:
return url

def _create_headers(self, content_type=None, headers={}):
request_headers = self.headers.copy()
request_headers.update(headers)
if content_type is not None:
request_headers['Content-type'] = content_type
return request_headers

def set_token(self, token):
"""Sets the private token for authentication.
Expand Down Expand Up @@ -279,23 +272,36 @@ def enable_debug(self):
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

def _create_headers(self, content_type=None):
request_headers = self.headers.copy()
if content_type is not None:
request_headers['Content-type'] = content_type
return request_headers

def _create_auth(self):
if self.http_username and self.http_password:
return requests.auth.HTTPBasicAuth(self.http_username,
self.http_password)
return None

def _get_session_opts(self, content_type):
return {
'headers': self._create_headers(content_type),
'auth': self._create_auth(),
'timeout': self.timeout,
'verify': self.ssl_verify
}

def _raw_get(self, path_, content_type=None, streamed=False, **kwargs):
if path_.startswith('http://') or path_.startswith('https://'):
url = path_
else:
url = '%s%s' % (self._url, path_)

headers = self._create_headers(content_type)
opts = self._get_session_opts(content_type)
try:
return self.session.get(url,
params=kwargs,
headers=headers,
verify=self.ssl_verify,
timeout=self.timeout,
stream=streamed,
auth=requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password))
return self.session.get(url, params=kwargs, stream=streamed,
**opts)
except Exception as e:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % e)
Expand Down Expand Up @@ -335,48 +341,27 @@ def _raw_list(self, path_, cls, extra_attrs={}, **kwargs):

def _raw_post(self, path_, data=None, content_type=None, **kwargs):
url = '%s%s' % (self._url, path_)
headers = self._create_headers(content_type)
opts = self._get_session_opts(content_type)
try:
return self.session.post(url, params=kwargs, data=data,
headers=headers,
verify=self.ssl_verify,
timeout=self.timeout,
auth=requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password))
return self.session.post(url, params=kwargs, data=data, **opts)
except Exception as e:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % e)

def _raw_put(self, path_, data=None, content_type=None, **kwargs):
url = '%s%s' % (self._url, path_)
headers = self._create_headers(content_type)

opts = self._get_session_opts(content_type)
try:
return self.session.put(url, data=data, params=kwargs,
headers=headers,
verify=self.ssl_verify,
timeout=self.timeout,
auth=requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password))
return self.session.put(url, data=data, params=kwargs, **opts)
except Exception as e:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % e)

def _raw_delete(self, path_, content_type=None, **kwargs):
url = '%s%s' % (self._url, path_)
headers = self._create_headers(content_type)

opts = self._get_session_opts(content_type)
try:
return self.session.delete(url,
params=kwargs,
headers=headers,
verify=self.ssl_verify,
timeout=self.timeout,
auth=requests.auth.HTTPBasicAuth(
self.http_username,
self.http_password))
return self.session.delete(url, params=kwargs, **opts)
except Exception as e:
raise GitlabConnectionError(
"Can't connect to GitLab server (%s)" % e)
Expand Down

0 comments on commit 6e5734b

Please sign in to comment.