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):
|
def __init__(self, reader):
|
||||||
self.content = reader
|
self.content = reader
|
||||||
|
|
||||||
def read(self, sz=-1):
|
async def read(self, sz=-1):
|
||||||
return (yield from self.content.read(sz))
|
return await self.content.read(sz)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<ClientResponse %d %s>" % (self.status, self.headers)
|
return "<ClientResponse %d %s>" % (self.status, self.headers)
|
||||||
|
@ -17,22 +17,22 @@ class ChunkedClientResponse(ClientResponse):
|
||||||
self.content = reader
|
self.content = reader
|
||||||
self.chunk_size = 0
|
self.chunk_size = 0
|
||||||
|
|
||||||
def read(self, sz=4 * 1024 * 1024):
|
async def read(self, sz=4 * 1024 * 1024):
|
||||||
if self.chunk_size == 0:
|
if self.chunk_size == 0:
|
||||||
line = yield from self.content.readline()
|
line = await self.content.readline()
|
||||||
# print("chunk line:", l)
|
# print("chunk line:", l)
|
||||||
line = line.split(b";", 1)[0]
|
line = line.split(b";", 1)[0]
|
||||||
self.chunk_size = int(line, 16)
|
self.chunk_size = int(line, 16)
|
||||||
# print("chunk size:", self.chunk_size)
|
# print("chunk size:", self.chunk_size)
|
||||||
if self.chunk_size == 0:
|
if self.chunk_size == 0:
|
||||||
# End of message
|
# End of message
|
||||||
sep = yield from self.content.read(2)
|
sep = await self.content.read(2)
|
||||||
assert sep == b"\r\n"
|
assert sep == b"\r\n"
|
||||||
return b""
|
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)
|
self.chunk_size -= len(data)
|
||||||
if self.chunk_size == 0:
|
if self.chunk_size == 0:
|
||||||
sep = yield from self.content.read(2)
|
sep = await self.content.read(2)
|
||||||
assert sep == b"\r\n"
|
assert sep == b"\r\n"
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ class ChunkedClientResponse(ClientResponse):
|
||||||
return "<ChunkedClientResponse %d %s>" % (self.status, self.headers)
|
return "<ChunkedClientResponse %d %s>" % (self.status, self.headers)
|
||||||
|
|
||||||
|
|
||||||
def request_raw(method, url):
|
async def request_raw(method, url):
|
||||||
try:
|
try:
|
||||||
proto, dummy, host, path = url.split("/", 3)
|
proto, dummy, host, path = url.split("/", 3)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -55,7 +55,7 @@ def request_raw(method, url):
|
||||||
|
|
||||||
if proto != "http:":
|
if proto != "http:":
|
||||||
raise ValueError("Unsupported protocol: " + proto)
|
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
|
# Use protocol 1.0, because 1.1 always allows to use chunked
|
||||||
# transfer-encoding But explicitly set Connection: close, even
|
# transfer-encoding But explicitly set Connection: close, even
|
||||||
# though this should be default for 1.0, because some servers
|
# though this should be default for 1.0, because some servers
|
||||||
|
@ -65,22 +65,21 @@ def request_raw(method, url):
|
||||||
path,
|
path,
|
||||||
host,
|
host,
|
||||||
)
|
)
|
||||||
yield from writer.awrite(query.encode("latin-1"))
|
await writer.awrite(query.encode("latin-1"))
|
||||||
# yield from writer.aclose()
|
|
||||||
return reader
|
return reader
|
||||||
|
|
||||||
|
|
||||||
def request(method, url):
|
async def request(method, url):
|
||||||
redir_cnt = 0
|
redir_cnt = 0
|
||||||
while redir_cnt < 2:
|
while redir_cnt < 2:
|
||||||
reader = yield from request_raw(method, url)
|
reader = await request_raw(method, url)
|
||||||
headers = []
|
headers = []
|
||||||
sline = yield from reader.readline()
|
sline = await reader.readline()
|
||||||
sline = sline.split(None, 2)
|
sline = sline.split(None, 2)
|
||||||
status = int(sline[1])
|
status = int(sline[1])
|
||||||
chunked = False
|
chunked = False
|
||||||
while True:
|
while True:
|
||||||
line = yield from reader.readline()
|
line = await reader.readline()
|
||||||
if not line or line == b"\r\n":
|
if not line or line == b"\r\n":
|
||||||
break
|
break
|
||||||
headers.append(line)
|
headers.append(line)
|
||||||
|
@ -92,7 +91,7 @@ def request(method, url):
|
||||||
|
|
||||||
if 301 <= status <= 303:
|
if 301 <= status <= 303:
|
||||||
redir_cnt += 1
|
redir_cnt += 1
|
||||||
yield from reader.aclose()
|
await reader.aclose()
|
||||||
continue
|
continue
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue