defaults['max_redirects'] = 30
defaults['decode_unicode'] = True
defaults['keepalive'] = True
+defaults['max_connections'] = 10
def request(method, url,
params=None, data=None, headers=None, cookies=None, files=None, auth=None,
timeout=None, allow_redirects=False, proxies=None, hooks=None,
- config=None, _connection=None):
+ config=None, _pools=None):
"""Constructs and sends a :class:`Request <Request>`.
Returns :class:`Response <Response>` object.
:param timeout: (optional) Float describing the timeout of the request.
:param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
- :param _connection: (optional) An HTTP Connection to re-use.
+ :param _pools: (optional) An HTTP PoolManager to use.
"""
method = str(method).upper()
r = dispatch_hook('pre_request', hooks, r)
# Send the HTTP Request.
- r.send(connection=_connection)
+ r.send(pools=_pools)
# Post-request hook.
r = dispatch_hook('post_request', hooks, r)
- def send(self, connection=None, anyway=False):
+ def send(self, pools=None, anyway=False):
"""Sends the shit."""
# Safety check.
try:
# Create a new HTTP connection, since one wasn't passed in.
- if not connection:
+ if not pools:
connection = urllib3.connection_from_url(url, timeout=self.timeout)
# One-off request. Delay fetching the content until needed.
do_block = False
else:
+ connection = pools.connection_from_url(url, timeout=self.timeout)
# Part of a connection pool, so no fancy stuff. Sorry!
do_block = True
from . import api
from ._config import get_config
from .utils import add_dict_to_cookiejar
+from .packages.urllib3.poolmanager import PoolManager
def merge_kwargs(local_kwarg, default_kwarg):
self.hooks = hooks
self.config = get_config(config)
+ self.__pool = PoolManager(
+ num_pools=self.config.get('max_connections')
+ )
+
# Map and wrap requests.api methods.
self._map_api_methods()
if k not in _kwargs:
_kwargs[k] = v
+ # Add in PoolManager, if neccesary.
+ if self.config.get('keepalive'):
+ _kwargs['_pool'] = self.__pool
+
# TODO: Persist cookies.
return func(*args, **_kwargs)