new hooks system
authorKenneth Reitz <me@kennethreitz.com>
Wed, 17 Aug 2011 03:57:44 +0000 (23:57 -0400)
committerKenneth Reitz <me@kennethreitz.com>
Wed, 17 Aug 2011 03:57:44 +0000 (23:57 -0400)
#118

requests/api.py
requests/hooks.py [new file with mode: 0644]

index 1a2ce30084b32c3ac4ed7899759cfb0d8933bb88..a1a3938784a647c77e9a93df2fdde2224ba66308 100644 (file)
@@ -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 <models.Request>`. Returns :class:`Response <models.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 (file)
index 0000000..7e9f23a
--- /dev/null
@@ -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