Repository navigation
Expand file tree
/
Copy pathminimal_client.py
More file actions
61 lines (45 loc) · 1.55 KB
/
Copy pathminimal_client.py
File metadata and controls
61 lines (45 loc) · 1.55 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
59
60
61
#!/usr/bin/env python3
"""OpenANP Minimal Client Example.
Call a remote ANP agent with minimal code.
Prerequisites:
Start minimal_server.py first:
uvicorn examples.python.openanp_examples.minimal_server:app --port 8000
Run:
uv run python examples/python/openanp_examples/minimal_client.py
"""
import asyncio
from pathlib import Path
from anp.authentication import DIDWbaAuthHeader
from anp.openanp import RemoteAgent
# DID document and private key paths
PROJECT_ROOT = Path(__file__).parent.parent.parent.parent
DID_DOC = PROJECT_ROOT / "docs/did_public/public-did-doc.json"
PRIVATE_KEY = PROJECT_ROOT / "docs/did_public/public-private-key.pem"
async def main() -> None:
"""Minimal client demo."""
# 1. Create authentication
auth = DIDWbaAuthHeader(
did_document_path=str(DID_DOC),
private_key_path=str(PRIVATE_KEY),
)
# 2. Discover agent
print("Discovering agent...")
try:
agent = await RemoteAgent.discover(
"http://localhost:8000/agent/ad.json",
auth,
)
except Exception as e:
print(f"Connection failed: {e}")
print("\nPlease start the server first:")
print(" uvicorn examples.python.openanp_examples.minimal_server:app --port 8000")
return
print(f"Connected: {agent.name}")
# 3. Call methods
result = await agent.add(a=10, b=20)
print(f"10 + 20 = {result}")
result = await agent.multiply(a=6, b=7)
print(f"6 × 7 = {result}")
print("\nDemo completed!")
if __name__ == "__main__":
asyncio.run(main())