1
Description: Fix json loads calls bytes vs unicode.
2
Origin: commit, revision id: dimitri.j.ledkov@intel.com-20150121185019-7lhyq3goy2dpgvid
3
Author: Dimitri John Ledkov <dimitri.j.ledkov@intel.com>
4
Bug: https://launchpad.net/bugs/1403524
5
Last-Update: 2015-01-21
6
X-Bzr-Revision-Id: dimitri.j.ledkov@intel.com-20150121185019-7lhyq3goy2dpgvid
8
=== modified file 'src/lazr/restfulclient/resource.py'
9
--- old/src/lazr/restfulclient/resource.py 2014-07-28 15:35:35 +0000
10
+++ new/src/lazr/restfulclient/resource.py 2015-01-21 18:50:19 +0000
12
representation = self._root._browser.get(self._wadl_resource)
13
if isinstance(representation, binary_type):
14
representation = representation.decode('utf-8')
15
- representation = loads(text_type(representation))
16
+ representation = loads(representation)
18
# In rare cases, the resource type served by the
19
# server conflicts with the type the client thought
22
url = str(self._root_uri.append(url))
23
document = self._browser.get(url)
24
+ if isinstance(document, binary_type):
25
+ document = document.decode('utf-8')
27
- representation = loads(text_type(document))
28
+ representation = loads(document)
30
raise ValueError("%s doesn't serve a JSON document." % url)
31
type_link = representation.get("resource_type_link")
33
# The operation returned a document with nothing
35
if content_type == self.JSON_MEDIA_TYPE:
36
- return loads(text_type(content))
37
+ if isinstance(content, binary_type):
38
+ content = content.decode('utf-8')
39
+ return loads(content)
40
# We don't know how to process the content.
43
# The operation returned a representation of some
44
# resource. Instantiate a Resource object for it.
45
- document = loads(text_type(content))
46
+ if isinstance(content, binary_type):
47
+ content = content.decode('utf-8')
49
+ document = loads(content)
51
# The operation returned a null value.
54
if response.status == 209 and content_type == self.JSON_MEDIA_TYPE:
55
# The server sent back a new representation of the object.
56
# Use it in preference to the existing representation.
57
- new_representation = loads(text_type(content))
58
+ if isinstance(content, binary_type):
59
+ content = content.decode('utf-8')
60
+ new_representation = loads(content)
61
self._wadl_resource.representation = new_representation
62
self._wadl_resource.media_type = content_type
65
next_link = current_page.get('next_collection_link')
68
- current_page = loads(
69
- text_type(self._root._browser.get(URI(next_link))))
70
+ next_get = self._root._browser.get(URI(next_link))
71
+ if isinstance(next_get, binary_type):
72
+ next_get = next_get.decode('utf-8')
73
+ current_page = loads(next_get)
75
def __getitem__(self, key):
76
"""Look up a slice, or a subordinate resource by index.
79
# Iterate over pages until we have the correct number of entries.
80
while more_needed > 0 and page_url is not None:
81
- representation = loads(
82
- text_type(self._root._browser.get(page_url)))
83
+ page_get = self._root._browser.get(page_url)
84
+ if isinstance(page_get, binary_type):
85
+ page_get = page_get.decode('utf-8')
86
+ representation = loads(page_get)
87
current_page_entries = representation['entries']
88
entry_dicts += current_page_entries[:more_needed]
89
more_needed = desired_size - len(entry_dicts)
90
@@ -1020,8 +1033,10 @@
91
# retrieve a representation of the resource and see how
92
# the resource describes itself.
94
- representation = loads(
95
- text_type(self._root._browser.get(url)))
96
+ url_get = self._root._browser.get(url)
97
+ if isinstance(url_get, binary_type):
98
+ url_get = url_get.decode('utf-8')
99
+ representation = loads(url_get)
100
except HTTPError as error:
101
# There's no resource corresponding to the given ID.
102
if error.response.status == 404: