Remove relative imports from all pkgs, fix astakos
[kamaki] / kamaki / clients / __init__.py
1 # Copyright 2011-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 import json
35 import logging
36 from kamaki.clients.connection import HTTPConnectionError
37 #from .connection.request import HTTPRequest
38 from kamaki.clients.connection.kamakicon import KamakiHTTPConnection
39
40 sendlog = logging.getLogger('clients.send')
41 recvlog = logging.getLogger('clients.recv')
42
43 class ClientError(Exception):
44     def __init__(self, message, status=0, details=''):
45         super(ClientError, self).__init__(message, status, details)
46         self.message = message
47         self.status = status
48         self.details = details
49
50 class Client(object):
51
52     def __init__(self, base_url, token, http_client=KamakiHTTPConnection()):
53     #def __init__(self, base_url, token, http_client=HTTPRequest()):
54         self.base_url = base_url
55         self.token = token
56         self.headers = {}
57         self.DATE_FORMATS = ["%a %b %d %H:%M:%S %Y",
58             "%A, %d-%b-%y %H:%M:%S GMT",
59             "%a, %d %b %Y %H:%M:%S GMT"]
60         self.http_client = http_client
61
62     def _raise_for_status(self, r):
63         message = "%d %s" % (r.status_code, r.status)
64         try:
65             details = r.text
66         except:
67             details = ''
68         raise ClientError(message, r.status_code, details)
69
70     def set_header(self, name, value, iff=True):
71         """Set a header 'name':'value' provided value is not None and iff is True"""
72         if value is not None and iff:
73             self.http_client.set_header(name, value)
74
75     def set_param(self, name, value=None, iff=True):
76         if iff:
77             self.http_client.set_param(name, value)
78
79     def set_default_header(self, name, value):
80         self.http_client.headers.setdefault(name, value)
81
82     def request(self,
83         method,
84         path,
85         async_headers={},
86         async_params={},
87         **kwargs):
88         """In threaded/asynchronous requests, headers and params are not safe
89         Therefore, the standard self.set_header/param system can be used only for 
90         headers and params that are common for all requests. All other params and
91         headers should passes as
92         @param async_headers
93         @async_params
94         E.g. in most queries the 'X-Auth-Token' header might be the same for all, but the
95         'Range' header might be different from request to request.
96         """
97
98         try:
99             success = kwargs.pop('success', 200)
100
101             data = kwargs.pop('data', None)
102             self.set_default_header('X-Auth-Token', self.token)
103
104             if 'json' in kwargs:
105                 data = json.dumps(kwargs.pop('json'))
106                 self.set_default_header('Content-Type', 'application/json')
107             if data:
108                 self.set_default_header('Content-Length', unicode(len(data)))
109
110             self.http_client.url = self.base_url + path
111             r = self.http_client.perform_request(method, data, async_headers, async_params)
112
113             req = self.http_client
114             sendlog.info('%s %s', method, req.url)
115             headers = dict(req.headers)
116             headers.update(async_headers)
117
118             for key, val in headers.items():
119                 sendlog.info('\t%s: %s', key, val)
120             sendlog.info('')
121             if data:
122                 sendlog.info('%s', data)
123
124             recvlog.info('%d %s', r.status_code, r.status)
125             for key, val in r.headers.items():
126                 recvlog.info('%s: %s', key, val)
127             #if r.content:
128             #    recvlog.debug(r.content)
129
130             if success is not None:
131                 # Success can either be an in or a collection
132                 success = (success,) if isinstance(success, int) else success
133                 if r.status_code not in success:
134                     self._raise_for_status(r)
135         except Exception as err:
136             self.http_client.reset_headers()
137             self.http_client.reset_params()
138             if isinstance(err, HTTPConnectionError):
139                 raise ClientError(message=err.message, status=err.status, details=err.details)
140             raise
141
142         self.http_client.reset_headers()
143         self.http_client.reset_params()
144         return r
145
146     def delete(self, path, **kwargs):
147         return self.request('delete', path, **kwargs)
148
149     def get(self, path, **kwargs):
150         return self.request('get', path, **kwargs)
151
152     def head(self, path, **kwargs):
153         return self.request('head', path, **kwargs)
154
155     def post(self, path, **kwargs):
156         return self.request('post', path, **kwargs)
157
158     def put(self, path, **kwargs):
159         return self.request('put', path, **kwargs)
160
161     def copy(self, path, **kwargs):
162         return self.request('copy', path, **kwargs)
163
164     def move(self, path, **kwargs):
165         return self.request('move', path, **kwargs)
166