Merge lp:~cjwatson/lazr.restful/py3-resource-return-bytes into lp:lazr.restful

Proposed by Colin Watson
Status: Merged
Merged at revision: 287
Proposed branch: lp:~cjwatson/lazr.restful/py3-resource-return-bytes
Merge into: lp:lazr.restful
Diff against target: 107 lines (+13/-13)
2 files modified
src/lazr/restful/_resource.py (+12/-12)
src/lazr/restful/docs/webservice.rst (+1/-1)
To merge this branch: bzr merge lp:~cjwatson/lazr.restful/py3-resource-return-bytes
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+396617@code.launchpad.net

Commit message

Return bytes from HTTPResource.__call__ implementations.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/restful/_resource.py'
2--- src/lazr/restful/_resource.py 2020-09-24 10:01:25 +0000
3+++ src/lazr/restful/_resource.py 2021-01-20 23:25:25 +0000
4@@ -911,7 +911,7 @@
5
6 def __call__(self):
7 """Handle a GET, PUT, or PATCH request."""
8- result = ""
9+ result = b""
10 method = self.getRequestMethod()
11 try:
12 if method == "GET":
13@@ -1251,7 +1251,7 @@
14 self.request.response.setStatus(209)
15 media_type = self.getPreferredSupportedContentType()
16 self.request.response.setHeader('Content-type', media_type)
17- return self._representation(media_type)
18+ return self._representation(media_type).encode('UTF-8')
19 else:
20 # The object moved. Serve a redirect to its new location.
21 # This might not necessarily be the location of the entry!
22@@ -1288,10 +1288,10 @@
23 media_type = self.handleConditionalGET()
24 if media_type is None:
25 # The conditional GET succeeded. Serve nothing.
26- return ""
27+ return b""
28 else:
29 self.request.response.setHeader('Content-Type', media_type)
30- return self._representation(media_type)
31+ return self._representation(media_type).encode('UTF-8')
32
33 def do_PUT(self, media_type, representation):
34 """Overwrite the field's existing value with a new value."""
35@@ -1328,7 +1328,7 @@
36 elif media_type == self.XHTML_TYPE:
37 name, value = self.unmarshallFieldToHTML(
38 self.context.name, self.context.field)
39- return encode_value(value)
40+ return decode_value(value)
41 else:
42 raise AssertionError(
43 "No representation implementation for media type %s"
44@@ -1564,10 +1564,10 @@
45 media_type = self.handleConditionalGET()
46 if media_type is None:
47 # The conditional GET succeeded. Serve nothing.
48- return ""
49+ return b""
50 else:
51 self.request.response.setHeader('Content-Type', media_type)
52- return self._representation(media_type)
53+ return self._representation(media_type).encode('UTF-8')
54
55 def do_PUT(self, media_type, representation):
56 """Modify the entry's state to match the given representation.
57@@ -1708,7 +1708,7 @@
58 """Return a representation of this entry, of the given media type."""
59
60 if media_type in [self.WADL_TYPE, self.DEPRECATED_WADL_TYPE]:
61- return self.toWADL().encode("utf-8")
62+ return self.toWADL()
63 elif media_type in (self.JSON_TYPE, self.JSON_PLUS_XHTML_TYPE):
64 cache = self._representation_cache
65 if cache is None:
66@@ -1754,7 +1754,7 @@
67 representation = simplejson.dumps(json)
68 return representation
69 elif media_type == self.XHTML_TYPE:
70- return self.toXHTML().encode("utf-8")
71+ return self.toXHTML()
72 else:
73 raise AssertionError(
74 "No representation implementation for media type %s"
75@@ -1929,16 +1929,16 @@
76 self.setCachingHeaders()
77 if media_type is None:
78 # The conditional GET succeeded. Serve nothing.
79- return ""
80+ return b""
81 elif media_type in [self.WADL_TYPE, self.DEPRECATED_WADL_TYPE]:
82- result = self.toWADL().encode("utf-8")
83+ result = self.toWADL()
84 elif media_type == self.JSON_TYPE:
85 # Serve a JSON map containing links to all the top-level
86 # resources.
87 result = simplejson.dumps(self, cls=ResourceJSONEncoder)
88
89 self.request.response.setHeader('Content-Type', media_type)
90- return result
91+ return result.encode("utf-8")
92
93 def toWADL(self):
94 # Find all resource types.
95
96=== modified file 'src/lazr/restful/docs/webservice.rst'
97--- src/lazr/restful/docs/webservice.rst 2021-01-18 11:58:31 +0000
98+++ src/lazr/restful/docs/webservice.rst 2021-01-20 23:25:25 +0000
99@@ -1094,7 +1094,7 @@
100 >>> import simplejson
101 >>> import six
102 >>> response = app(request)
103- >>> representation = simplejson.loads(six.text_type(response))
104+ >>> representation = simplejson.loads(six.ensure_text(response))
105
106 >>> print(representation["authors_collection_link"])
107 http://api.cookbooks.dev/beta/authors

Subscribers

People subscribed via source and target branches