hooks.py 733 B

123456789101112131415161718192021222324252627282930313233
  1. """
  2. requests.hooks
  3. ~~~~~~~~~~~~~~
  4. This module provides the capabilities for the Requests hooks system.
  5. Available hooks:
  6. ``response``:
  7. The response generated from a Request.
  8. """
  9. HOOKS = ["response"]
  10. def default_hooks():
  11. return {event: [] for event in HOOKS}
  12. # TODO: response is the only one
  13. def dispatch_hook(key, hooks, hook_data, **kwargs):
  14. """Dispatches a hook dictionary on a given piece of data."""
  15. hooks = hooks or {}
  16. hooks = hooks.get(key)
  17. if hooks:
  18. if hasattr(hooks, "__call__"):
  19. hooks = [hooks]
  20. for hook in hooks:
  21. _hook_data = hook(hook_data, **kwargs)
  22. if _hook_data is not None:
  23. hook_data = _hook_data
  24. return hook_data