Revision bcb51856
b/kamaki/clients/__init__.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
import json
|
|
34 |
from json import dumps as json_dumps
|
|
35 | 35 |
import logging |
36 | 36 |
from kamaki.clients.connection.kamakicon import KamakiHTTPConnection |
37 | 37 |
|
... | ... | |
40 | 40 |
|
41 | 41 |
|
42 | 42 |
class ClientError(Exception): |
43 |
"""Error in clients |
|
44 |
""" |
|
45 | 43 |
def __init__(self, message, status=0, details=''): |
46 | 44 |
super(ClientError, self).__init__(message) |
47 | 45 |
self.status = status |
... | ... | |
49 | 47 |
|
50 | 48 |
|
51 | 49 |
class Client(object): |
52 |
|
|
53 | 50 |
def __init__(self, base_url, token, http_client=KamakiHTTPConnection()): |
54 | 51 |
self.base_url = base_url |
55 | 52 |
self.token = token |
... | ... | |
101 | 98 |
self.set_default_header('X-Auth-Token', self.token) |
102 | 99 |
|
103 | 100 |
if 'json' in kwargs: |
104 |
data = json.dumps(kwargs.pop('json'))
|
|
101 |
data = json_dumps(kwargs.pop('json'))
|
|
105 | 102 |
self.set_default_header('Content-Type', 'application/json') |
106 | 103 |
if data: |
107 | 104 |
self.set_default_header('Content-Length', unicode(len(data))) |
... | ... | |
127 | 124 |
recvlog.info('%d %s', r.status_code, r.status) |
128 | 125 |
for key, val in r.headers.items(): |
129 | 126 |
recvlog.info('%s: %s', key, val) |
130 |
#if r.content:
|
|
131 |
# recvlog.debug(r.content)
|
|
127 |
if r.content: |
|
128 |
recvlog.debug(r.content) |
|
132 | 129 |
|
133 | 130 |
if success is not None: |
134 | 131 |
# Success can either be an in or a collection |
... | ... | |
139 | 136 |
except ClientError: |
140 | 137 |
raise |
141 | 138 |
except Exception as err: |
139 |
from traceback import print_stack |
|
140 |
recvlog.debug(print_stack) |
|
142 | 141 |
self.http_client.reset_headers() |
143 | 142 |
self.http_client.reset_params() |
144 | 143 |
raise ClientError('%s' % err, status=getattr(err, 'status', 0)) |
b/kamaki/clients/connection/errors.py | ||
---|---|---|
1 |
# Copyright 2012 GRNET S.A. All rights reserved. |
|
2 |
# |
|
3 |
# Redistribution and use in source and binary forms, with or |
|
4 |
# without modification, are permitted provided that the following |
|
5 |
# conditions are met: |
|
6 |
# |
|
7 |
# 1. Redistributions of source code must retain the above |
|
8 |
# copyright notice, self.list of conditions and the following |
|
9 |
# disclaimer. |
|
10 |
# |
|
11 |
# 2. Redistributions in binary form must reproduce the above |
|
12 |
# copyright notice, self.list of conditions and the following |
|
13 |
# disclaimer in the documentation and/or other materials |
|
14 |
# provided with the distribution. |
|
15 |
# |
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
27 |
# POSSIBILITY OF SUCH DAMAGE. |
|
28 |
# |
|
29 |
# The views and conclusions contained in the software and |
|
30 |
# documentation are those of the authors and should not be |
|
31 |
# interpreted as representing official policies, either expressed |
|
32 |
# or implied, of GRNET S.A. |
|
33 |
|
|
34 |
|
|
35 |
class HTTPConnectionError(Exception): |
|
36 |
""" |
|
37 |
700: Generic connection error |
|
38 |
701: Cannot connect to server |
|
39 |
702: Response format error |
|
40 |
""" |
|
41 |
def __init__(self, message, status=700): |
|
42 |
super(HTTPConnectionError, self).__init__(message) |
|
43 |
self.status = status |
|
44 |
|
|
45 |
|
|
46 |
class HTTPResponseFormatError(HTTPConnectionError): |
|
47 |
|
|
48 |
def __init__(self, message, details=''): |
|
49 |
super(HTTPResponseFormatError, self).__init__(message, status=702) |
Also available in: Unified diff