import config
from .models import Request, Response, AuthObject
from .status_codes import codes
-from .hooks import dispatch_hooks
+from hooks import setup_hooks, dispatch_hooks
from .utils import cookiejar_from_dict
from urlparse import urlparse
method = str(method).upper()
- if cookies is None:
- cookies = {}
+ cookies = cookiejar_from_dict(cookies or dict())
- cookies = cookiejar_from_dict(cookies)
+ hooks = setup_hooks(hooks or dict())
args = dict(
method = method,
allow_redirects = allow_redirects,
proxies = proxies or config.settings.proxies,
)
+
# Arguments manipulation hook.
- args = dispatch_hooks('args', hooks, args)
+ args = dispatch_hooks(hooks.get('args', []), args)
r = Request(**args)
# Pre-request hook.
- r = dispatch_hooks('pre_request', hooks, r)
+ r = dispatch_hooks(hooks.get('pre_request', []), r)
# Send the HTTP Request.
r.send()
# Post-request hook.
- r = dispatch_hooks('post_request', hooks, r)
+ r = dispatch_hooks(hooks.get('post_request', []), hooks, r)
# Response manipulation hook.
- r.response = dispatch_hooks('response', hooks, r.response)
+ r.response = dispatch_hooks(hooks.get('response', []), r.response)
return r.response
settings.verbose = None
settings.timeout = None
settings.max_redirects = 30
-settings.decode_unicode = True
+# settings.decode_unicode = True
+settings.unicode_response = True
+settings.decode_response = True
#: Use socket.setdefaulttimeout() as fallback?
settings.timeout_fallback = True
+++ /dev/null
-# -*- coding: utf-8 -*-
-
-"""
-requests.hooks
-~~~~~~~~~~~~~~
-
-This module provides the capabilities for the Requests hooks system.
-
-Available hooks:
-
-``args``:
- A dictionary of the arguments being sent to Request().
-
-``pre_request``:
- The Request object, directly before being sent.
-
-``post_request``:
- The Request object, directly after being sent.
-
-``response``:
- The response generated from a Request.
-
-"""
-
-import warnings
-from collections import Iterable
-
-def dispatch_hooks(key, hooks, hook_data):
- """Dispatches multiple hooks on a given piece of data.
-
- :param key: the hooks group to lookup
- :type key: str
- :param hooks: the hooks dictionary. The value of each key can be a callable
- object, or a list of callable objects.
- :type hooks: dict
- :param hook_data: the object on witch the hooks should be applied
- :type hook_data: object
- """
- hook_list = hooks.get(key, []) if hooks else []
- dispatching = hook_list if isinstance(hook_list, Iterable) else [hook_list]
- for hook in dispatching:
- try:
- # hook must be a callable
- hook_data = hook(hook_data)
- except Exception, why:
- warnings.warn(str(why))
- return hook_data
-
--- /dev/null
+# -*- coding: utf-8 -*-
+
+"""
+requests.hooks
+~~~~~~~~~~~~~~
+
+This module provides the capabilities for the Requests hooks system.
+
+Available hooks:
+
+``args``:
+ A dictionary of the arguments being sent to Request().
+
+``pre_request``:
+ The Request object, directly before being sent.
+
+``post_request``:
+ The Request object, directly after being sent.
+
+``response``:
+ The response generated from a Request.
+
+"""
+
+import warnings
+from collections import Iterable
+from .. import config
+from response import unicode_response, decode_response
+
+def setup_hooks(hooks):
+ """Setup hooks as a dictionary. Each value is a set of hooks."""
+
+ for key, values in hooks.items():
+ hook_list = values if isinstance(values, Iterable) else [values]
+ hooks[key] = set(hook_list)
+
+ # Also, based on settings,
+ if config.settings.unicode_response:
+ hooks.setdefault('response', set()).add(unicode_response)
+ if config.settings.decode_response:
+ hooks.setdefault('response', set()).add(decode_response)
+ return hooks
+
+def dispatch_hooks(hooks, hook_data):
+ """Dispatches multiple hooks on a given piece of data.
+
+ :param key: the hooks group to lookup
+ :type key: str
+ :param hooks: the hooks dictionary. The value of each key can be a callable
+ object, or a list of callable objects.
+ :type hooks: dict
+ :param hook_data: the object on witch the hooks should be applied
+ :type hook_data: object
+ """
+ for hook in hooks:
+ try:
+ # hook must be a callable
+ hook_data = hook(hook_data)
+ except Exception, why:
+ warnings.warn(str(why))
+ return hook_data
(if available).
"""
- if self._content is not None:
- return self._content
-
- # Read the contents.
- self._content = self.fo.read()
-
- # Decode GZip'd content.
- if 'gzip' in self.headers.get('content-encoding', ''):
- try:
- self._content = decode_gzip(self._content)
- except zlib.error:
- pass
-
- # Decode unicode content.
- if settings.decode_unicode:
- self._content = get_unicode_from_response(self)
+ if self._content is None:
+ # Read the contents.
+ self._content = self.fo.read()
+
+ # # Decode GZip'd content.
+ # if 'gzip' in self.headers.get('content-encoding', ''):
+ # try:
+ # self._content = decode_gzip(self._content)
+ # except zlib.error:
+ # pass
+
+ # # Decode unicode content.
+ # if settings.decode_unicode:
+ # self._content = get_unicode_from_response(self)
return self._content