Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / __init__.py @ 33dc6317

History | View | Annotate | Download (5.5 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, 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 .connection import HTTPConnectionError
37
#from .connection.request import HTTPRequest
38
from .connection.kamakicon import KamakiHTTPConnection
39

    
40
sendlog = logging.getLogger('clients.send')
41
recvlog = logging.getLogger('clients.recv')
42

    
43

    
44
class ClientError(Exception):
45
    def __init__(self, message, status=0, details=''):
46
        super(ClientError, self).__init__(message, status, details)
47
        self.message = message
48
        self.status = status
49
        self.details = details
50

    
51
class Client(object):
52

    
53
    def __init__(self, base_url, token, http_client=KamakiHTTPConnection()):
54
    #def __init__(self, base_url, token, http_client=HTTPRequest()):
55
        self.base_url = base_url
56
        self.token = token
57
        self.headers = {}
58
        self.DATE_FORMATS = ["%a %b %d %H:%M:%S %Y",
59
            "%A, %d-%b-%y %H:%M:%S GMT",
60
            "%a, %d %b %Y %H:%M:%S GMT"]
61
        self.http_client = http_client
62

    
63
    def _raise_for_status(self, r):
64
        message = "%d %s" % (r.status_code, r.status)
65
        try:
66
            details = r.text
67
        except:
68
            details = ''
69
        raise ClientError(message, r.status_code, details)
70

    
71
    def set_header(self, name, value, iff=True):
72
        """Set a header 'name':'value' provided value is not None and iff is True"""
73
        if value is not None and iff:
74
            self.http_client.set_header(name, value)
75

    
76
    def set_param(self, name, value=None, iff=True):
77
        if iff:
78
            self.http_client.set_param(name, value)
79

    
80
    def set_default_header(self, name, value):
81
        self.http_client.headers.setdefault(name, value)
82

    
83
    def request(self, method, path, **kwargs):
84
        try:
85
            success = kwargs.pop('success', 200)
86

    
87
            binary = kwargs.pop('binary', False)
88
            data = kwargs.pop('data', None)
89
            self.set_default_header('X-Auth-Token', self.token)
90

    
91
            if 'json' in kwargs:
92
                data = json.dumps(kwargs.pop('json'))
93
                self.set_default_header('Content-Type', 'application/json')
94
            if data:
95
                self.set_default_header('Content-Length', unicode(len(data)))
96

    
97
            self.http_client.url = self.base_url + path
98
            r = self.http_client.perform_request(method=method, data=data)
99

    
100
            req = self.http_client
101
            sendlog.info('%s %s', method, req.url)
102
            for key, val in req.headers.items():
103
                sendlog.info('%s: %s', key, val)
104
            sendlog.info('')
105
            if data:
106
                sendlog.info('%s', data)
107

    
108
            recvlog.info('%d %s', r.status_code, r.status)
109
            for key, val in r.headers.items():
110
                recvlog.info('%s: %s', key, val)
111
            recvlog.info('')
112
            if r.content:
113
                recvlog.debug(r.content)
114

    
115
            if success is not None:
116
                # Success can either be an in or a collection
117
                success = (success,) if isinstance(success, int) else success
118
                if r.status_code not in success:
119
                    self._raise_for_status(r)
120
        except Exception as err:
121
            self.http_client.reset_headers()
122
            self.http_client.reset_params()
123
            if isinstance(err, HTTPConnectionError):
124
                raise ClientError(message=err.message, status=err.status, details=err.details)
125
            raise
126

    
127
        self.http_client.reset_headers()
128
        self.http_client.reset_params()
129
        return r
130

    
131
    def delete(self, path, **kwargs):
132
        return self.request('delete', path, **kwargs)
133

    
134
    def get(self, path, **kwargs):
135
        return self.request('get', path, **kwargs)
136

    
137
    def head(self, path, **kwargs):
138
        return self.request('head', path, **kwargs)
139

    
140
    def post(self, path, **kwargs):
141
        return self.request('post', path, **kwargs)
142

    
143
    def put(self, path, **kwargs):
144
        return self.request('put', path, **kwargs)
145

    
146
    def copy(self, path, **kwargs):
147
        return self.request('copy', path, **kwargs)
148

    
149
    def move(self, path, **kwargs):
150
        return self.request('move', path, **kwargs)
151