]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Centralize some shared logic bewteen HTTPClient implementations.
authorBen Darnell <ben@bendarnell.com>
Sun, 17 Feb 2013 23:28:44 +0000 (18:28 -0500)
committerBen Darnell <ben@bendarnell.com>
Sun, 17 Feb 2013 23:28:44 +0000 (18:28 -0500)
tornado/curl_httpclient.py
tornado/httpclient.py
tornado/simple_httpclient.py

index dcc601179b0a780bf8fb1d0c5f761cd46e6dd721..73ecfb0e12a38eb95e80e9e5ac275719305afc9b 100644 (file)
@@ -39,11 +39,8 @@ except ImportError:
 
 
 class CurlAsyncHTTPClient(AsyncHTTPClient):
-    def initialize(self, io_loop=None, max_clients=10, defaults=None):
-        self.io_loop = io_loop
-        self.defaults = dict(HTTPRequest._DEFAULTS)
-        if defaults is not None:
-            self.defaults.update(defaults)
+    def initialize(self, io_loop, max_clients=10, defaults=None):
+        super(CurlAsyncHTTPClient, self).initialize(io_loop, defaults=defaults)
         self._multi = pycurl.CurlMulti()
         self._multi.setopt(pycurl.M_TIMERFUNCTION, self._set_timeout)
         self._multi.setopt(pycurl.M_SOCKETFUNCTION, self._handle_socket)
@@ -90,10 +87,7 @@ class CurlAsyncHTTPClient(AsyncHTTPClient):
         self._closed = True
         super(CurlAsyncHTTPClient, self).close()
 
-    def fetch(self, request, callback, **kwargs):
-        if not isinstance(request, HTTPRequest):
-            request = HTTPRequest(url=request, **kwargs)
-        request = _RequestProxy(request, self.defaults)
+    def fetch_impl(self, request, callback):
         self._requests.append((request, stack_context.wrap(callback)))
         self._process_queue()
         self._set_timeout(0)
index 73a88af16271487f4af50400c05557196362664a..18e53bd226ac78d716532d4b946a59cf80cc48c2 100644 (file)
@@ -144,6 +144,12 @@ class AsyncHTTPClient(Configurable):
             cls._async_clients()[io_loop] = instance
         return instance
 
+    def initialize(self, io_loop, defaults=None):
+        self.io_loop = io_loop
+        self.defaults = dict(HTTPRequest._DEFAULTS)
+        if defaults is not None:
+            self.defaults.update(defaults)
+
     def close(self):
         """Destroys this http client, freeing any file descriptors used.
         Not needed in normal use, but may be helpful in unittests that
@@ -165,6 +171,16 @@ class AsyncHTTPClient(Configurable):
         encountered during the request. You can call response.rethrow() to
         throw the exception (if any) in the callback.
         """
+        if not isinstance(request, HTTPRequest):
+            request = HTTPRequest(url=request, **kwargs)
+        # We may modify this (to add Host, Accept-Encoding, etc),
+        # so make sure we don't modify the caller's object.  This is also
+        # where normal dicts get converted to HTTPHeaders objects.
+        request.headers = httputil.HTTPHeaders(request.headers)
+        request = _RequestProxy(request, self.defaults)
+        self.fetch_impl(request, callback)
+
+    def fetch_impl(self, request, callback):
         raise NotImplementedError()
 
     @classmethod
index bbd6b70b874079c38a247648a6b005ce75896aca..a2d18a3392e703118947d618bde0ba322427db70 100644 (file)
@@ -45,7 +45,7 @@ class SimpleAsyncHTTPClient(AsyncHTTPClient):
     are not reused, and callers cannot select the network interface to be
     used.
     """
-    def initialize(self, io_loop=None, max_clients=10,
+    def initialize(self, io_loop, max_clients=10,
                    hostname_mapping=None, max_buffer_size=104857600,
                    resolver=None, defaults=None):
         """Creates a AsyncHTTPClient.
@@ -67,25 +67,16 @@ class SimpleAsyncHTTPClient(AsyncHTTPClient):
         max_buffer_size is the number of bytes that can be read by IOStream. It
         defaults to 100mb.
         """
-        self.io_loop = io_loop
+        super(SimpleAsyncHTTPClient, self).initialize(io_loop,
+                                                      defaults=defaults)
         self.max_clients = max_clients
         self.queue = collections.deque()
         self.active = {}
         self.hostname_mapping = hostname_mapping
         self.max_buffer_size = max_buffer_size
         self.resolver = resolver or Resolver(io_loop=io_loop)
-        self.defaults = dict(HTTPRequest._DEFAULTS)
-        if defaults is not None:
-            self.defaults.update(defaults)
-
-    def fetch(self, request, callback, **kwargs):
-        if not isinstance(request, HTTPRequest):
-            request = HTTPRequest(url=request, **kwargs)
-        # We're going to modify this (to add Host, Accept-Encoding, etc),
-        # so make sure we don't modify the caller's object.  This is also
-        # where normal dicts get converted to HTTPHeaders objects.
-        request.headers = HTTPHeaders(request.headers)
-        request = _RequestProxy(request, self.defaults)
+
+    def fetch_impl(self, request, callback):
         callback = stack_context.wrap(callback)
         self.queue.append((request, callback))
         self._process_queue()