hooks.py 767 B

12345678910111213141516171819202122232425262728293031323334
  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. return dict((event, []) for event in HOOKS)
  13. # TODO: response is the only one
  14. def dispatch_hook(key, hooks, hook_data, **kwargs):
  15. """Dispatches a hook dictionary on a given piece of data."""
  16. hooks = hooks or dict()
  17. hooks = hooks.get(key)
  18. if hooks:
  19. if hasattr(hooks, '__call__'):
  20. hooks = [hooks]
  21. for hook in hooks:
  22. _hook_data = hook(hook_data, **kwargs)
  23. if _hook_data is not None:
  24. hook_data = _hook_data
  25. return hook_data