Skip to content
Myr

Added multiple hooks support.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Luca De Vitis · committed by Luca De Vitis · parent 317c5b6 · Browse files · 2 files+22 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+22 added−14 removed

  • other:+22 added−14 removed

2 files+22 added−14 removed

modified:requests/api.py+5 added-5 removed
12 unmodified lines
1314import config15from .models import Request, Response, AuthObject16from .status_codes import codes17from .hooks import dispatch_hook17from .hooks import dispatch_hooks18from .utils import cookiejar_from_dict1920from urlparse import urlparse21
40 unmodified lines
62 proxies = proxies or config.settings.proxies,63 )6465 # Arguments manipulation hook.66 args = dispatch_hook('args', hooks, args)66 args = dispatch_hooks('args', hooks, args)6768 r = Request(**args)6970 # Pre-request hook.71 r = dispatch_hook('pre_request', hooks, r)71 r = dispatch_hooks('pre_request', hooks, r)7273 # Send the HTTP Request.74 r.send()7576 # Post-request hook.77 r = dispatch_hook('post_request', hooks, r)77 r = dispatch_hooks('post_request', hooks, r)7879 # Response manipulation hook.80 r.response = dispatch_hook('response', hooks, r.response)80 r.response = dispatch_hooks('response', hooks, r.response)8182 return r.response8384
62 unmodified lines
modified:requests/hooks.py+17 added-9 removed
21 unmodified lines
2223"""2425import warnings26from collections import Iterable2728def dispatch_hooks(key, hooks, hook_data):29 """Dispatches multiple hooks on a given piece of data.3028def dispatch_hook(key, hooks, hook_data):29 """Dipatches a hook dictionary on a given peice of data."""3031 hooks = hooks or dict()3233 if key in hooks:31 :param key: the hooks group to lookup32 :type key: str33 :param hooks: the hooks dictionary. The value of each key can be a callable34 object, or a list of callable objects.35 :type hooks: dict36 :param hook_data: the object on witch the hooks should be applied37 :type hook_data: object38 """39 hook_list = hooks.get(key, []) if hooks else []40 dispatching = hook_list if isinstance(hook_list, Iterable) else [hook_list]41 for hook in dispatching:42 try:35 return hooks.get(key).__call__(hook_data) or hook_data3643 # hook must be a callable44 hook_data = hook(hook_data)45 except Exception, why:46 warnings.warn(str(why))3947 return hook_data48