-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.py
More file actions
58 lines (38 loc) · 1.36 KB
/
http_client.py
File metadata and controls
58 lines (38 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""HTTP client that connects to the demo HTTP server.
Requires the HTTP extra: ``pip install vgi-rpc[http]``
Start the server first::
python examples/http_server.py
Then run this client::
python examples/http_client.py
"""
from __future__ import annotations
from typing import Protocol
from vgi_rpc import Stream, StreamState
from vgi_rpc.http import http_connect
PORT = 8234
class DemoService(Protocol):
"""Demonstration HTTP service.
This Protocol is duplicated from http_server.py so each file is
self-contained. In a real project you'd define the Protocol once in a
shared module and import it from both sides.
"""
def echo(self, message: str) -> str:
"""Echo a message back."""
...
def fibonacci(self, limit: int) -> Stream[StreamState]:
"""Stream Fibonacci numbers up to *limit*."""
...
def main() -> None:
"""Connect to the HTTP server and make calls."""
url = f"http://127.0.0.1:{PORT}"
with http_connect(DemoService, url) as svc:
# Unary call
result = svc.echo(message="Hello from HTTP!")
print(f"echo: {result}")
# Streaming call
print("\nFibonacci numbers up to 100:")
for batch in svc.fibonacci(limit=100):
for row in batch.batch.to_pylist():
print(f" {row['fib']}")
if __name__ == "__main__":
main()