* hooks is a package now.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* 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.
authored by Luca De Vitis · committed by Luca De Vitis · parent 2dc556e · Browse files · 5 files·+84 added−71 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+84 added−71 removed
- other:+84 added−71 removed
Provenance
- Commit
d12eac8- Tree
fa17123- Author
Luca De Vitis <luca@monkeython.com>- Committer
Luca De Vitis <luca@monkeython.com>- Parent
2dc556e
5 files changed+84 added−71 removed
added:requests/hooks/__init__.py+61 added
1+# -*- coding: utf-8 -*-2+3+"""4+requests.hooks5+~~~~~~~~~~~~~~6+7+This module provides the capabilities for the Requests hooks system.8+9+Available hooks:10+11+``args``:12+ A dictionary of the arguments being sent to Request().13+14+``pre_request``:15+ The Request object, directly before being sent.16+17+``post_request``:18+ The Request object, directly after being sent.19+20+``response``:21+ The response generated from a Request.22+23+"""24+25+import warnings26+from collections import Iterable27+from .. import config28+from response import unicode_response, decode_response29+30+def setup_hooks(hooks):31+ """Setup hooks as a dictionary. Each value is a set of hooks."""32+33+ for key, values in hooks.items():34+ hook_list = values if isinstance(values, Iterable) else [values]35+ hooks[key] = set(hook_list) 36+37+ # Also, based on settings, 38+ if config.settings.unicode_response:39+ hooks.setdefault('response', set()).add(unicode_response)40+ if config.settings.decode_response:41+ hooks.setdefault('response', set()).add(decode_response)42+ return hooks43+44+def dispatch_hooks(hooks, hook_data):45+ """Dispatches multiple hooks on a given piece of data.46+47+ :param key: the hooks group to lookup48+ :type key: str49+ :param hooks: the hooks dictionary. The value of each key can be a callable50+ object, or a list of callable objects.51+ :type hooks: dict52+ :param hook_data: the object on witch the hooks should be applied53+ :type hook_data: object54+ """55+ for hook in hooks:56+ try:57+ # hook must be a callable58+ hook_data = hook(hook_data)59+ except Exception, why:60+ warnings.warn(str(why))61+ return hook_datamodified:requests/api.py+8 added-8 removed
12 unmodified lines13 14 import config15 from .models import Request, Response, AuthObject16 from .status_codes import codes17-from .hooks import dispatch_hooks17+from hooks import setup_hooks, dispatch_hooks18 from .utils import cookiejar_from_dict19 20 from urlparse import urlparse21 20 unmodified lines42 """43 44 method = str(method).upper()45 46- if cookies is None:47- cookies = {}46+ cookies = cookiejar_from_dict(cookies or dict())47 49- cookies = cookiejar_from_dict(cookies)48+ hooks = setup_hooks(hooks or dict())49 50 args = dict(51 method = method,52 url = url,6 unmodified lines59 timeout = timeout or config.settings.timeout,60 allow_redirects = allow_redirects,61 proxies = proxies or config.settings.proxies,62 )63+ 64 65 # Arguments manipulation hook.66- args = dispatch_hooks('args', hooks, args)66+ args = dispatch_hooks(hooks.get('args', []), args)67 68 r = Request(**args)69 70 # Pre-request hook.71- r = dispatch_hooks('pre_request', hooks, r)71+ r = dispatch_hooks(hooks.get('pre_request', []), r)72 73 # Send the HTTP Request.74 r.send()75 76 # Post-request hook.77- r = dispatch_hooks('post_request', hooks, r)77+ r = dispatch_hooks(hooks.get('post_request', []), hooks, r)78 79 # Response manipulation hook.80- r.response = dispatch_hooks('response', hooks, r.response)80+ r.response = dispatch_hooks(hooks.get('response', []), r.response)81 82 return r.response83 84 62 unmodified linesmodified:requests/config.py+3 added-1 removed
60 unmodified lines61 settings.proxies = None62 settings.verbose = None63 settings.timeout = None64 settings.max_redirects = 3065-settings.decode_unicode = True65+# settings.decode_unicode = True66+settings.unicode_response = True67+settings.decode_response = True68 69 #: Use socket.setdefaulttimeout() as fallback?70 settings.timeout_fallback = Truedeleted:requests/hooks.py-48 removed
1-# -*- coding: utf-8 -*-2-3-"""4-requests.hooks5-~~~~~~~~~~~~~~6-7-This module provides the capabilities for the Requests hooks system.8-9-Available hooks:10-11-``args``:12- A dictionary of the arguments being sent to Request().13-14-``pre_request``:15- The Request object, directly before being sent.16-17-``post_request``:18- The Request object, directly after being sent.19-20-``response``:21- The response generated from a Request.22-23-"""24-25-import warnings26-from collections import Iterable27-28-def dispatch_hooks(key, hooks, hook_data):29- """Dispatches multiple hooks on a given piece of data.30-31- :param key: the hooks group to lookup32- :type key: str33- :param hooks: the hooks dictionary. The value of each key can be a callable34- object, or a list of callable objects.35- :type hooks: dict36- :param hook_data: the object on witch the hooks should be applied37- :type hook_data: object38- """39- hook_list = hooks.get(key, []) if hooks else []40- dispatching = hook_list if isinstance(hook_list, Iterable) else [hook_list]41- for hook in dispatching:42- try:43- # hook must be a callable44- hook_data = hook(hook_data)45- except Exception, why:46- warnings.warn(str(why))47- return hook_data48-modified:requests/models.py+12 added-14 removed
442 unmodified lines443 """Content of the response, in bytes or unicode444 (if available).445 """446 447- if self._content is not None:448- return self._content447+ if self._content is None:448+ # Read the contents.449+ self._content = self.fo.read()450 450- # Read the contents.451- self._content = self.fo.read()451+ # # Decode GZip'd content.452+ # if 'gzip' in self.headers.get('content-encoding', ''):453+ # try:454+ # self._content = decode_gzip(self._content)455+ # except zlib.error:456+ # pass457 453- # Decode GZip'd content.454- if 'gzip' in self.headers.get('content-encoding', ''):455- try:456- self._content = decode_gzip(self._content)457- except zlib.error:458- pass458+ # # Decode unicode content.459+ # if settings.decode_unicode:460+ # self._content = get_unicode_from_response(self)461 460- # Decode unicode content.461- if settings.decode_unicode:462- self._content = get_unicode_from_response(self)463-462 return self._content463 464 465 def raise_for_status(self):180 unmodified lines