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 0b53c41
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
18 changes: 17 additions & 1 deletion docs/sanic/streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,31 @@ 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

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
26 changes: 25 additions & 1 deletion 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 All @@ -31,7 +42,7 @@ async def get(request):


@bp.stream('/bp_stream')
async def bp_handler(request):
async def bp_stream(request):
result = ''
while True:
body = await request.stream.get()
Expand All @@ -41,6 +52,11 @@ async def bp_handler(request):
return text(result)


@bp.get('/bp_get')
async def bp_get(request):
return text('OK')


def get_handler(request):
return text('OK')

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

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

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

0 comments on commit 0b53c41

Please sign in to comment.