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
=== modified file 'src/lazr/restful/_resource.py'
--- src/lazr/restful/_resource.py 2020-09-24 10:01:25 +0000
+++ src/lazr/restful/_resource.py 2021-01-20 23:25:25 +0000
@@ -911,7 +911,7 @@
911911
912 def __call__(self):912 def __call__(self):
913 """Handle a GET, PUT, or PATCH request."""913 """Handle a GET, PUT, or PATCH request."""
914 result = ""914 result = b""
915 method = self.getRequestMethod()915 method = self.getRequestMethod()
916 try:916 try:
917 if method == "GET":917 if method == "GET":
@@ -1251,7 +1251,7 @@
1251 self.request.response.setStatus(209)1251 self.request.response.setStatus(209)
1252 media_type = self.getPreferredSupportedContentType()1252 media_type = self.getPreferredSupportedContentType()
1253 self.request.response.setHeader('Content-type', media_type)1253 self.request.response.setHeader('Content-type', media_type)
1254 return self._representation(media_type)1254 return self._representation(media_type).encode('UTF-8')
1255 else:1255 else:
1256 # The object moved. Serve a redirect to its new location.1256 # The object moved. Serve a redirect to its new location.
1257 # This might not necessarily be the location of the entry!1257 # This might not necessarily be the location of the entry!
@@ -1288,10 +1288,10 @@
1288 media_type = self.handleConditionalGET()1288 media_type = self.handleConditionalGET()
1289 if media_type is None:1289 if media_type is None:
1290 # The conditional GET succeeded. Serve nothing.1290 # The conditional GET succeeded. Serve nothing.
1291 return ""1291 return b""
1292 else:1292 else:
1293 self.request.response.setHeader('Content-Type', media_type)1293 self.request.response.setHeader('Content-Type', media_type)
1294 return self._representation(media_type)1294 return self._representation(media_type).encode('UTF-8')
12951295
1296 def do_PUT(self, media_type, representation):1296 def do_PUT(self, media_type, representation):
1297 """Overwrite the field's existing value with a new value."""1297 """Overwrite the field's existing value with a new value."""
@@ -1328,7 +1328,7 @@
1328 elif media_type == self.XHTML_TYPE:1328 elif media_type == self.XHTML_TYPE:
1329 name, value = self.unmarshallFieldToHTML(1329 name, value = self.unmarshallFieldToHTML(
1330 self.context.name, self.context.field)1330 self.context.name, self.context.field)
1331 return encode_value(value)1331 return decode_value(value)
1332 else:1332 else:
1333 raise AssertionError(1333 raise AssertionError(
1334 "No representation implementation for media type %s"1334 "No representation implementation for media type %s"
@@ -1564,10 +1564,10 @@
1564 media_type = self.handleConditionalGET()1564 media_type = self.handleConditionalGET()
1565 if media_type is None:1565 if media_type is None:
1566 # The conditional GET succeeded. Serve nothing.1566 # The conditional GET succeeded. Serve nothing.
1567 return ""1567 return b""
1568 else:1568 else:
1569 self.request.response.setHeader('Content-Type', media_type)1569 self.request.response.setHeader('Content-Type', media_type)
1570 return self._representation(media_type)1570 return self._representation(media_type).encode('UTF-8')
15711571
1572 def do_PUT(self, media_type, representation):1572 def do_PUT(self, media_type, representation):
1573 """Modify the entry's state to match the given representation.1573 """Modify the entry's state to match the given representation.
@@ -1708,7 +1708,7 @@
1708 """Return a representation of this entry, of the given media type."""1708 """Return a representation of this entry, of the given media type."""
17091709
1710 if media_type in [self.WADL_TYPE, self.DEPRECATED_WADL_TYPE]:1710 if media_type in [self.WADL_TYPE, self.DEPRECATED_WADL_TYPE]:
1711 return self.toWADL().encode("utf-8")1711 return self.toWADL()
1712 elif media_type in (self.JSON_TYPE, self.JSON_PLUS_XHTML_TYPE):1712 elif media_type in (self.JSON_TYPE, self.JSON_PLUS_XHTML_TYPE):
1713 cache = self._representation_cache1713 cache = self._representation_cache
1714 if cache is None:1714 if cache is None:
@@ -1754,7 +1754,7 @@
1754 representation = simplejson.dumps(json)1754 representation = simplejson.dumps(json)
1755 return representation1755 return representation
1756 elif media_type == self.XHTML_TYPE:1756 elif media_type == self.XHTML_TYPE:
1757 return self.toXHTML().encode("utf-8")1757 return self.toXHTML()
1758 else:1758 else:
1759 raise AssertionError(1759 raise AssertionError(
1760 "No representation implementation for media type %s"1760 "No representation implementation for media type %s"
@@ -1929,16 +1929,16 @@
1929 self.setCachingHeaders()1929 self.setCachingHeaders()
1930 if media_type is None:1930 if media_type is None:
1931 # The conditional GET succeeded. Serve nothing.1931 # The conditional GET succeeded. Serve nothing.
1932 return ""1932 return b""
1933 elif media_type in [self.WADL_TYPE, self.DEPRECATED_WADL_TYPE]:1933 elif media_type in [self.WADL_TYPE, self.DEPRECATED_WADL_TYPE]:
1934 result = self.toWADL().encode("utf-8")1934 result = self.toWADL()
1935 elif media_type == self.JSON_TYPE:1935 elif media_type == self.JSON_TYPE:
1936 # Serve a JSON map containing links to all the top-level1936 # Serve a JSON map containing links to all the top-level
1937 # resources.1937 # resources.
1938 result = simplejson.dumps(self, cls=ResourceJSONEncoder)1938 result = simplejson.dumps(self, cls=ResourceJSONEncoder)
19391939
1940 self.request.response.setHeader('Content-Type', media_type)1940 self.request.response.setHeader('Content-Type', media_type)
1941 return result1941 return result.encode("utf-8")
19421942
1943 def toWADL(self):1943 def toWADL(self):
1944 # Find all resource types.1944 # Find all resource types.
19451945
=== modified file 'src/lazr/restful/docs/webservice.rst'
--- src/lazr/restful/docs/webservice.rst 2021-01-18 11:58:31 +0000
+++ src/lazr/restful/docs/webservice.rst 2021-01-20 23:25:25 +0000
@@ -1094,7 +1094,7 @@
1094 >>> import simplejson1094 >>> import simplejson
1095 >>> import six1095 >>> import six
1096 >>> response = app(request)1096 >>> response = app(request)
1097 >>> representation = simplejson.loads(six.text_type(response))1097 >>> representation = simplejson.loads(six.ensure_text(response))
10981098
1099 >>> print(representation["authors_collection_link"])1099 >>> print(representation["authors_collection_link"])
1100 http://api.cookbooks.dev/beta/authors1100 http://api.cookbooks.dev/beta/authors

Subscribers

People subscribed via source and target branches