Updated aiohttp example.
This commit is contained in:
parent
e9eca25792
commit
833b14e353
@ -1,28 +1,26 @@
|
|||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.response import json
|
from sanic import response
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic()
|
||||||
|
|
||||||
async def fetch(session, url):
|
async def fetch(session, url):
|
||||||
"""
|
"""
|
||||||
Use session object to perform 'get' request on url
|
Use session object to perform 'get' request on url
|
||||||
"""
|
"""
|
||||||
async with session.get(url) as response:
|
async with session.get(url) as result:
|
||||||
return await response.json()
|
return await result.text()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route('/')
|
||||||
async def test(request):
|
async def handle_request(request):
|
||||||
"""
|
|
||||||
Download and serve example JSON
|
|
||||||
"""
|
|
||||||
url = "https://api.github.com/repos/channelcat/sanic"
|
url = "https://api.github.com/repos/channelcat/sanic"
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
response = await fetch(session, url)
|
result = await fetch(session, url)
|
||||||
return json(response)
|
return response.json(result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
app.run(host="0.0.0.0", port=8000, workers=2)
|
app.run(host="0.0.0.0", port=8000, workers=2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user