Updated aiohttp example.

This commit is contained in:
Shawn Niederriter 2017-04-06 13:33:29 -04:00 committed by GitHub
parent e9eca25792
commit 833b14e353

View File

@ -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)