From d8dcd35316621df0691ab5fc3fe1d5f3d76f71bb Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Tue, 21 Aug 2012 01:06:33 +1000 Subject: [PATCH] Fixes #711! --- requests/models.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/requests/models.py b/requests/models.py index 02713ddd..8565760b 100644 --- a/requests/models.py +++ b/requests/models.py @@ -13,7 +13,7 @@ from datetime import datetime from io import BytesIO from .hooks import dispatch_hook, HOOKS -from .structures import CaseInsensitiveDict +from .structures import CaseInsensitiveDict, MultiDict from .status_codes import codes from .auth import HTTPBasicAuth, HTTPProxyAuth @@ -31,7 +31,7 @@ from .exceptions import ( from .utils import ( get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri, stream_decode_response_unicode, get_netrc_auth, get_environ_proxies, - to_key_val_list, DEFAULT_CA_BUNDLE_PATH) + to_key_val_list, DEFAULT_CA_BUNDLE_PATH, parse_header_links) from .compat import ( cookielib, urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes, StringIO, is_py2, chardet, json, builtin_str, numeric_types) @@ -833,12 +833,30 @@ class Response(object): @property def json(self): - """Returns the json-encoded content of a request, if any.""" + """Returns the json-encoded content of a response, if any.""" try: return json.loads(self.text or self.content) except ValueError: return None + @property + def links(self): + """Returns the parsed header links of the response, if any.""" + + header = self.headers['link'] + + # l = MultiDict() + l = {} + + if header: + links = parse_header_links(header) + + for link in links: + key = link.get('rel') or link.get('url') + l[key] = link + + return l + @property def reason(self): """The HTTP Reason for the response."""