Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / connection / __init__.py @ 9a7efb0d

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
class HTTPResponse(object):
35

    
36
    def __init__(self, request=None, prefetched=False):
37
        self.request=request
38
        self.prefetched = prefetched
39

    
40
    def _get_response(self):
41
        """Wait for http response as late as possible: the first time needed"""
42
        if self.prefetched:
43
            return
44
        self = self.request.response
45
        self.prefetched = True
46

    
47
    def release(self):
48
        """Release the connection.
49
        Use this after finished using the response"""
50
        raise NotImplementedError
51

    
52
    @property 
53
    def prefetched(self):
54
        return self._prefetched
55
    @prefetched.setter
56
    def prefetched(self, p):
57
        self._prefetched = p
58

    
59
    @property 
60
    def content(self):
61
        self._get_response()
62
        return self._content
63
    @content.setter 
64
    def content(self, v):
65
        self._content = v
66

    
67
    @property 
68
    def text(self):
69
        self._get_response()
70
        return self._text
71
    @text.setter 
72
    def text(self, v):
73
        self._text = v
74

    
75
    @property 
76
    def json(self):
77
        self._get_response()
78
        return self._json
79
    @json.setter 
80
    def json(self, v):
81
        self._json = v
82

    
83
    @property 
84
    def headers(self):
85
        self._get_response()
86
        return self._headers
87
    @headers.setter 
88
    def headers(self, v):
89
        self._headers = v
90

    
91
    @property 
92
    def status_code(self):
93
        self._get_response()
94
        return self._status_code
95
    @status_code.setter 
96
    def status_code(self, v):
97
        self._status_code = v
98

    
99
    @property 
100
    def status(self):
101
        self._get_response()
102
        return self._status
103
    @status.setter 
104
    def status(self, v):
105
        self._status = v
106

    
107
    @property 
108
    def request(self):
109
        return self._request
110
    @request.setter 
111
    def request(self, v):
112
        self._request = v
113

    
114
class HTTPConnectionError(Exception):
115
    def __init__(self, message, status=0, details=''):
116
            super(HTTPConnectionError, self).__init__(message)
117
        self.message = message
118
        self.status = status
119
        self.details = details
120

    
121
class HTTPConnection(object):
122

    
123
    def __init__(self, method=None, url=None, params={}, headers={}):
124
            self.headers = headers
125
            self.params = params
126
            self.url = url
127
            self.method = method
128

    
129
    def raise_for_status(self, r):
130
        message = "%d %s" % (r.status_code, r.status)
131
        try:
132
            details = r.text
133
        except:
134
            details = ''
135
        raise HTTPConnectionError(message, r.status_code, details)
136

    
137
    def set_header(self, name, value):
138
            self.headers[unicode(name)] = unicode(value)
139

    
140
    def remove_header(self, name):
141
            try:
142
                    self.headers.pop(name)
143
            except KeyError:
144
                    pass
145

    
146
    def replace_headers(self, new_headers):
147
            self.headers = new_headers
148

    
149
    def reset_headers(self):
150
            self.replace_headers({})
151

    
152
    def set_param(self, name, value=None):
153
            self.params[name] = value
154

    
155
    def remove_param(self, name):
156
            try:
157
                    self.params.pop(name)
158
            except KeyError:
159
                    pass
160

    
161
    def replace_params(self, new_params):
162
            self.params = new_params
163

    
164
    def reset_params(self):
165
            self.replace_params({})
166

    
167
    def set_url(self, url):
168
            self.url = url
169

    
170
    def set_method(self, method):
171
            self.method = method
172

    
173
        def perform_request(self, method=None, url=None, async_headers={}, async_params={}, data=None):
174
                """
175
                @return an HTTPResponse (also in self.response of this object)
176
                named args offer the ability to reset a request or a part of the request
177
                e.g. r = HTTPConnection(url='http://....', method='GET')
178
                         r.perform_request()
179
                         r.perform_request(method='POST')
180
                will perform a GET request and later a POST request on the same URL
181
                another example:
182
                         r = HTTPConnection(url='http://....', params='format=json')
183
                         r.perform_request(method='GET')
184
                         r.perform_request(method='POST')
185
                """
186
                raise NotImplementedError
187

    
188
    """
189
    @property 
190
    def response(self):
191
        return self._response
192
    @response.setter
193
    def response(self, r):
194
        self._response = r
195
    """