Fixes #711!
authorKenneth Reitz <me@kennethreitz.com>
Mon, 20 Aug 2012 15:06:33 +0000 (01:06 +1000)
committerKenneth Reitz <me@kennethreitz.com>
Mon, 20 Aug 2012 15:06:33 +0000 (01:06 +1000)
requests/models.py

index 02713dd..8565760 100644 (file)
@@ -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,13 +833,31 @@ 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."""
         return self.raw.reason