From 833b14e3531e2c7a61634652607621ea24b918ad Mon Sep 17 00:00:00 2001 From: Shawn Niederriter Date: Thu, 6 Apr 2017 13:33:29 -0400 Subject: [PATCH 1/4] Updated aiohttp example. --- examples/aiohttp_example.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/examples/aiohttp_example.py b/examples/aiohttp_example.py index a8fb20f0..0762f5d4 100644 --- a/examples/aiohttp_example.py +++ b/examples/aiohttp_example.py @@ -1,28 +1,26 @@ from sanic import Sanic -from sanic.response import json +from sanic import response import aiohttp -app = Sanic(__name__) +app = Sanic() async def fetch(session, url): """ Use session object to perform 'get' request on url """ - async with session.get(url) as response: - return await response.json() + async with session.get(url) as result: + return await result.text() -@app.route("/") -async def test(request): - """ - Download and serve example JSON - """ +@app.route('/') +async def handle_request(request): url = "https://api.github.com/repos/channelcat/sanic" - + async with aiohttp.ClientSession() as session: - response = await fetch(session, url) - return json(response) + result = await fetch(session, url) + return response.json(result) -app.run(host="0.0.0.0", port=8000, workers=2) +if __name__ == '__main__': + app.run(host="0.0.0.0", port=8000, workers=2) From 46ac79f4dcd62fcbcb39a689e48f9f3ef947d6d9 Mon Sep 17 00:00:00 2001 From: Shawn Niederriter Date: Thu, 6 Apr 2017 14:39:54 -0400 Subject: [PATCH 2/4] Update run_async demo --- examples/run_async.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/run_async.py b/examples/run_async.py index d514c7d0..1fb232c3 100644 --- a/examples/run_async.py +++ b/examples/run_async.py @@ -1,5 +1,5 @@ from sanic import Sanic -from sanic.response import json +from sanic import response from multiprocessing import Event from signal import signal, SIGINT import asyncio @@ -9,10 +9,10 @@ app = Sanic(__name__) @app.route("/") async def test(request): - return json({"answer": "42"}) + return response.json({"answer": "42"}) asyncio.set_event_loop(uvloop.new_event_loop()) -server = app.create_server(host="0.0.0.0", port=8001) +server = app.create_server(host="0.0.0.0", port=8080) loop = asyncio.get_event_loop() task = asyncio.ensure_future(server) signal(SIGINT, lambda s, f: loop.stop()) From c30437448bf5e35924bec7401cba5634720047ae Mon Sep 17 00:00:00 2001 From: Shawn Niederriter Date: Thu, 6 Apr 2017 19:42:05 +0000 Subject: [PATCH 3/4] Updated aiohttp & run_async examples, added redirect --- examples/aiohttp_example.py | 2 +- examples/redirect_example.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 examples/redirect_example.py diff --git a/examples/aiohttp_example.py b/examples/aiohttp_example.py index 0762f5d4..f8043d5c 100644 --- a/examples/aiohttp_example.py +++ b/examples/aiohttp_example.py @@ -3,7 +3,7 @@ from sanic import response import aiohttp -app = Sanic() +app = Sanic(__name__) async def fetch(session, url): """ diff --git a/examples/redirect_example.py b/examples/redirect_example.py new file mode 100644 index 00000000..c062a76f --- /dev/null +++ b/examples/redirect_example.py @@ -0,0 +1,17 @@ +from sanic import Sanic +from sanic import response + +app = Sanic(__name__) + + +@app.route('/') +def handle_request(request): + return response.redirect('/redirect') + +@app.route('/redirect') +async def test(request): + return response.json({"Redirected": True}) + + +if __name__ == '__main__': + app.run(host="0.0.0.0", port=8080) \ No newline at end of file From 9d3bb4a37ad2edcededc9c79108d1c5892de3f9a Mon Sep 17 00:00:00 2001 From: Shawn Niederriter Date: Thu, 6 Apr 2017 19:47:25 +0000 Subject: [PATCH 4/4] Updated examples in-line with response docs --- examples/aiohttp_example.py | 2 +- examples/redirect_example.py | 2 +- examples/run_async.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/aiohttp_example.py b/examples/aiohttp_example.py index f8043d5c..853ea959 100644 --- a/examples/aiohttp_example.py +++ b/examples/aiohttp_example.py @@ -10,7 +10,7 @@ async def fetch(session, url): Use session object to perform 'get' request on url """ async with session.get(url) as result: - return await result.text() + return await result.json() @app.route('/') diff --git a/examples/redirect_example.py b/examples/redirect_example.py index c062a76f..acc7d1ff 100644 --- a/examples/redirect_example.py +++ b/examples/redirect_example.py @@ -14,4 +14,4 @@ async def test(request): if __name__ == '__main__': - app.run(host="0.0.0.0", port=8080) \ No newline at end of file + app.run(host="0.0.0.0", port=8000) \ No newline at end of file diff --git a/examples/run_async.py b/examples/run_async.py index 1fb232c3..b6f6d76d 100644 --- a/examples/run_async.py +++ b/examples/run_async.py @@ -12,7 +12,7 @@ async def test(request): return response.json({"answer": "42"}) asyncio.set_event_loop(uvloop.new_event_loop()) -server = app.create_server(host="0.0.0.0", port=8080) +server = app.create_server(host="0.0.0.0", port=8000) loop = asyncio.get_event_loop() task = asyncio.ensure_future(server) signal(SIGINT, lambda s, f: loop.stop())