Merge pull request #1099 from sprt/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Make hooks work with prepared requests
authored by Kenneth Reitz · committed by Kenneth Reitz · parents beec48c, ab2e7df · Browse files · 3 files·+27 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+27 added−10 removed
- other:+27 added−10 removed
Provenance
- Commit
22623bd- Tree
3b8ba30- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
beec48c- Parent 2
ab2e7df
3 files changed+27 added−10 removed
modified:requests/models.py+7 added
223 unmodified lines224 p.prepare_body(self.data, self.files)225 # Note that prepare_auth must be last to enable authentication schemes226 # such as OAuth to work on a fully prepared request.227 p.prepare_auth(self.auth)228+ # This MUST go after prepare_auth. Authenticators could add a hook229+ p.prepare_hooks(self.hooks)230 231 return p232 233 188 unmodified lines422 cookie_header = get_cookie_header(cookies, self)423 if cookie_header is not None:424 self.headers['Cookie'] = cookie_header425 426+ def prepare_hooks(self, hooks):427+ """Prepares the given hooks."""428+ for event in hooks:429+ self.register_hook(event, hooks[event])430 431+432 class Response(object):433 """The :class:`Response <Response>` object, which contains a434 server's response to an HTTP request.435 """222 unmodified linesmodified:requests/sessions.py+7 added-10 removed
11 unmodified lines12 13 from .compat import cookielib14 from .cookies import cookiejar_from_dict15 from .models import Request16-from .hooks import dispatch_hook, default_hooks16+from .hooks import default_hooks, dispatch_hook17 from .utils import from_key_val_list, default_headers18 from .exceptions import TooManyRedirects, InvalidSchema19 20 from .compat import urlparse, urljoin108 unmodified lines129 stream=stream,130 timeout=timeout,131 verify=verify,132 cert=cert,133- proxies=proxies134- )133+ proxies=proxies,134+ hooks=req.hooks,135+ )136 137 i += 1138 yield resp139 135 unmodified lines275 276 # Prepare the Request.277 prep = req.prepare()278 278- # If auth hooks are present, they aren't passed to `dispatch_hook`279- # As such, we need to update the original hooks dictionary with them280- hooks.update(prep.hooks)281-279 # Send the request.280 resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)281 282 # Persist cookies.11 unmodified lines294 history.insert(0, resp)295 resp = history.pop()296 resp.history = tuple(history)297 301- # Response manipulation hook.302- resp = dispatch_hook('response', hooks, resp)303-298 return resp299 300 def get(self, url, **kwargs):301 """Sends a GET request. Returns :class:`Response` object.65 unmodified lines367 return self.request('DELETE', url, **kwargs)368 369 def send(self, request, **kwargs):370 """Send a given PreparedRequest."""371+ hooks = request.hooks372 adapter = self.get_adapter(url=request.url)373 r = adapter.send(request, **kwargs)374+ # Response manipulation hooks375+ r = dispatch_hook('response', hooks, r)376 return r377 378 def get_adapter(self, url):379 """Returns the appropriate connnection adapter for the given URL."""29 unmodified linesmodified:test_requests.py+13 added
259 unmodified lines260 'text/py-content-type')})261 self.assertEqual(r.status_code, 200)262 self.assertTrue(b"text/py-content-type" in r.request.body)263 264+ def test_prepared_request_hook(self):265+ def hook(resp):266+ resp.hook_working = True267+ return resp268+269+ req = requests.Request('GET', HTTPBIN, hooks={'response': hook})270+ prep = req.prepare()271+272+ s = requests.Session()273+ resp = s.send(prep)274+275+ self.assertTrue(hasattr(resp, 'hook_working'))276+277 def test_links(self):278 url = 'https://api.github.com/users/kennethreitz/repos?page=1&per_page=10'279 r = requests.head(url=url)280 self.assertEqual(r.links['next']['rel'], 'next')4 unmodified lines