Make hooks work with prepared requests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sprt · committed by sprt · parent 3491761 · Browse files · 3 files·+31 added−5 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+31 added−5 removed
- other:+31 added−5 removed
Provenance
- Commit
a721d59- Tree
5394834- Author
sprt <hellosprt@gmail.com>- Committer
sprt <hellosprt@gmail.com>- Parent
3491761
3 files changed+31 added−5 removed
modified:requests/models.py+17 added-1 removed
9 unmodified lines10 import collections11 import logging12 13 from io import BytesIO14-from .hooks import default_hooks14+from .hooks import default_hooks, HOOKS15 from .structures import CaseInsensitiveDict16 from .status_codes import codes17 18 from .auth import HTTPBasicAuth205 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+ p.prepare_hooks(self.hooks)229 230 return p231 232 182 unmodified lines415 cookie_header = get_cookie_header(cookies, self)416 if cookie_header is not None:417 self.headers['Cookie'] = cookie_header418 419+ def prepare_hooks(self, hooks):420+ """Prepares the given hooks."""421+ for event in HOOKS:422+ if event not in self.hooks:423+ self.hooks[event] = []424+ if event not in hooks:425+ hooks[event] = []426 427+ if not hasattr(self.hooks[event], '__iter__'):428+ self.hooks[event] = [self.hooks[event]]429+ if not hasattr(hooks[event], '__iter__'):430+ hooks[event] = [hooks[event]]431+432+ self.hooks[event].extend(hooks[event])433+434+435 class Response(object):436 """The :class:`Response <Response>` object, which contains a437 server's response to an HTTP request.438 """219 unmodified linesmodified:requests/sessions.py+1 added-4 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_hooks17 from .utils import from_key_val_list, default_headers18 from .exceptions import TooManyRedirects, InvalidSchema19 20 from .compat import urlparse, urljoin272 unmodified lines293 history.insert(0, resp)294 resp = history.pop()295 resp.history = tuple(history)296 297- # Response manipulation hook.298- self.response = dispatch_hook('response', hooks, resp)299-297 return resp298 299 def get(self, url, **kwargs):300 """Sends a GET request. Returns :class:`Response` object.104 unmodified linesmodified:test_requests.py+13 added
262 unmodified lines263 'text/py-content-type')})264 self.assertEqual(r.status_code, 200)265 self.assertTrue(b"text/py-content-type" in r.request.body)266 267+ def test_prepared_request_hook(self):268+ def hook(resp):269+ resp.hook_working = True270+ return resp271 272+ req = requests.Request('GET', HTTPBIN, hooks={'response': hook})273+ prep = req.prepare()274+275+ s = requests.Session()276+ resp = s.send(prep)277+278+ self.assertTrue(hasattr(resp, 'hook_working'))279+280+281 if __name__ == '__main__':282 unittest.main()