Skip to content

Commit

Permalink
Merge pull request psf#1099 from sprt/master
Browse files Browse the repository at this point in the history
Make hooks work with prepared requests
  • Loading branch information
Kenneth Reitz committed Jan 24, 2013
2 parents beec48c + ab2e7df commit 22623bd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
7 changes: 7 additions & 0 deletions requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
17 changes: 7 additions & 10 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
13 changes: 13 additions & 0 deletions test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 22623bd

Please sign in to comment.