pre_request hook

This commit is contained in:
Kenneth Reitz
2011-08-17 00:27:53 -04:00
parent 8ef0e88220
commit 3e8a14e133
2 changed files with 16 additions and 4 deletions
+2 -2
View File
@@ -11,10 +11,10 @@ Available hooks:
``args``:
A dictionary of the arguments being sent to Request().
``pre-request``:
``pre_request``:
The Request object, directly before being sent.
``post-request``:
``post_request``:
The Request object, directly after being sent.
``response``:
+14 -2
View File
@@ -16,6 +16,7 @@ from urlparse import urlparse, urlunparse, urljoin
from datetime import datetime
from .config import settings
from .hooks import dispatch_hook
from .monkeys import Request as _Request, HTTPBasicAuthHandler, HTTPForcedBasicAuthHandler, HTTPDigestAuthHandler, HTTPRedirectHandler
from .structures import CaseInsensitiveDict
from .packages.poster.encode import multipart_encode
@@ -36,7 +37,7 @@ class Request(object):
def __init__(self,
url=None, headers=dict(), files=None, method=None, data=dict(),
params=dict(), auth=None, cookiejar=None, timeout=None, redirect=False,
allow_redirects=False, proxies=None):
allow_redirects=False, proxies=None, hooks=None):
#: Float describ the timeout of the request.
# (Use socket.setdefaulttimeout() as fallback)
@@ -93,6 +94,9 @@ class Request(object):
#: True if Request has been sent.
self.sent = False
#: Dictionary of event hook callbacks.
self.hooks = hooks
# Header manipulation and defaults.
@@ -323,10 +327,18 @@ class Request(object):
try:
opener = self._get_opener()
try:
# pre-request hook.
self.__dict__.update(
dispatch_hook('pre_request',
self.hooks, self.__dict__)
)
resp = opener(req, timeout=self.timeout)
except TypeError, err:
# timeout argument is new since Python v2.6
if not "timeout" in str(err):
if not 'timeout' in str(err):
raise
if settings.timeout_fallback: