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