You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hooks.py 757B

5 years ago
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 {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 {}
  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