Skip to content

Commit

Permalink
Add stream decorator for HTTPMethodView
Browse files Browse the repository at this point in the history
  • Loading branch information
38elements committed May 7, 2017
1 parent 931397c commit f379275
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/sanic/streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,28 @@ Sanic allows you to get request data by stream, as below. When the request ends,
```
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
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):
Expand Down Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion examples/request_stream/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
for i in range(1, 250000):
data += str(i)

r = requests.post('http://127.0.0.1:8000/bp_stream', data=data)
r = requests.post('http://127.0.0.1:8000/method_view', data=data)
print(r.text)
16 changes: 16 additions & 0 deletions examples/request_stream/server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
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

bp = Blueprint('blueprint_request_stream')
app = Sanic('request_stream')


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):
Expand Down Expand Up @@ -45,6 +60,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')
Expand Down
3 changes: 3 additions & 0 deletions sanic/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,7 @@ def is_stream_handler(self, request):
:return: bool
"""
handler = self.get(request)[0]
if (hasattr(handler, 'view_class') and
hasattr(handler.view_class, request.method.lower())):
handler = getattr(handler.view_class, request.method.lower())
return hasattr(handler, 'is_stream') and handler.is_stream
5 changes: 5 additions & 0 deletions sanic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def view(*args, **kwargs):
return view


def stream(func):
func.is_stream = True
return func


class CompositionView:
"""Simple method-function mapped view for the sanic.
You can add handler functions to methods (get, post, put, patch, delete)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_request_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sanic.blueprints import Blueprint
from sanic.views import CompositionView
from sanic.views import HTTPMethodView
from sanic.views import stream as stream_decorator
from sanic.response import stream, text

bp = Blueprint('test_blueprint_request_stream')
Expand All @@ -13,6 +14,16 @@ class SimpleView(HTTPMethodView):
def get(self, request):
return text('OK')

@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):
Expand Down Expand Up @@ -73,6 +84,10 @@ def test_request_stream():
assert response.status == 200
assert response.text == 'OK'

request, response = app.test_client.post('/method_view', data=data)
assert response.status == 200
assert response.text == data

request, response = app.test_client.get('/composition_view')
assert response.status == 200
assert response.text == 'OK'
Expand Down

0 comments on commit f379275

Please sign in to comment.