Skip to content
Myr

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

3 files+27 added−10 removed

modified:requests/models.py+7 added
223 unmodified lines
224 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)230231 return p232233
188 unmodified lines
422 cookie_header = get_cookie_header(cookies, self)423 if cookie_header is not None:424 self.headers['Cookie'] = cookie_header425426 def prepare_hooks(self, hooks):427 """Prepares the given hooks."""428 for event in hooks:429 self.register_hook(event, hooks[event])430431432class Response(object):433 """The :class:`Response <Response>` object, which contains a434 server's response to an HTTP request.435 """
222 unmodified lines
modified:requests/sessions.py+7 added-10 removed
11 unmodified lines
1213from .compat import cookielib14from .cookies import cookiejar_from_dict15from .models import Request16from .hooks import dispatch_hook, default_hooks16from .hooks import default_hooks, dispatch_hook17from .utils import from_key_val_list, default_headers18from .exceptions import TooManyRedirects, InvalidSchema1920from .compat import urlparse, urljoin
108 unmodified lines
129 stream=stream,130 timeout=timeout,131 verify=verify,132 cert=cert,133 proxies=proxies134 )133 proxies=proxies,134 hooks=req.hooks,135 )136137 i += 1138 yield resp139
135 unmodified lines
275276 # Prepare the Request.277 prep = req.prepare()278278 # 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)281279 # Send the request.280 resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)281282 # Persist cookies.
11 unmodified lines
294 history.insert(0, resp)295 resp = history.pop()296 resp.history = tuple(history)297301 # Response manipulation hook.302 resp = dispatch_hook('response', hooks, resp)303298 return resp299300 def get(self, url, **kwargs):301 """Sends a GET request. Returns :class:`Response` object.
65 unmodified lines
367 return self.request('DELETE', url, **kwargs)368369 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 r377378 def get_adapter(self, url):379 """Returns the appropriate connnection adapter for the given URL."""
29 unmodified lines
modified:test_requests.py+13 added
259 unmodified lines
260 'text/py-content-type')})261 self.assertEqual(r.status_code, 200)262 self.assertTrue(b"text/py-content-type" in r.request.body)263264 def test_prepared_request_hook(self):265 def hook(resp):266 resp.hook_working = True267 return resp268269 req = requests.Request('GET', HTTPBIN, hooks={'response': hook})270 prep = req.prepare()271272 s = requests.Session()273 resp = s.send(prep)274275 self.assertTrue(hasattr(resp, 'hook_working'))276277 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