Skip to content

Commit 6eea7af

Browse files
committed
[WIP] examples: adjusting
1 parent c846656 commit 6eea7af

File tree

3 files changed

+87
-67
lines changed

3 files changed

+87
-67
lines changed

examples/client-gzip.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,41 @@
44
from extra.utils.codec import GZipDecoder
55

66

7-
# NOTE: Start "examples/sse.py"
7+
# --
8+
# ## HTTP Client Example
9+
#
10+
# Shows how to use the HTTP client module, in this case with GZipped content.
11+
# Note how the client API by default returns an iterator, which may be more
12+
# low-level than what you'd be used to (with
813
async def main(path: str, host: str = "127.0.0.1", port: int = 443, ssl: bool = True):
9-
transform = GZipDecoder()
14+
transform = GZipDecoder()
1015

11-
with open("/dev/stdout", "wb") as f:
12-
async for atom in HTTPClient.Request(
13-
host=host,
14-
method="GET",
15-
port=port,
16-
path=path,
17-
timeout=11.0,
18-
streaming=False,
19-
headers={"Accept-Encoding": "gzip"},
20-
ssl=ssl,
21-
):
22-
if isinstance(atom, HTTPBodyBlob):
23-
f.write(transform.feed(atom.payload) or b"")
24-
f.write(transform.flush() or b"")
16+
with open("/dev/stdout", "wb") as f:
17+
async for atom in HTTPClient.Request(
18+
host=host,
19+
method="GET",
20+
port=port,
21+
path=path,
22+
timeout=11.0,
23+
streaming=False,
24+
headers={"Accept-Encoding": "gzip"},
25+
ssl=ssl,
26+
):
27+
if isinstance(atom, HTTPBodyBlob):
28+
f.write(transform.feed(atom.payload) or b"")
29+
f.write(transform.flush() or b"")
2530

2631

2732
if __name__ == "__main__":
28-
import sys
33+
import sys
2934

30-
args = sys.argv[2:] or ["/index"]
31-
n = len(args)
32-
asyncio.run(
33-
main(
34-
path="/gh/lodash/lodash/4.17.15-npm/lodash.min.js",
35-
host="cdn.statically.io",
36-
)
37-
)
35+
args = sys.argv[2:] or ["/index"]
36+
n = len(args)
37+
# Test: curl -v https://cdn.statically.io/gh/lodash/lodash/4.17.15-npm/lodash.min.js
38+
asyncio.run(
39+
main(
40+
path="/gh/lodash/lodash/4.17.15-npm/lodash.min.js",
41+
host="cdn.statically.io",
42+
)
43+
)
3844
# EOF

examples/client-sse.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
import asyncio
22
from extra.client import HTTPClient
3+
from extra.utils.logging import info
4+
5+
6+
# --
7+
# ## HTTP Client Sever Set Eevents
8+
#
9+
# This shows how to use the HTTP client with the `streaming=True` option
10+
# to process SSE responses.
11+
#
12+
# This should be run with the `examples/sse.py` as the server.
313

414

515
# NOTE: Start "examples/sse.py"
6-
async def main():
7-
async for atom in HTTPClient.Request(
8-
host="127.0.0.1",
9-
method="GET",
10-
port=8001,
11-
path="/time/5",
12-
timeout=10.0,
13-
streaming=True,
14-
ssl=False,
15-
):
16-
print(" >>> ", atom)
16+
async def main(host: str = "localhost", port=8003):
17+
info("Connecting", Host=host, Port=port)
18+
async for atom in HTTPClient.Request(
19+
method="GET",
20+
host=host,
21+
port=port,
22+
path="/time/5",
23+
timeout=10.0,
24+
streaming=True,
25+
ssl=False,
26+
):
27+
info(f"Received atom: {atom}")
1728

1829

30+
info("Make sure you have an SSE server running, eg: 'python examples/sse.py")
1931
print(asyncio.run(main()))
2032
# EOF

examples/client.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
import asyncio
22
from extra.client import HTTPClient, pooling
3+
from extra.utils.logging import info
4+
from extra.utils.uri import URI
35

46

57
# NOTE: Start "examples/sse.py"
68
async def main(path: str, host: str = "127.0.0.1", port: int = 8000, ssl: bool = False):
7-
print(f"Connecting to {host}:{port}{path}")
8-
# NOTE: Connection pooling does not seem to be working
9-
with pooling(idle=3600):
10-
for _ in range(n := 5):
11-
async for atom in HTTPClient.Request(
12-
host=host,
13-
method="GET",
14-
port=port,
15-
path=path,
16-
timeout=10.0,
17-
streaming=False,
18-
# NOTE: If you se this to False and you get pooling,
19-
# you'll get a Connection lost, which is expected.
20-
keepalive=_ < n - 1,
21-
ssl=ssl,
22-
):
23-
pass
24-
# print(" >>> ", atom)
25-
await asyncio.sleep(0.25)
9+
info(f"Client connecting to {host}:{port}{path}")
10+
# NOTE: Connection pooling does not seem to be working
11+
with pooling(idle=3600):
12+
for _ in range(n := 5):
13+
info("Trying request", Count=n)
14+
async for atom in HTTPClient.Request(
15+
host=host,
16+
method="GET",
17+
port=port,
18+
path=path,
19+
timeout=10.0,
20+
streaming=False,
21+
# NOTE: If you se this to False and you get pooling,
22+
# you'll get a Connection lost, which is expected.
23+
keepalive=_ < n - 1,
24+
ssl=ssl,
25+
):
26+
info(f"Received atom: {atom}")
27+
await asyncio.sleep(0.25)
2628

2729

2830
if __name__ == "__main__":
29-
import sys
31+
import sys
3032

31-
args = sys.argv[1:] or ["/index"]
32-
n = len(args)
33-
print(
34-
asyncio.run(
35-
main(
36-
path=args[0],
37-
host=args[1] if n > 1 else "127.0.0.1",
38-
port=int(args[2]) if n > 2 else 8000,
39-
)
40-
)
41-
)
33+
uri = URI.Parse(sys.argv[1] if len(sys.argv) > 1 else "https://google.com/")
34+
print(
35+
asyncio.run(
36+
main(
37+
path=uri.path,
38+
host=uri.host,
39+
port=uri.port,
40+
ssl=uri.ssl,
41+
)
42+
)
43+
)
4244
# EOF

0 commit comments

Comments
 (0)