-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexam_aio.py
More file actions
48 lines (37 loc) · 1.55 KB
/
Copy pathexam_aio.py
File metadata and controls
48 lines (37 loc) · 1.55 KB
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
48
import asyncio
from creart import it
from launart import Launart, Service
from launart.status import Phase
from graia.amnesia.builtins.http import request
from graia.amnesia.builtins.http.aiohttp import AiohttpClientService
# from graia.amnesia.builtins.http.httpx import HttpxClientService
# from graia.amnesia.builtins.http.niquests import NiquestsClientService
# from graia.amnesia.builtins.http.pyreqwest import PyReqwestClientService
from graia.amnesia.builtins.http.httptypes import HTTPStatusError, Request
manager = it(Launart)
manager.add_component(AiohttpClientService())
class MainService(Service):
id = "main"
@property
def required(self):
return set()
@property
def stages(self) -> set[Phase]:
return {"blocking"}
async def launch(self, manager: Launart) -> None:
async with self.stage("blocking"):
await asyncio.sleep(2)
resp = await request(Request("GET", "https://httpbin.org/get"))
print(resp.status_code)
print(await resp.aread())
print(await resp.ajson())
resp1 = await request(Request("GET", "https://httpbin.org/image"), stream=True)
async for chunk in resp1.aiter_bytes(1024):
print(f"chunk: {len(chunk)} bytes")
try:
async with await request(Request("GET", "https://httpbin.org/status/404")) as resp:
resp.raise_for_status()
except HTTPStatusError as e:
print(e)
manager.add_component(MainService())
manager.launch_blocking()