Revision 3dabe5d2 kamaki/clients/__init__.py
b/kamaki/clients/__init__.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
import json |
35 | 35 |
import logging |
36 |
from kamaki.clients.connection import HTTPConnectionError |
|
37 | 36 |
from kamaki.clients.connection.kamakicon import KamakiHTTPConnection |
38 | 37 |
|
39 | 38 |
sendlog = logging.getLogger('clients.send') |
40 | 39 |
recvlog = logging.getLogger('clients.recv') |
41 | 40 |
|
41 |
|
|
42 | 42 |
class ClientError(Exception): |
43 | 43 |
def __init__(self, message, status=0, details=''): |
44 | 44 |
super(ClientError, self).__init__(message, status, details) |
... | ... | |
46 | 46 |
self.status = status |
47 | 47 |
self.details = details |
48 | 48 |
|
49 |
|
|
49 | 50 |
class Client(object): |
50 | 51 |
|
51 | 52 |
def __init__(self, base_url, token, http_client=KamakiHTTPConnection()): |
... | ... | |
63 | 64 |
details = r.text |
64 | 65 |
except: |
65 | 66 |
details = '' |
66 |
raise ClientError(message=message, status=r.status_code, details=details) |
|
67 |
raise ClientError(message=message, |
|
68 |
status=r.status_code, |
|
69 |
details=details) |
|
67 | 70 |
|
68 | 71 |
def set_header(self, name, value, iff=True): |
69 |
"""Set a header 'name':'value' provided value is not None and iff is True"""
|
|
72 |
"""Set a header 'name':'value'""" |
|
70 | 73 |
if value is not None and iff: |
71 | 74 |
self.http_client.set_header(name, value) |
72 | 75 |
|
... | ... | |
84 | 87 |
async_params={}, |
85 | 88 |
**kwargs): |
86 | 89 |
"""In threaded/asynchronous requests, headers and params are not safe |
87 |
Therefore, the standard self.set_header/param system can be used only for
|
|
88 |
headers and params that are common for all requests. All other params and
|
|
89 |
headers should passes as |
|
90 |
Therefore, the standard self.set_header/param system can be used only |
|
91 |
for headers and params that are common for all requests. All other
|
|
92 |
params and headers should passes as
|
|
90 | 93 |
@param async_headers |
91 | 94 |
@async_params |
92 |
E.g. in most queries the 'X-Auth-Token' header might be the same for all, but the
|
|
93 |
'Range' header might be different from request to request. |
|
95 |
E.g. in most queries the 'X-Auth-Token' header might be the same for |
|
96 |
all, but the 'Range' header might be different from request to request.
|
|
94 | 97 |
""" |
95 | 98 |
|
96 | 99 |
try: |
... | ... | |
106 | 109 |
self.set_default_header('Content-Length', unicode(len(data))) |
107 | 110 |
|
108 | 111 |
self.http_client.url = self.base_url + path |
109 |
r = self.http_client.perform_request(method, data, async_headers, async_params) |
|
112 |
r = self.http_client.perform_request(method, |
|
113 |
data, |
|
114 |
async_headers, |
|
115 |
async_params) |
|
110 | 116 |
|
111 | 117 |
req = self.http_client |
112 | 118 |
sendlog.info('%s %s', method, req.url) |
... | ... | |
135 | 141 |
self.http_client.reset_headers() |
136 | 142 |
self.http_client.reset_params() |
137 | 143 |
errmsg = getattr(err, 'message', unicode(err)) |
138 |
errdetails ='%s %s'%(type(err), getattr(err, 'details', ''))
|
|
144 |
errdetails = '%s %s' % (type(err), getattr(err, 'details', ''))
|
|
139 | 145 |
errstatus = getattr(err, 'status', 0) |
140 |
raise ClientError(message=errmsg,status=errstatus,details=errdetails) |
|
146 |
raise ClientError(message=errmsg, |
|
147 |
status=errstatus, |
|
148 |
details=errdetails) |
|
141 | 149 |
|
142 | 150 |
self.http_client.reset_headers() |
143 | 151 |
self.http_client.reset_params() |
Also available in: Unified diff