kopia lustrzana https://github.com/micropython/micropython-lib
uaiohttpclient: Update "yield from" to "await".
Signed-off-by: Mark Blakeney <mark.blakeney@bullet-systems.net>pull/764/head
rodzic
9d09cdd4af
commit
05efdd03a7
|
@ -5,8 +5,8 @@ class ClientResponse:
|
|||
def __init__(self, reader):
|
||||
self.content = reader
|
||||
|
||||
def read(self, sz=-1):
|
||||
return (yield from self.content.read(sz))
|
||||
async def read(self, sz=-1):
|
||||
return await self.content.read(sz)
|
||||
|
||||
def __repr__(self):
|
||||
return "<ClientResponse %d %s>" % (self.status, self.headers)
|
||||
|
@ -17,22 +17,22 @@ class ChunkedClientResponse(ClientResponse):
|
|||
self.content = reader
|
||||
self.chunk_size = 0
|
||||
|
||||
def read(self, sz=4 * 1024 * 1024):
|
||||
async def read(self, sz=4 * 1024 * 1024):
|
||||
if self.chunk_size == 0:
|
||||
line = yield from self.content.readline()
|
||||
line = await self.content.readline()
|
||||
# print("chunk line:", l)
|
||||
line = line.split(b";", 1)[0]
|
||||
self.chunk_size = int(line, 16)
|
||||
# print("chunk size:", self.chunk_size)
|
||||
if self.chunk_size == 0:
|
||||
# End of message
|
||||
sep = yield from self.content.read(2)
|
||||
sep = await self.content.read(2)
|
||||
assert sep == b"\r\n"
|
||||
return b""
|
||||
data = yield from self.content.read(min(sz, self.chunk_size))
|
||||
data = await self.content.read(min(sz, self.chunk_size))
|
||||
self.chunk_size -= len(data)
|
||||
if self.chunk_size == 0:
|
||||
sep = yield from self.content.read(2)
|
||||
sep = await self.content.read(2)
|
||||
assert sep == b"\r\n"
|
||||
return data
|
||||
|
||||
|
@ -40,7 +40,7 @@ class ChunkedClientResponse(ClientResponse):
|
|||
return "<ChunkedClientResponse %d %s>" % (self.status, self.headers)
|
||||
|
||||
|
||||
def request_raw(method, url):
|
||||
async def request_raw(method, url):
|
||||
try:
|
||||
proto, dummy, host, path = url.split("/", 3)
|
||||
except ValueError:
|
||||
|
@ -55,7 +55,7 @@ def request_raw(method, url):
|
|||
|
||||
if proto != "http:":
|
||||
raise ValueError("Unsupported protocol: " + proto)
|
||||
reader, writer = yield from asyncio.open_connection(host, port)
|
||||
reader, writer = await asyncio.open_connection(host, port)
|
||||
# Use protocol 1.0, because 1.1 always allows to use chunked
|
||||
# transfer-encoding But explicitly set Connection: close, even
|
||||
# though this should be default for 1.0, because some servers
|
||||
|
@ -65,22 +65,21 @@ def request_raw(method, url):
|
|||
path,
|
||||
host,
|
||||
)
|
||||
yield from writer.awrite(query.encode("latin-1"))
|
||||
# yield from writer.aclose()
|
||||
await writer.awrite(query.encode("latin-1"))
|
||||
return reader
|
||||
|
||||
|
||||
def request(method, url):
|
||||
async def request(method, url):
|
||||
redir_cnt = 0
|
||||
while redir_cnt < 2:
|
||||
reader = yield from request_raw(method, url)
|
||||
reader = await request_raw(method, url)
|
||||
headers = []
|
||||
sline = yield from reader.readline()
|
||||
sline = await reader.readline()
|
||||
sline = sline.split(None, 2)
|
||||
status = int(sline[1])
|
||||
chunked = False
|
||||
while True:
|
||||
line = yield from reader.readline()
|
||||
line = await reader.readline()
|
||||
if not line or line == b"\r\n":
|
||||
break
|
||||
headers.append(line)
|
||||
|
@ -92,7 +91,7 @@ def request(method, url):
|
|||
|
||||
if 301 <= status <= 303:
|
||||
redir_cnt += 1
|
||||
yield from reader.aclose()
|
||||
await reader.aclose()
|
||||
continue
|
||||
break
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue