Added json_content and etree_content, more docs and comments.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Luca De Vitis · committed by Luca De Vitis · parent 3bf6c63 · Browse files · 1 file·+62 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+62 added−21 removed
- other:+62 added−21 removed
Provenance
- Commit
d8c923c- Tree
b52f7a5- Author
Luca De Vitis <luca@monkeython.com>- Committer
Luca De Vitis <luca@monkeython.com>- Parent
3bf6c63
1 file changed+62 added−21 removed
modified:requests/hooks/response.py+62 added-21 removed
2 unmodified lines3 ~~~~~~~~~~~~~~~~~~~~~~4 5 This module provide a collection of response hooks.6 """7-from functools import wraps7 import zlib8+import bz29 from cgi import parse_header10 11+try:12+ import json13+except ImportError:14+ try:15+ import simplejson as json16+ except ImportError:17+ json = False18+try:19+ from lxml import etree20+except ImportError:21+ try:22+ import xml.etree.cElementTree as etree23+ except ImportError:24+ try:25+ import xml.etree.ElementTree as etree26+ except ImportError:27+ try:28+ import cElementTree as etree29+ except ImportError:30+ try:31+ import elementtree.ElementTree as etree32+ except ImportError:33+ etree = False34+35 #: Dictionary of content decoders.12-decoders = {36+content_decoders = {37 # No decoding applied.14- 'identity': lambda r: r.content,38+ 'identity': lambda content: content,39 # Decode Response file object compressed with deflate.16- 'deflate': lambda r: zlib.decompress(r.content),40+ 'deflate': lambda content: zlib.decompress(content),41 # Decode Response file object compressed with gzip.18- 'gzip': lambda r: zlib.decompress(r.content, 16+zlib.MAX_WBITS),42+ 'gzip': lambda content: zlib.decompress(content, 16+zlib.MAX_WBITS),43+ # Decode Response file object compressed with bz2.44+ # Not a standard Content-Encoding value, but..45+ 'bzip2': lambda content: bz2.decompress(content),46 }47 48 # Decode Response file object compressed with compress.22-decoders['compress'] = decoders['deflate']49+content_decoders['compress'] = content_decoders['deflate']50 24-try:25- import bz226-except ImportError:27- pass28-else:29- # Decode Response file object compressed with bz2.30- decoders['bzip2'] = lambda r: bz2.decompress(r.content)31-32-def unicode_response(r):33- """Encode response file object in unicode."""34- content_type, params = parse_header(r.headers.get('content-type'))51+def decode_unicode(r):52+ """Encode a :py:class:`requests.models.Response` file object in unicode."""53+ content_type, params = parse_header(r.headers['content-type'])54 charset = params.get('charset', '').strip("'\"")55 r._content = unicode(r.content, charset) if charset else unicode(r.content)56 return r57 39-def decode_response(r):40- """Decode compressed response content using Contetn-Encoding header."""41- encoding = r.headers.get('content-encoding')42- r._content = decoders.get(encoding)(r)58+def decode_encoding(r):59+ """60+ Decode a :py:class:`requests.models.Response` content using61+ Contetn-Encoding header.62+ """63+ # Apply decoding only if the header is set.64+ encoding = r.headers['content-encoding']65+ if encoding:66+ r._content = content_decoders[encoding](r.content)67 return r68 69+if json:70+ def json_content(r):71+ """72+ Turns :py:class:`requests.models.Response` content into a dumped73+ JSON structure.74+ """75+ r._content = json.dumps(r.content)76+ return r77+78+if etree:79+ def etree_content(r):80+ """81+ Turns :py:class:`requests.models.Response` content into an82+ ElementTree structure.83+ """84+ r._content = etree.fromstring(r.content)85+ return r