11from contextlib import asynccontextmanager
2- from typing import Union , Dict , Any
3-
4- from aiohttp import ClientSession , ClientWebSocketResponse
2+ from typing import Union , Dict , Any , Optional
3+ import json
4+ from aiohttp import ClientSession , ClientWebSocketResponse , ClientResponse , WSMessage
55from yarl import URL
66
7- from ..main .network import NetworkClient , HTTP_METHODS , NetworkResponse
7+ from ..main .network import NetworkClient , HTTP_METHODS , NetworkResponse , NetworkConnection
8+
9+
10+ class AiohttpResponse (NetworkResponse ):
11+ response : ClientResponse
12+
13+ def __init__ (self , response : ClientResponse ):
14+ self .response = response
15+
16+ @property
17+ def url (self ) -> URL :
18+ return self .response .url
19+
20+ @property
21+ def status (self ) -> int :
22+ return self .response .status
23+
24+ def raise_for_status (self ):
25+ self .response .raise_for_status ()
26+
27+ async def read_json (self ) -> dict :
28+ return await self .response .json ()
29+
30+ async def read (self ) -> bytes :
31+ return await self .response .read ()
32+
33+ async def cookies (self ) -> Dict [str , str ]:
34+ return {k : str (v ) for k , v in self .response .cookies .items ()}
35+
36+ async def headers (self ) -> Dict [str , str ]:
37+ return {k : str (v ) for k , v in self .response .headers .items ()}
38+
39+ async def close (self ):
40+ self .response .close ()
41+
42+
43+ class AiohttpWSConnection (NetworkConnection ):
44+
45+ async def accept (self ) -> None :
46+ pass
47+
48+ def status (self ) -> int :
49+ pass
50+
51+ def raise_for_code (self ):
52+ pass
53+
54+ response : ClientWebSocketResponse
55+
56+ def __init__ (self , response : ClientWebSocketResponse ):
57+ self .response = response
58+
59+ async def send (self , data : Union [bytes , str , dict ]) -> None :
60+ if isinstance (data , str ):
61+ return await self .response .send_str (data )
62+ if isinstance (data , bytes ):
63+ return await self .response .send_bytes (data )
64+ if isinstance (data , dict ):
65+ return await self .response .send_str (json .dumps (data ))
66+
67+ async def receive (self , timeout : Optional [float ] = None ) -> WSMessage :
68+ return await self .response .receive (timeout )
69+
70+ async def ping (self ) -> None :
71+ return await self .response .ping ()
72+
73+ async def pong (self ) -> None :
74+ return await self .response .pong ()
75+
76+ async def close (self , code : int = 1000 , message : bytes = b'' ) -> None :
77+ await self .response .close (code = code , message = message )
878
979
1080class AioHttpClient (NetworkClient ):
@@ -26,7 +96,7 @@ async def ensure_network(
2696 ** kwargs : Any
2797 ):
2898 resp : ClientWebSocketResponse = await self .session .ws_connect (url , timeout = timeout , ** kwargs ).__aenter__ ()
29- yield NetworkResponse ( get_connection = lambda : resp )
99+ yield AiohttpWSConnection ( resp )
30100
31101 @asynccontextmanager
32102 async def request (
@@ -40,10 +110,8 @@ async def request(
40110 async with self .session .request (
41111 method , url , ** {"headers" : headers , "data" : data , ** kwargs }
42112 ) as resp :
43- async def _read ():
44- return await resp .read ()
45113
46- yield NetworkResponse ( get_content = _read )
114+ yield AiohttpResponse ( resp )
47115
48116 @asynccontextmanager
49117 async def get (
@@ -55,15 +123,7 @@ async def get(
55123 async with self .session .get (
56124 url , ** {"headers" : headers , ** kwargs }
57125 ) as resp :
58- resp .raise_for_status ()
59-
60- async def _read ():
61- return await resp .read ()
62-
63- async def _json ():
64- return await resp .json ()
65-
66- yield NetworkResponse (get_content = _read , get_json = _json )
126+ yield AiohttpResponse (resp )
67127
68128 @asynccontextmanager
69129 async def post (
@@ -75,15 +135,7 @@ async def post(
75135 async with self .session .post (
76136 url , ** {"headers" : headers , "data" : data , ** kwargs }
77137 ) as resp :
78- resp .raise_for_status ()
79-
80- async def _read ():
81- return await resp .read ()
82-
83- async def _json ():
84- return await resp .json ()
85-
86- yield NetworkResponse (get_content = _read , get_json = _json )
138+ yield AiohttpResponse (resp )
87139
88140 @asynccontextmanager
89141 async def put (
0 commit comments