Revision 6ad245d5 kamaki/clients/__init__.py
b/kamaki/clients/__init__.py | ||
---|---|---|
34 | 34 |
import json |
35 | 35 |
import logging |
36 | 36 |
from .connection import HTTPConnectionError |
37 |
from .connection.request import HTTPRequest |
|
38 |
#from .connection.kamakicon import KamakiHTTPConnection |
|
37 | 39 |
|
38 | 40 |
sendlog = logging.getLogger('clients.send') |
39 | 41 |
recvlog = logging.getLogger('clients.recv') |
... | ... | |
48 | 50 |
|
49 | 51 |
class Client(object): |
50 | 52 |
|
51 |
def __init__(self, base_url, token, http_client=None):
|
|
53 |
def __init__(self, base_url, token, http_client=HTTPRequest()):
|
|
52 | 54 |
self.base_url = base_url |
53 | 55 |
self.token = token |
54 | 56 |
self.headers = {} |
... | ... | |
57 | 59 |
"%a, %d %b %Y %H:%M:%S GMT"] |
58 | 60 |
self.http_client = http_client |
59 | 61 |
|
60 |
def raise_for_status(self, r): |
|
62 |
def _raise_for_status(self, r):
|
|
61 | 63 |
message = "%d %s" % (r.status_code, r.status) |
62 | 64 |
try: |
63 | 65 |
details = r.text |
... | ... | |
84 | 86 |
|
85 | 87 |
data = kwargs.pop('data', None) |
86 | 88 |
self.set_default_header('X-Auth-Token', self.token) |
89 |
#self.set_default_header('Accept', '*/*') |
|
90 |
#self.set_default_header('Accept-Encoding', 'identity, deflate, compress, gzip') |
|
87 | 91 |
|
88 | 92 |
if 'json' in kwargs: |
89 | 93 |
data = json.dumps(kwargs.pop('json')) |
... | ... | |
93 | 97 |
|
94 | 98 |
#kwargs.setdefault('verify', False) # Disable certificate verification |
95 | 99 |
self.http_client.url = self.base_url + path |
96 |
self.http_client.perform_request(method=method, data=data) |
|
100 |
r = self.http_client.perform_request(method=method, data=data)
|
|
97 | 101 |
#r = requests.request(method, url, headers=self.headers, data=data, **kwargs) |
98 | 102 |
|
99 | 103 |
req = self.http_client |
... | ... | |
104 | 108 |
if data: |
105 | 109 |
sendlog.info('%s', data) |
106 | 110 |
|
107 |
r = self.http_client.response |
|
111 |
#r = self.http_client.response |
|
112 |
#print('What is the case with r? %s'%unicode(r.content)) |
|
108 | 113 |
recvlog.info('%d %s', r.status_code, r.status) |
109 | 114 |
for key, val in r.headers.items(): |
110 | 115 |
recvlog.info('%s: %s', key, val) |
... | ... | |
116 | 121 |
# Success can either be an in or a collection |
117 | 122 |
success = (success,) if isinstance(success, int) else success |
118 | 123 |
if r.status_code not in success: |
119 |
self.raise_for_status(r) |
|
124 |
self._raise_for_status(r)
|
|
120 | 125 |
except Exception as err: |
121 | 126 |
self.http_client.reset_headers() |
122 | 127 |
self.http_client.reset_params() |
Also available in: Unified diff