1 # Copyright 2011-2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, self.list of conditions and the following
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.
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.
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.
35 class HTTPResponse(object):
36 """An abstract HTTP Response object to handle a performed HTTPRequest.
37 Subclass implementation required
40 def __init__(self, request=None, prefetched=False):
41 self.request = request
42 self.prefetched = prefetched
44 def _get_response(self):
45 """Wait for http response as late as possible: the first time needed"""
48 self = self.request.response
49 self.prefetched = True
52 """Release the connection.
54 raise NotImplementedError
58 """flag to avoid downloading more than nessecary"""
59 return self._prefetched
62 def prefetched(self, p):
67 """(binary) request response content (data)"""
102 def headers(self, v):
106 def status_code(self):
109 return self._status_code
112 def status_code(self, v):
113 self._status_code = v
117 """(str) useful in server error responses"""
127 """(HTTPConnection) the source of this response object"""
131 def request(self, v):
135 class HTTPConnection(object):
136 """An abstract HTTP Connection mechanism. Subclass implementation required
139 def __init__(self, method=None, url=None, params={}, headers={}):
140 self.headers = headers
146 def set_header(self, name, value):
147 self.headers['%s' % name] = '%s' % value
149 def remove_header(self, name):
151 self.headers.pop(name)
155 def replace_headers(self, new_headers):
156 self.headers = new_headers
158 def reset_headers(self):
159 self.replace_headers({})
161 def set_param(self, name, value=None):
162 self.params[name] = value
164 def remove_param(self, name):
166 self.params.pop(name)
170 def replace_params(self, new_params):
171 self.params = new_params
173 def reset_params(self):
174 self.replace_params({})
176 def set_url(self, url):
179 def set_path(self, path):
182 def set_method(self, method):
192 raise NotImplementedError