Skip to content
Myr

* 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

5 files+84 added−71 removed

added:requests/hooks/__init__.py+61 added
1# -*- coding: utf-8 -*-23"""4requests.hooks5~~~~~~~~~~~~~~67This module provides the capabilities for the Requests hooks system.89Available hooks:1011``args``:12 A dictionary of the arguments being sent to Request().1314``pre_request``:15 The Request object, directly before being sent.1617``post_request``:18 The Request object, directly after being sent.1920``response``:21 The response generated from a Request.2223"""2425import warnings26from collections import Iterable27from .. import config28from response import unicode_response, decode_response2930def setup_hooks(hooks):31 """Setup hooks as a dictionary. Each value is a set of hooks."""3233 for key, values in hooks.items():34 hook_list = values if isinstance(values, Iterable) else [values]35 hooks[key] = set(hook_list) 3637 # 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 hooks4344def dispatch_hooks(hooks, hook_data):45 """Dispatches multiple hooks on a given piece of data.4647 :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_data
modified:requests/api.py+8 added-8 removed
12 unmodified lines
1314import config15from .models import Request, Response, AuthObject16from .status_codes import codes17from .hooks import dispatch_hooks17from hooks import setup_hooks, dispatch_hooks18from .utils import cookiejar_from_dict1920from urlparse import urlparse21
20 unmodified lines
42 """4344 method = str(method).upper()4546 if cookies is None:47 cookies = {}46 cookies = cookiejar_from_dict(cookies or dict())4749 cookies = cookiejar_from_dict(cookies)48 hooks = setup_hooks(hooks or dict())4950 args = dict(51 method = method,52 url = url,
6 unmodified lines
59 timeout = timeout or config.settings.timeout,60 allow_redirects = allow_redirects,61 proxies = proxies or config.settings.proxies,62 )63 6465 # Arguments manipulation hook.66 args = dispatch_hooks('args', hooks, args)66 args = dispatch_hooks(hooks.get('args', []), args)6768 r = Request(**args)6970 # Pre-request hook.71 r = dispatch_hooks('pre_request', hooks, r)71 r = dispatch_hooks(hooks.get('pre_request', []), r)7273 # Send the HTTP Request.74 r.send()7576 # Post-request hook.77 r = dispatch_hooks('post_request', hooks, r)77 r = dispatch_hooks(hooks.get('post_request', []), hooks, r)7879 # Response manipulation hook.80 r.response = dispatch_hooks('response', hooks, r.response)80 r.response = dispatch_hooks(hooks.get('response', []), r.response)8182 return r.response8384
62 unmodified lines
modified:requests/config.py+3 added-1 removed
60 unmodified lines
61settings.proxies = None62settings.verbose = None63settings.timeout = None64settings.max_redirects = 3065settings.decode_unicode = True65# settings.decode_unicode = True66settings.unicode_response = True67settings.decode_response = True6869#: Use socket.setdefaulttimeout() as fallback?70settings.timeout_fallback = True
deleted:requests/hooks.py-48 removed
1# -*- coding: utf-8 -*-23"""4requests.hooks5~~~~~~~~~~~~~~67This module provides the capabilities for the Requests hooks system.89Available hooks:1011``args``:12 A dictionary of the arguments being sent to Request().1314``pre_request``:15 The Request object, directly before being sent.1617``post_request``:18 The Request object, directly after being sent.1920``response``:21 The response generated from a Request.2223"""2425import warnings26from collections import Iterable2728def dispatch_hooks(key, hooks, hook_data):29 """Dispatches multiple hooks on a given piece of data.3031 :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 lines
443 """Content of the response, in bytes or unicode444 (if available).445 """446447 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()450450 # 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 # pass457453 # 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)461460 # Decode unicode content.461 if settings.decode_unicode:462 self._content = get_unicode_from_response(self)463462 return self._content463464465 def raise_for_status(self):
180 unmodified lines