root / kamaki / clients / __init__.py @ dba6ec94
History | View | Annotate | Download (4.7 kB)
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, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this 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 |
|
37 |
import requests |
38 |
|
39 |
from requests.auth import AuthBase |
40 |
|
41 |
|
42 |
sendlog = logging.getLogger('clients.send')
|
43 |
recvlog = logging.getLogger('clients.recv')
|
44 |
|
45 |
|
46 |
# Add a convenience json property to the responses
|
47 |
def _json(self): |
48 |
try:
|
49 |
return json.loads(self.content) if self.content else {} |
50 |
except ValueError: |
51 |
raise ClientError("Invalid JSON reply", self.status_code) |
52 |
requests.Response.json = property(_json)
|
53 |
|
54 |
# Add a convenience status property to the responses
|
55 |
def _status(self): |
56 |
return requests.status_codes._codes[self.status_code][0].upper() |
57 |
requests.Response.status = property(_status)
|
58 |
|
59 |
|
60 |
class XAuthTokenAuth(AuthBase): |
61 |
def __init__(self, token): |
62 |
self.token = token
|
63 |
|
64 |
def __call__(self, r): |
65 |
r.headers['X-Auth-Token'] = self.token |
66 |
return r
|
67 |
|
68 |
|
69 |
class ClientError(Exception): |
70 |
def __init__(self, message, status=0, details=''): |
71 |
self.message = message
|
72 |
self.status = status
|
73 |
self.details = details
|
74 |
|
75 |
|
76 |
class Client(object): |
77 |
def __init__(self, base_url, token): |
78 |
self.base_url = base_url
|
79 |
self.auth = XAuthTokenAuth(token)
|
80 |
|
81 |
def raise_for_status(self, r): |
82 |
message = "%d %s" % (r.status_code, r.status)
|
83 |
details = r.text |
84 |
raise ClientError(message, r.status_code, details)
|
85 |
|
86 |
def request(self, method, path, **kwargs): |
87 |
raw = kwargs.pop('raw', False) |
88 |
success = kwargs.pop('success', 200) |
89 |
if 'json' in kwargs: |
90 |
data = json.dumps(kwargs.pop('json'))
|
91 |
kwargs['data'] = data
|
92 |
headers = kwargs.setdefault('headers', {})
|
93 |
headers['content-type'] = 'application/json' |
94 |
|
95 |
url = self.base_url + path
|
96 |
kwargs.setdefault('auth', self.auth) |
97 |
r = requests.request(method, url, **kwargs) |
98 |
|
99 |
req = r.request |
100 |
sendlog.info('%s %s', req.method, req.url)
|
101 |
for key, val in req.headers.items(): |
102 |
sendlog.info('%s: %s', key, val)
|
103 |
sendlog.info('')
|
104 |
if req.data:
|
105 |
sendlog.info('%s', req.data)
|
106 |
|
107 |
recvlog.info('%d %s', r.status_code, r.status)
|
108 |
for key, val in r.headers.items(): |
109 |
recvlog.info('%s: %s', key, val)
|
110 |
recvlog.info('')
|
111 |
if not raw and r.text: |
112 |
recvlog.debug(r.text) |
113 |
|
114 |
if success is not None: |
115 |
# Success can either be an in or a collection
|
116 |
success = (success,) if isinstance(success, int) else success |
117 |
if r.status_code not in success: |
118 |
self.raise_for_status(r)
|
119 |
|
120 |
return r
|
121 |
|
122 |
def delete(self, path, **kwargs): |
123 |
return self.request('delete', path, **kwargs) |
124 |
|
125 |
def get(self, path, **kwargs): |
126 |
return self.request('get', path, **kwargs) |
127 |
|
128 |
def head(self, path, **kwargs): |
129 |
return self.request('head', path, **kwargs) |
130 |
|
131 |
def post(self, path, **kwargs): |
132 |
return self.request('post', path, **kwargs) |
133 |
|
134 |
def put(self, path, **kwargs): |
135 |
return self.request('put', path, **kwargs) |
136 |
|
137 |
|
138 |
from .compute import ComputeClient as compute |
139 |
from .image import ImageClient as image |
140 |
from .storage import StorageClient as storage |
141 |
from .cyclades import CycladesClient as cyclades |
142 |
from .pithos import PithosClient as pithos |
143 |
from .astakos import AstakosClient as astakos |