Add stream decorator for HTTPMethodView

This commit is contained in:
38elements
2017-05-07 21:11:40 +09:00
parent 931397c7e1
commit 0b53c413a7
6 changed files with 67 additions and 3 deletions

View File

@@ -35,9 +35,11 @@ async def index(request):
Sanic allows you to get request data by stream, as below. When the request ends, `request.stream.get()` returns `None`.
```
```python
from sanic import Sanic
from sanic.views import CompositionView
from sanic.views import HTTPMethodView
from sanic.views import stream as stream_decorator
from sanic.blueprints import Blueprint
from sanic.response import stream, text
@@ -45,6 +47,19 @@ bp = Blueprint('blueprint_request_stream')
app = Sanic('request_stream', is_request_stream=True)
class SimpleView(HTTPMethodView):
@stream_decorator
async def post(self, request):
result = ''
while True:
body = await request.stream.get()
if body is None:
break
result += body.decode('utf-8')
return text(result)
@app.stream('/stream')
async def handler(request):
async def streaming(response):
@@ -83,6 +98,7 @@ async def post_handler(request):
return text(result)
app.blueprint(bp)
app.add_route(SimpleView.as_view(), '/method_view')
view = CompositionView()
view.add(['POST'], post_handler, stream=True)
app.add_route(view, '/composition_view')