Commit 2aa8f6d1 authored by Giorgos Verigakis's avatar Giorgos Verigakis
Browse files

Add Content-Length header when sending data

parent b982462f
...@@ -57,15 +57,6 @@ def _status(self): ...@@ -57,15 +57,6 @@ def _status(self):
requests.Response.status = property(_status) requests.Response.status = property(_status)
class XAuthTokenAuth(AuthBase):
def __init__(self, token):
self.token = token
def __call__(self, r):
r.headers['X-Auth-Token'] = self.token
return r
class ClientError(Exception): class ClientError(Exception):
def __init__(self, message, status=0, details=''): def __init__(self, message, status=0, details=''):
self.message = message self.message = message
...@@ -76,8 +67,8 @@ class ClientError(Exception): ...@@ -76,8 +67,8 @@ class ClientError(Exception):
class Client(object): class Client(object):
def __init__(self, base_url, token): def __init__(self, base_url, token):
self.base_url = base_url self.base_url = base_url
self.auth = XAuthTokenAuth(token) self.token = token
def raise_for_status(self, r): def raise_for_status(self, r):
message = "%d %s" % (r.status_code, r.status) message = "%d %s" % (r.status_code, r.status)
details = r.text details = r.text
...@@ -86,17 +77,22 @@ class Client(object): ...@@ -86,17 +77,22 @@ class Client(object):
def request(self, method, path, **kwargs): def request(self, method, path, **kwargs):
raw = kwargs.pop('raw', False) raw = kwargs.pop('raw', False)
success = kwargs.pop('success', 200) success = kwargs.pop('success', 200)
data = kwargs.pop('data', None)
headers = kwargs.pop('headers', {})
headers.setdefault('X-Auth-Token', self.token)
if 'json' in kwargs: if 'json' in kwargs:
data = json.dumps(kwargs.pop('json')) data = json.dumps(kwargs.pop('json'))
kwargs['data'] = data headers.setdefault('Content-Type', 'application/json')
headers = kwargs.setdefault('headers', {})
headers['content-type'] = 'application/json' if data:
headers.setdefault('Content-Length', str(len(data)))
url = self.base_url + path url = self.base_url + path
kwargs.setdefault('auth', self.auth)
kwargs.setdefault('verify', False) # Disable certificate verification kwargs.setdefault('verify', False) # Disable certificate verification
r = requests.request(method, url, **kwargs) r = requests.request(method, url, headers=headers, data=data, **kwargs)
req = r.request req = r.request
sendlog.info('%s %s', req.method, req.url) sendlog.info('%s %s', req.method, req.url)
for key, val in req.headers.items(): for key, val in req.headers.items():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment