From d0cb56932c251667420e4876f99f5fa6b5c8a9a6 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Tue, 16 Aug 2011 23:57:44 -0400 Subject: [PATCH] new hooks system #118 --- requests/api.py | 13 +++++++++---- requests/hooks.py | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 requests/hooks.py diff --git a/requests/api.py b/requests/api.py index 1a2ce30..a1a3938 100644 --- a/requests/api.py +++ b/requests/api.py @@ -12,15 +12,16 @@ This module impliments the Requests API. """ import config -from .models import Request, Response, AuthManager, AuthObject, auth_manager +from .models import Request, Response, AuthObject from .status_codes import codes +from .hooks import dispatch_hook __all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete') def request(method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, - timeout=None, allow_redirects=False, proxies=None): + timeout=None, allow_redirects=False, proxies=None, hooks=None): """Constructs and sends a :class:`Request `. Returns :class:`Response ` object. @@ -37,7 +38,7 @@ def request(method, url, :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. """ - r = Request( + args = dict( method = method, url = url, data = data, @@ -45,12 +46,16 @@ def request(method, url, headers = headers, cookiejar = cookies, files = files, - auth = auth or auth_manager.get_auth(url), + auth = auth, timeout = timeout or config.settings.timeout, allow_redirects = allow_redirects, proxies = proxies or config.settings.proxies ) + args = dispatch_hook('args', hooks, args) + + r = Request(**args) + r.send() return r.response diff --git a/requests/hooks.py b/requests/hooks.py new file mode 100644 index 0000000..7e9f23a --- /dev/null +++ b/requests/hooks.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +requests.hooks +~~~~~~~~~~~~~~ + +This module provides the capabilities for the Requests hooks system. +""" + +import warnings + +def dispatch_hook(key, hooks, hook_data): + """""" + + hooks = hooks or dict() + + if key in hooks: + try: + return hooks.get(key).__call__(hook_data) or hook_data + + except Exception, why: + warnings.warn(str(why)) + + + return hook_data -- 2.34.1