Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1099 from sprt/master

Make hooks work with prepared requests
  • Loading branch information...
commit 22623bd8c265b78b161542663ee980738441c307 2 parents beec48c + ab2e7df
Kenneth Reitz authored
Showing with 27 additions and 10 deletions.
  1. +7 −0 requests/models.py
  2. +7 −10 requests/sessions.py
  3. +13 −0 test_requests.py
7 requests/models.py
View
@@ -225,6 +225,8 @@ def prepare(self):
# Note that prepare_auth must be last to enable authentication schemes
# such as OAuth to work on a fully prepared request.
p.prepare_auth(self.auth)
+ # This MUST go after prepare_auth. Authenticators could add a hook
+ p.prepare_hooks(self.hooks)
return p
@@ -421,6 +423,11 @@ def prepare_cookies(self, cookies):
if cookie_header is not None:
self.headers['Cookie'] = cookie_header
+ def prepare_hooks(self, hooks):
+ """Prepares the given hooks."""
+ for event in hooks:
+ self.register_hook(event, hooks[event])
+
class Response(object):
"""The :class:`Response <Response>` object, which contains a
17 requests/sessions.py
View
@@ -13,7 +13,7 @@
from .compat import cookielib
from .cookies import cookiejar_from_dict
from .models import Request
-from .hooks import dispatch_hook, default_hooks
+from .hooks import default_hooks, dispatch_hook
from .utils import from_key_val_list, default_headers
from .exceptions import TooManyRedirects, InvalidSchema
@@ -130,8 +130,9 @@ def resolve_redirects(self, resp, req, stream=False, timeout=None, verify=True,
timeout=timeout,
verify=verify,
cert=cert,
- proxies=proxies
- )
+ proxies=proxies,
+ hooks=req.hooks,
+ )
i += 1
yield resp
@@ -275,10 +276,6 @@ def request(self, method, url,
# Prepare the Request.
prep = req.prepare()
- # If auth hooks are present, they aren't passed to `dispatch_hook`
- # As such, we need to update the original hooks dictionary with them
- hooks.update(prep.hooks)
-
# Send the request.
resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
@@ -298,9 +295,6 @@ def request(self, method, url,
resp = history.pop()
resp.history = tuple(history)
- # Response manipulation hook.
- resp = dispatch_hook('response', hooks, resp)
-
return resp
def get(self, url, **kwargs):
@@ -374,8 +368,11 @@ def delete(self, url, **kwargs):
def send(self, request, **kwargs):
"""Send a given PreparedRequest."""
+ hooks = request.hooks
adapter = self.get_adapter(url=request.url)
r = adapter.send(request, **kwargs)
+ # Response manipulation hooks
+ r = dispatch_hook('response', hooks, r)
return r
def get_adapter(self, url):
13 test_requests.py
View
@@ -261,6 +261,19 @@ def test_custom_content_type(self):
self.assertEqual(r.status_code, 200)
self.assertTrue(b"text/py-content-type" in r.request.body)
+ def test_prepared_request_hook(self):
+ def hook(resp):
+ resp.hook_working = True
+ return resp
+
+ req = requests.Request('GET', HTTPBIN, hooks={'response': hook})
+ prep = req.prepare()
+
+ s = requests.Session()
+ resp = s.send(prep)
+
+ self.assertTrue(hasattr(resp, 'hook_working'))
+
def test_links(self):
url = 'https://api.github.com/users/kennethreitz/repos?page=1&per_page=10'
r = requests.head(url=url)
Please sign in to comment.
Something went wrong with that request. Please try again.