mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
* hooks is a package now.
* Added hooks.setup_hooks to setup multiple and default hooks * Added hooks.response hooks to unicode encoding, and uncompress response. * Modified hooks.dispatch_hooks api: don't need a key any more.
This commit is contained in:
+8
-8
@@ -14,7 +14,7 @@ This module impliments the Requests API.
|
||||
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
|
||||
@@ -43,10 +43,9 @@ def request(method, url,
|
||||
|
||||
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,
|
||||
@@ -61,23 +60,24 @@ def request(method, url,
|
||||
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
|
||||
|
||||
|
||||
+3
-1
@@ -62,7 +62,9 @@ settings.proxies = None
|
||||
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
|
||||
|
||||
@@ -24,8 +24,24 @@ Available hooks:
|
||||
|
||||
import warnings
|
||||
from collections import Iterable
|
||||
from .. import config
|
||||
from response import unicode_response, decode_response
|
||||
|
||||
def dispatch_hooks(key, hooks, hook_data):
|
||||
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
|
||||
@@ -36,13 +52,10 @@ def dispatch_hooks(key, hooks, hook_data):
|
||||
: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:
|
||||
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
|
||||
|
||||
+12
-14
@@ -444,22 +444,20 @@ class Response(object):
|
||||
(if available).
|
||||
"""
|
||||
|
||||
if self._content is not None:
|
||||
return self._content
|
||||
if self._content is None:
|
||||
# Read the contents.
|
||||
self._content = self.fo.read()
|
||||
|
||||
# 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 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)
|
||||
# # Decode unicode content.
|
||||
# if settings.decode_unicode:
|
||||
# self._content = get_unicode_from_response(self)
|
||||
|
||||
return self._content
|
||||
|
||||
|
||||
Reference in New Issue
Block a user