Skip to content

Commit 25ebe4a

Browse files
committed
python-ecosys/urequests: add streaming API support
-implement iterator protocol for Response so it can be used with streaming APIs -set socket blocking to False for responses to requests with `stream` argument so that stream APIs can be checked from any loop
1 parent a3df207 commit 25ebe4a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

python-ecosys/urequests/urequests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ def __init__(self, f):
77
self.encoding = "utf-8"
88
self._cached = None
99

10+
def __iter__(self):
11+
return self
12+
13+
def __next__(self):
14+
data = b""
15+
chunk = self.raw.read()
16+
while chunk:
17+
data += chunk
18+
chunk = self.raw.read()
19+
return data
20+
1021
def close(self):
1122
if self.raw:
1223
self.raw.close()
@@ -105,6 +116,10 @@ def request(method, url, data=None, json=None, headers={}, stream=None):
105116
resp = Response(s)
106117
resp.status_code = status
107118
resp.reason = reason
119+
120+
if stream:
121+
resp.raw.setblocking(False)
122+
108123
return resp
109124

110125

0 commit comments

Comments
 (0)