Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / __init__.py @ d5832d3b

History | View | Annotate | Download (5 kB)

1 14ec1f17 Stavros Sachtouris
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 14ec1f17 Stavros Sachtouris
#
3 14ec1f17 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 14ec1f17 Stavros Sachtouris
# without modification, are permitted provided that the following
5 14ec1f17 Stavros Sachtouris
# conditions are met:
6 14ec1f17 Stavros Sachtouris
#
7 14ec1f17 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 14ec1f17 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 14ec1f17 Stavros Sachtouris
#      disclaimer.
10 14ec1f17 Stavros Sachtouris
#
11 14ec1f17 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 14ec1f17 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 14ec1f17 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 14ec1f17 Stavros Sachtouris
#      provided with the distribution.
15 14ec1f17 Stavros Sachtouris
#
16 14ec1f17 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 14ec1f17 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 14ec1f17 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 14ec1f17 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 14ec1f17 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 14ec1f17 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 14ec1f17 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 14ec1f17 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 14ec1f17 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 14ec1f17 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 14ec1f17 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 14ec1f17 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 14ec1f17 Stavros Sachtouris
#
29 14ec1f17 Stavros Sachtouris
# The views and conclusions contained in the software and
30 14ec1f17 Stavros Sachtouris
# documentation are those of the authors and should not be
31 14ec1f17 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 14ec1f17 Stavros Sachtouris
# or implied, of GRNET S.A.
33 14ec1f17 Stavros Sachtouris
34 14ec1f17 Stavros Sachtouris
import json
35 14ec1f17 Stavros Sachtouris
import logging
36 14ec1f17 Stavros Sachtouris
37 14ec1f17 Stavros Sachtouris
import requests
38 14ec1f17 Stavros Sachtouris
39 14ec1f17 Stavros Sachtouris
from requests.auth import AuthBase
40 14ec1f17 Stavros Sachtouris
41 14ec1f17 Stavros Sachtouris
42 14ec1f17 Stavros Sachtouris
sendlog = logging.getLogger('clients.send')
43 14ec1f17 Stavros Sachtouris
recvlog = logging.getLogger('clients.recv')
44 14ec1f17 Stavros Sachtouris
45 14ec1f17 Stavros Sachtouris
46 14ec1f17 Stavros Sachtouris
# Add a convenience status property to the responses
47 14ec1f17 Stavros Sachtouris
def _status(self):
48 14ec1f17 Stavros Sachtouris
    return requests.status_codes._codes[self.status_code][0].upper()
49 14ec1f17 Stavros Sachtouris
requests.Response.status = property(_status)
50 14ec1f17 Stavros Sachtouris
51 14ec1f17 Stavros Sachtouris
52 14ec1f17 Stavros Sachtouris
class ClientError(Exception):
53 14ec1f17 Stavros Sachtouris
    def __init__(self, message, status=0, details=''):
54 14ec1f17 Stavros Sachtouris
        super(ClientError, self).__init__(message, status, details)
55 14ec1f17 Stavros Sachtouris
        self.message = message
56 14ec1f17 Stavros Sachtouris
        self.status = status
57 14ec1f17 Stavros Sachtouris
        self.details = details
58 14ec1f17 Stavros Sachtouris
59 14ec1f17 Stavros Sachtouris
60 14ec1f17 Stavros Sachtouris
class Client(object):
61 14ec1f17 Stavros Sachtouris
    def __init__(self, base_url, token):
62 14ec1f17 Stavros Sachtouris
        self.base_url = base_url
63 14ec1f17 Stavros Sachtouris
        self.token = token
64 14ec1f17 Stavros Sachtouris
65 14ec1f17 Stavros Sachtouris
    def raise_for_status(self, r):
66 14ec1f17 Stavros Sachtouris
        message = "%d %s" % (r.status_code, r.status)
67 14ec1f17 Stavros Sachtouris
        details = r.text
68 14ec1f17 Stavros Sachtouris
        raise ClientError(message, r.status_code, details)
69 14ec1f17 Stavros Sachtouris
70 14ec1f17 Stavros Sachtouris
    def request(self, method, path, **kwargs):
71 14ec1f17 Stavros Sachtouris
        raw = kwargs.pop('raw', False)
72 14ec1f17 Stavros Sachtouris
        success = kwargs.pop('success', 200)
73 14ec1f17 Stavros Sachtouris
        directory = kwargs.pop('directory', False)
74 d29f25fe Stavros Sachtouris
        meta = kwargs.pop('meta', False)
75 14ec1f17 Stavros Sachtouris
76 14ec1f17 Stavros Sachtouris
        data = kwargs.pop('data', None)
77 14ec1f17 Stavros Sachtouris
        headers = kwargs.pop('headers', {})
78 14ec1f17 Stavros Sachtouris
        headers.setdefault('X-Auth-Token', self.token)
79 14ec1f17 Stavros Sachtouris
80 14ec1f17 Stavros Sachtouris
        if directory:
81 14ec1f17 Stavros Sachtouris
            headers.setdefault('Content-Type', 'application/directory')
82 14ec1f17 Stavros Sachtouris
            headers.setdefault('Content-length', '0')
83 14ec1f17 Stavros Sachtouris
        else:
84 14ec1f17 Stavros Sachtouris
            if 'json' in kwargs:
85 14ec1f17 Stavros Sachtouris
                data = json.dumps(kwargs.pop('json'))
86 14ec1f17 Stavros Sachtouris
                headers.setdefault('Content-Type', 'application/json')
87 14ec1f17 Stavros Sachtouris
            if data:
88 67158731 Stavros Sachtouris
                headers.setdefault('Content-Length', unicode(len(data)))
89 14ec1f17 Stavros Sachtouris
90 d29f25fe Stavros Sachtouris
        if meta:
91 d29f25fe Stavros Sachtouris
            for key in meta.keys():
92 d29f25fe Stavros Sachtouris
                headers[key] = meta[key]
93 d29f25fe Stavros Sachtouris
            
94 14ec1f17 Stavros Sachtouris
        url = self.base_url + path
95 14ec1f17 Stavros Sachtouris
        kwargs.setdefault('verify', False)  # Disable certificate verification
96 d5832d3b Stavros Sachtouris
        print('HAVE WE BEEN OVER THIS? '+unicode(headers))
97 14ec1f17 Stavros Sachtouris
        r = requests.request(method, url, headers=headers, data=data, **kwargs)
98 14ec1f17 Stavros Sachtouris
99 d5832d3b Stavros Sachtouris
        print('HAVE WE BEEN OVER THIS?')
100 14ec1f17 Stavros Sachtouris
        req = r.request
101 14ec1f17 Stavros Sachtouris
        sendlog.info('%s %s', req.method, req.url)
102 14ec1f17 Stavros Sachtouris
        for key, val in req.headers.items():
103 14ec1f17 Stavros Sachtouris
            sendlog.info('%s: %s', key, val)
104 14ec1f17 Stavros Sachtouris
        sendlog.info('')
105 14ec1f17 Stavros Sachtouris
        if req.data:
106 14ec1f17 Stavros Sachtouris
            sendlog.info('%s', req.data)
107 14ec1f17 Stavros Sachtouris
108 14ec1f17 Stavros Sachtouris
        recvlog.info('%d %s', r.status_code, r.status)
109 14ec1f17 Stavros Sachtouris
        for key, val in r.headers.items():
110 14ec1f17 Stavros Sachtouris
            recvlog.info('%s: %s', key, val)
111 14ec1f17 Stavros Sachtouris
        recvlog.info('')
112 14ec1f17 Stavros Sachtouris
        if not raw and r.content:
113 14ec1f17 Stavros Sachtouris
            recvlog.debug(r.content)
114 14ec1f17 Stavros Sachtouris
115 14ec1f17 Stavros Sachtouris
        if success is not None:
116 14ec1f17 Stavros Sachtouris
            # Success can either be an in or a collection
117 14ec1f17 Stavros Sachtouris
            success = (success,) if isinstance(success, int) else success
118 14ec1f17 Stavros Sachtouris
            if r.status_code not in success:
119 14ec1f17 Stavros Sachtouris
                self.raise_for_status(r)
120 14ec1f17 Stavros Sachtouris
121 14ec1f17 Stavros Sachtouris
        return r
122 14ec1f17 Stavros Sachtouris
123 14ec1f17 Stavros Sachtouris
    def delete(self, path, **kwargs):
124 14ec1f17 Stavros Sachtouris
        return self.request('delete', path, **kwargs)
125 14ec1f17 Stavros Sachtouris
126 14ec1f17 Stavros Sachtouris
    def get(self, path, **kwargs):
127 14ec1f17 Stavros Sachtouris
        return self.request('get', path, **kwargs)
128 14ec1f17 Stavros Sachtouris
129 14ec1f17 Stavros Sachtouris
    def head(self, path, **kwargs):
130 14ec1f17 Stavros Sachtouris
        return self.request('head', path, **kwargs)
131 14ec1f17 Stavros Sachtouris
132 14ec1f17 Stavros Sachtouris
    def post(self, path, **kwargs):
133 14ec1f17 Stavros Sachtouris
        return self.request('post', path, **kwargs)
134 14ec1f17 Stavros Sachtouris
135 14ec1f17 Stavros Sachtouris
    def put(self, path, **kwargs):
136 14ec1f17 Stavros Sachtouris
        return self.request('put', path, **kwargs)
137 14ec1f17 Stavros Sachtouris
138 14ec1f17 Stavros Sachtouris
139 14ec1f17 Stavros Sachtouris
from .compute import ComputeClient as compute
140 14ec1f17 Stavros Sachtouris
from .image import ImageClient as image
141 14ec1f17 Stavros Sachtouris
from .storage import StorageClient as storage
142 14ec1f17 Stavros Sachtouris
from .cyclades import CycladesClient as cyclades
143 14ec1f17 Stavros Sachtouris
from .pithos import PithosClient as pithos
144 14ec1f17 Stavros Sachtouris
from .astakos import AstakosClient as astakos