From: Kenneth Reitz Date: Wed, 17 Aug 2011 03:57:44 +0000 (-0400) Subject: new hooks system X-Git-Tag: v0.6.0~43 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d0cb56932c251667420e4876f99f5fa6b5c8a9a6;p=services%2Fpython-requests.git new hooks system #118 --- 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