Add stream support for bp.add_route() (#1482)
* Fix #1454 * Update doc * Fix F632 in response.py
This commit is contained in:
parent
bc7d0f0da5
commit
52bdd1d5a2
|
@ -42,7 +42,7 @@ async def handler(request):
|
||||||
|
|
||||||
|
|
||||||
@bp.put('/bp_stream', stream=True)
|
@bp.put('/bp_stream', stream=True)
|
||||||
async def bp_handler(request):
|
async def bp_put_handler(request):
|
||||||
result = ''
|
result = ''
|
||||||
while True:
|
while True:
|
||||||
body = await request.stream.read()
|
body = await request.stream.read()
|
||||||
|
@ -52,6 +52,19 @@ async def bp_handler(request):
|
||||||
return text(result)
|
return text(result)
|
||||||
|
|
||||||
|
|
||||||
|
# You can also use `bp.add_route()` with stream argument
|
||||||
|
async def bp_post_handler(request):
|
||||||
|
result = ''
|
||||||
|
while True:
|
||||||
|
body = await request.stream.read()
|
||||||
|
if body is None:
|
||||||
|
break
|
||||||
|
result += body.decode('utf-8').replace('1', 'A')
|
||||||
|
return text(result)
|
||||||
|
|
||||||
|
bp.add_route(bp_post_handler, '/bp_stream', methods=['POST'], stream=True)
|
||||||
|
|
||||||
|
|
||||||
async def post_handler(request):
|
async def post_handler(request):
|
||||||
result = ''
|
result = ''
|
||||||
while True:
|
while True:
|
||||||
|
|
|
@ -212,6 +212,7 @@ class Blueprint:
|
||||||
strict_slashes=None,
|
strict_slashes=None,
|
||||||
version=None,
|
version=None,
|
||||||
name=None,
|
name=None,
|
||||||
|
stream=False,
|
||||||
):
|
):
|
||||||
"""Create a blueprint route from a function.
|
"""Create a blueprint route from a function.
|
||||||
|
|
||||||
|
@ -224,6 +225,7 @@ class Blueprint:
|
||||||
training */*
|
training */*
|
||||||
:param version: Blueprint Version
|
:param version: Blueprint Version
|
||||||
:param name: user defined route name for url_for
|
:param name: user defined route name for url_for
|
||||||
|
:param stream: boolean specifying if the handler is a stream handler
|
||||||
:return: function or class instance
|
:return: function or class instance
|
||||||
"""
|
"""
|
||||||
# Handle HTTPMethodView differently
|
# Handle HTTPMethodView differently
|
||||||
|
@ -246,6 +248,7 @@ class Blueprint:
|
||||||
methods=methods,
|
methods=methods,
|
||||||
host=host,
|
host=host,
|
||||||
strict_slashes=strict_slashes,
|
strict_slashes=strict_slashes,
|
||||||
|
stream=stream,
|
||||||
version=version,
|
version=version,
|
||||||
name=name,
|
name=name,
|
||||||
)(handler)
|
)(handler)
|
||||||
|
|
|
@ -117,7 +117,7 @@ class StreamingHTTPResponse(BaseHTTPResponse):
|
||||||
|
|
||||||
headers = self._parse_headers()
|
headers = self._parse_headers()
|
||||||
|
|
||||||
if self.status is 200:
|
if self.status == 200:
|
||||||
status = b"OK"
|
status = b"OK"
|
||||||
else:
|
else:
|
||||||
status = STATUS_CODES.get(self.status)
|
status = STATUS_CODES.get(self.status)
|
||||||
|
@ -176,7 +176,7 @@ class HTTPResponse(BaseHTTPResponse):
|
||||||
|
|
||||||
headers = self._parse_headers()
|
headers = self._parse_headers()
|
||||||
|
|
||||||
if self.status is 200:
|
if self.status == 200:
|
||||||
status = b"OK"
|
status = b"OK"
|
||||||
else:
|
else:
|
||||||
status = STATUS_CODES.get(self.status, b"UNKNOWN RESPONSE")
|
status = STATUS_CODES.get(self.status, b"UNKNOWN RESPONSE")
|
||||||
|
|
|
@ -270,6 +270,18 @@ def test_request_stream_blueprint(app):
|
||||||
|
|
||||||
return stream(streaming)
|
return stream(streaming)
|
||||||
|
|
||||||
|
async def post_add_route(request):
|
||||||
|
assert isinstance(request.stream, StreamBuffer)
|
||||||
|
|
||||||
|
async def streaming(response):
|
||||||
|
while True:
|
||||||
|
body = await request.stream.read()
|
||||||
|
if body is None:
|
||||||
|
break
|
||||||
|
await response.write(body.decode("utf-8"))
|
||||||
|
return stream(streaming)
|
||||||
|
|
||||||
|
bp.add_route(post_add_route, '/post/add_route', methods=['POST'], stream=True)
|
||||||
app.blueprint(bp)
|
app.blueprint(bp)
|
||||||
|
|
||||||
assert app.is_request_stream is True
|
assert app.is_request_stream is True
|
||||||
|
@ -314,6 +326,10 @@ def test_request_stream_blueprint(app):
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
assert response.text == data
|
assert response.text == data
|
||||||
|
|
||||||
|
request, response = app.test_client.post("/post/add_route", data=data)
|
||||||
|
assert response.status == 200
|
||||||
|
assert response.text == data
|
||||||
|
|
||||||
|
|
||||||
def test_request_stream_composition_view(app):
|
def test_request_stream_composition_view(app):
|
||||||
"""for self.is_request_stream = True"""
|
"""for self.is_request_stream = True"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user