From 05efdd03a76e14b6d0b9d375f4c3441eb87a08a4 Mon Sep 17 00:00:00 2001 From: Mark Blakeney Date: Wed, 8 Nov 2023 08:15:21 +1000 Subject: [PATCH] uaiohttpclient: Update "yield from" to "await". Signed-off-by: Mark Blakeney --- micropython/uaiohttpclient/uaiohttpclient.py | 31 ++++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/micropython/uaiohttpclient/uaiohttpclient.py b/micropython/uaiohttpclient/uaiohttpclient.py index 878e37b9..6347c337 100644 --- a/micropython/uaiohttpclient/uaiohttpclient.py +++ b/micropython/uaiohttpclient/uaiohttpclient.py @@ -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 "" % (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 "" % (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