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.
34 from .pool import ObjectPool
38 class HTTPResponsePool(ObjectPool):
40 def __init__(self, netloc, size=POOL_SIZE):
41 super(HTTPResponsePool, self).__init__(size=size)
44 def _pool_create(self):
49 def _pool_cleanup(self, resp):
53 class HTTPResponse(object):
55 def __init__(self, request=None, prefetched=False):
58 self = request.response
59 self.prefetched = prefetched
61 def _get_response(self):
62 """Wait for http response as late as possible: the first time needed"""
65 self = self.request.response
66 self.prefetched = True
69 """Release the connection.
70 Use this after finished using the response"""
71 raise NotImplementedError
75 return self._prefetched
77 def prefetched(self, p):
109 def headers(self, v):
113 def status_code(self):
115 return self._status_code
117 def status_code(self, v):
118 self._status_code = v
132 def request(self, v):
135 class HTTPConnectionError(Exception):
136 def __init__(self, message, status=0, details=''):
137 super(HTTPConnectionError, self).__init__(message)
138 self.message = message
140 self.details = details
142 class HTTPConnection(object):
144 def __init__(self, method=None, url=None, params={}, headers={}):
145 self.headers = headers
150 def raise_for_status(self, r):
151 message = "%d %s" % (r.status_code, r.status)
156 raise HTTPConnectionError(message, r.status_code, details)
158 def set_header(self, name, value):
159 self.headers[unicode(name)] = unicode(value)
161 def remove_header(self, name):
163 self.headers.pop(name)
167 def replace_headers(self, new_headers):
168 self.headers = new_headers
170 def reset_headers(self):
171 self.replace_headers({})
173 def set_param(self, name, value=None):
174 self.params[name] = value
176 def remove_param(self, name):
178 self.params.pop(name)
182 def replace_params(self, new_params):
183 self.params = new_params
185 def reset_params(self):
186 self.replace_params({})
188 def set_url(self, url):
191 def set_method(self, method):
194 def perform_request(self, method=None, url=None, params=None, headers=None, data=None):
196 @return an HTTPResponse (also in self.response of this object)
197 named args offer the ability to reset a request or a part of the request
198 e.g. r = HTTPConnection(url='http://....', method='GET')
200 r.perform_request(method='POST')
201 will perform a GET request and later a POST request on the same URL
203 r = HTTPConnection(url='http://....', params='format=json')
204 r.perform_request(method='GET')
205 r.perform_request(method='POST')
207 raise NotImplementedError
212 return self._response
214 def response(self, r):