|
4 | 4 | from extra.utils.codec import GZipDecoder |
5 | 5 |
|
6 | 6 |
|
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 |
8 | 13 | async def main(path: str, host: str = "127.0.0.1", port: int = 443, ssl: bool = True): |
9 | | - transform = GZipDecoder() |
| 14 | + transform = GZipDecoder() |
10 | 15 |
|
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"") |
25 | 30 |
|
26 | 31 |
|
27 | 32 | if __name__ == "__main__": |
28 | | - import sys |
| 33 | + import sys |
29 | 34 |
|
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 | + ) |
38 | 44 | # EOF |
0 commit comments