-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
47 lines (32 loc) · 1.1 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import asyncio
import aiohttp
import requests
from detacache import DetaCache
app = DetaCache(projectKey='projectKey')
@app.cache(expire=20)
async def asyncgetJson(url: str):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()
@app.cache(expire=20)
async def asyncgetText(url: str):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
@app.cache(expire=10)
def syncgetJson(url: str):
return requests.get(url).json()
@app.cache(expire=10)
def syncgetText(url: str):
return requests.get(url).text
async def main():
asyncJsonData = await asyncgetJson('https://httpbin.org/json')
print(asyncJsonData)
syncJsonData = syncgetJson('https://httpbin.org/json')
print(syncJsonData)
asyncTextData = await asyncgetText('https://httpbin.org/html')
print(asyncTextData)
syncTextData = syncgetText('https://httpbin.org/html')
print(syncTextData)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())