Skip to content

Commit 4d82226

Browse files
committed
Rename aiohttpclient to aiohttp
1 parent 0bcaf3b commit 4d82226

File tree

6 files changed

+508
-91
lines changed

6 files changed

+508
-91
lines changed

async_modules/aiohttp/aiohttpclient/__init__.py renamed to async_modules/aiohttp/aiohttp/__init__.py

Lines changed: 15 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import json as _json
3-
from .aiohttpclient_ws import (
3+
from .aiohttp_ws import (
44
_WSRequestContextManager,
55
ClientWebSocketResponse,
66
WebSocketClient,
@@ -12,7 +12,7 @@ def __init__(self, reader):
1212
self.content = reader
1313

1414
def read(self, sz=-1):
15-
return (yield from self.content.read(sz))
15+
return self.content.read(sz)
1616

1717
def text(self, sz=-1):
1818
return self.read(sz=sz)
@@ -29,22 +29,20 @@ def __init__(self, reader):
2929
self.content = reader
3030
self.chunk_size = 0
3131

32-
def read(self, sz=4 * 1024 * 1024):
32+
async def read(self, sz=4 * 1024 * 1024):
3333
if self.chunk_size == 0:
34-
l = yield from self.content.readline()
35-
# print("chunk line:", l)
34+
l = await self.content.readline()
3635
l = l.split(b";", 1)[0]
3736
self.chunk_size = int(l, 16)
38-
# print("chunk size:", self.chunk_size)
3937
if self.chunk_size == 0:
4038
# End of message
41-
sep = yield from self.content.read(2)
39+
sep = await self.content.read(2)
4240
assert sep == b"\r\n"
4341
return b""
44-
data = yield from self.content.read(min(sz, self.chunk_size))
42+
data = await self.content.read(min(sz, self.chunk_size))
4543
self.chunk_size -= len(data)
4644
if self.chunk_size == 0:
47-
sep = yield from self.content.read(2)
45+
sep = await self.content.read(2)
4846
assert sep == b"\r\n"
4947
return data
5048

@@ -115,6 +113,13 @@ async def _request(self, method, url, data=None, json=None, ssl=None):
115113
resp = ClientResponse(reader)
116114
resp.status = status
117115
resp.headers = headers
116+
try:
117+
resp.headers = {
118+
val.split(":", 1)[0]: val.split(":", 1)[-1].strip()
119+
for val in [hed.decode().strip() for hed in headers]
120+
}
121+
except Exception:
122+
pass
118123
self._reader = reader
119124
return resp
120125

@@ -174,9 +179,7 @@ async def request_raw(self, method, url, data=None, json=None, ssl=None):
174179
return reader
175180

176181
def get(self, url, ssl=None):
177-
return _RequestContextManager(
178-
self, self._request("GET", self._base_url + url, ssl=ssl)
179-
)
182+
return _RequestContextManager(self, self._request("GET", self._base_url + url, ssl=ssl))
180183

181184
def post(self, url, data=None, json=None, ssl=None):
182185
return _RequestContextManager(
@@ -222,76 +225,3 @@ async def _ws_connect(self, url, ssl=None):
222225
await ws_client.connect(url, ssl=ssl)
223226
self._reader = ws_client.reader
224227
return ClientWebSocketResponse(ws_client)
225-
226-
227-
def request_raw(method, url):
228-
try:
229-
proto, dummy, host, path = url.split("/", 3)
230-
except ValueError:
231-
proto, dummy, host = url.split("/", 2)
232-
path = ""
233-
234-
if proto == "http:":
235-
port = 80
236-
elif proto == "https:":
237-
port = 443
238-
else:
239-
raise ValueError("Unsupported protocol: " + proto)
240-
241-
if ":" in host:
242-
host, port = host.split(":", 1)
243-
port = int(port)
244-
245-
reader, writer = yield from asyncio.open_connection(
246-
host, port, ssl=proto == "https:"
247-
)
248-
# Use protocol 1.0, because 1.1 always allows to use chunked transfer-encoding
249-
# But explicitly set Connection: close, even though this should be default for 1.0,
250-
# because some servers misbehave w/o it.
251-
query = (
252-
"%s /%s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\nUser-Agent: compat\r\n\r\n"
253-
% (
254-
method,
255-
path,
256-
host,
257-
)
258-
)
259-
yield from writer.awrite(query.encode("latin-1"))
260-
# yield from writer.aclose()
261-
return reader
262-
263-
264-
def request(method, url):
265-
redir_cnt = 0
266-
redir_url = None
267-
while redir_cnt < 2:
268-
reader = yield from request_raw(method, url)
269-
headers = []
270-
sline = yield from reader.readline()
271-
sline = sline.split(None, 2)
272-
status = int(sline[1])
273-
chunked = False
274-
while True:
275-
line = yield from reader.readline()
276-
if not line or line == b"\r\n":
277-
break
278-
headers.append(line)
279-
if line.startswith(b"Transfer-Encoding:"):
280-
if b"chunked" in line:
281-
chunked = True
282-
elif line.startswith(b"Location:"):
283-
url = line.rstrip().split(None, 1)[1].decode("latin-1")
284-
285-
if 301 <= status <= 303:
286-
redir_cnt += 1
287-
yield from reader.aclose()
288-
continue
289-
break
290-
291-
if chunked:
292-
resp = ChunkedClientResponse(reader)
293-
else:
294-
resp = ClientResponse(reader)
295-
resp.status = status
296-
resp.headers = headers
297-
return resp

async_modules/aiohttp/aiohttpclient/aiohttpclient_ws.py renamed to async_modules/aiohttp/aiohttp/aiohttp_ws.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,13 @@ def __init__(self, params):
5555
self.writer = None
5656

5757
async def connect(self, uri, ssl=None):
58-
...
5958
uri = urlparse(uri)
6059
assert uri
6160
if uri.protocol == "wss":
6261
if not ssl:
6362
ssl = True
6463

65-
self.reader, self.writer = await asyncio.open_connection(
66-
uri.hostname, uri.port, ssl=ssl
67-
)
64+
self.reader, self.writer = await asyncio.open_connection(uri.hostname, uri.port, ssl=ssl)
6865
await self.handshake(uri)
6966

7067
@classmethod

async_modules/async_mip/async_mip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# MicroPython package installer
22
# MIT license; Copyright (c) 2022 Jim Mussared
33

4-
import aiohttpclient as aiohttp
4+
import aiohttp
55
import sys
66

77

0 commit comments

Comments
 (0)