Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stream support for bp.add_route() #1482

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion docs/sanic/streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def handler(request):


@bp.put('/bp_stream', stream=True)
async def bp_handler(request):
async def bp_put_handler(request):
result = ''
while True:
body = await request.stream.read()
Expand All @@ -52,6 +52,19 @@ async def bp_handler(request):
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):
result = ''
while True:
Expand Down
3 changes: 3 additions & 0 deletions sanic/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def add_route(
strict_slashes=None,
version=None,
name=None,
stream=False,
):
"""Create a blueprint route from a function.

Expand All @@ -224,6 +225,7 @@ def add_route(
training */*
:param version: Blueprint Version
: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
"""
# Handle HTTPMethodView differently
Expand All @@ -246,6 +248,7 @@ def add_route(
methods=methods,
host=host,
strict_slashes=strict_slashes,
stream=stream,
version=version,
name=name,
)(handler)
Expand Down
4 changes: 2 additions & 2 deletions sanic/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_headers(

headers = self._parse_headers()

if self.status is 200:
if self.status == 200:
status = b"OK"
else:
status = STATUS_CODES.get(self.status)
Expand Down Expand Up @@ -176,7 +176,7 @@ def output(self, version="1.1", keep_alive=False, keep_alive_timeout=None):

headers = self._parse_headers()

if self.status is 200:
if self.status == 200:
status = b"OK"
else:
status = STATUS_CODES.get(self.status, b"UNKNOWN RESPONSE")
Expand Down
16 changes: 16 additions & 0 deletions tests/test_request_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,18 @@ async def streaming(response):

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)

assert app.is_request_stream is True
Expand Down Expand Up @@ -314,6 +326,10 @@ async def streaming(response):
assert response.status == 200
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):
"""for self.is_request_stream = True"""
Expand Down