jpayne@7: """ jpayne@7: requests.hooks jpayne@7: ~~~~~~~~~~~~~~ jpayne@7: jpayne@7: This module provides the capabilities for the Requests hooks system. jpayne@7: jpayne@7: Available hooks: jpayne@7: jpayne@7: ``response``: jpayne@7: The response generated from a Request. jpayne@7: """ jpayne@7: HOOKS = ["response"] jpayne@7: jpayne@7: jpayne@7: def default_hooks(): jpayne@7: return {event: [] for event in HOOKS} jpayne@7: jpayne@7: jpayne@7: # TODO: response is the only one jpayne@7: jpayne@7: jpayne@7: def dispatch_hook(key, hooks, hook_data, **kwargs): jpayne@7: """Dispatches a hook dictionary on a given piece of data.""" jpayne@7: hooks = hooks or {} jpayne@7: hooks = hooks.get(key) jpayne@7: if hooks: jpayne@7: if hasattr(hooks, "__call__"): jpayne@7: hooks = [hooks] jpayne@7: for hook in hooks: jpayne@7: _hook_data = hook(hook_data, **kwargs) jpayne@7: if _hook_data is not None: jpayne@7: hook_data = _hook_data jpayne@7: return hook_data