pre_request hook
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 8ef0e88 · Browse files · 2 files·+16 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+16 added−4 removed
- other:+16 added−4 removed
Provenance
- Commit
3e8a14e- Tree
da65b1c- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
8ef0e88
2 files changed+16 added−4 removed
modified:requests/hooks.py+2 added-2 removed
9 unmodified lines10 11 ``args``:12 A dictionary of the arguments being sent to Request().13 14-``pre-request``:14+``pre_request``:15 The Request object, directly before being sent.16 17-``post-request``:17+``post_request``:18 The Request object, directly after being sent.19 20 ``response``:21 The response generated from a Request.19 unmodified linesmodified:requests/models.py+14 added-2 removed
14 unmodified lines15 from urlparse import urlparse, urlunparse, urljoin16 from datetime import datetime17 18 from .config import settings19+from .hooks import dispatch_hook20 from .monkeys import Request as _Request, HTTPBasicAuthHandler, HTTPForcedBasicAuthHandler, HTTPDigestAuthHandler, HTTPRedirectHandler21 from .structures import CaseInsensitiveDict22 from .packages.poster.encode import multipart_encode23 from .packages.poster.streaminghttp import register_openers, get_handlers12 unmodified lines36 37 def __init__(self,38 url=None, headers=dict(), files=None, method=None, data=dict(),39 params=dict(), auth=None, cookiejar=None, timeout=None, redirect=False,39- allow_redirects=False, proxies=None):40+ allow_redirects=False, proxies=None, hooks=None):41 42 #: Float describ the timeout of the request.43 # (Use socket.setdefaulttimeout() as fallback)44 self.timeout = timeout48 unmodified lines93 94 #: True if Request has been sent.95 self.sent = False96 97+ #: Dictionary of event hook callbacks.98+ self.hooks = hooks99 100+101 # Header manipulation and defaults.102 103 if settings.accept_gzip:104 settings.base_headers.update({'Accept-Encoding': 'gzip'})221 unmodified lines326 327 try:328 opener = self._get_opener()329 try:330+331+ # pre-request hook.332+ self.__dict__.update(333+ dispatch_hook('pre_request',334+ self.hooks, self.__dict__)335+ )336+337 resp = opener(req, timeout=self.timeout)338+339 except TypeError, err:340 # timeout argument is new since Python v2.6329- if not "timeout" in str(err):341+ if not 'timeout' in str(err):342 raise343 344 if settings.timeout_fallback:345 # fall-back and use global socket timeout (This is not thread-safe!)266 unmodified lines