hooks.py 820 B

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