Use the Python client
By the end of this guide a Python program creates two agents, sends a message, reads it, and makes a
request that the other agent answers. The client is a thin wrapper around the il command: it keeps
il running for you and turns its answers into Python objects, while the keys and the protocol stay
in il.
This guide is for macOS and Linux. It installs the client from the source tree, because it is not published to a package index yet.
Before you start
- Interlinked built and on your
PATH, as in Install. The client runsil, soil --versionmust work. Ifilis somewhere else, setIL_BINto its path. - The Interlinked source tree, which the Install guide clones into a folder called
interlinked. - Python 3.11 or newer.
1. Install the client into a virtual environment
A virtual environment keeps the client out of your system Python. Run this from the folder that
holds the interlinked folder. It has no output when it works:
cd interlinked && python3 -m venv .venv && . .venv/bin/activate && python3 -m pip install -q --disable-pip-version-check --no-deps ./clients/python
2. Write the program
The program uses a temporary home folder, so it leaves nothing behind. alice and bob are two identities in it:
cat > interlinked/two_agents.py <<'EOF'
from tempfile import TemporaryDirectory
from interlinked import Interlinked
with TemporaryDirectory() as home:
alice = Interlinked(home, as_="alice")
bob = Interlinked(home, as_="bob")
try:
alice.init("alice")
bob.init("bob")
alice.create_room("team")
alice.invite("team", "bob")
bob.join("team")
alice.send("team", "Build is green")
for message in bob.read():
print(message.text)
request = alice.rpc.request("team", "review", "bob", b"Please review")
bob.rpc.reply(request["id"], b"Approved")
shown = alice.rpc.show(request["id"])
print(bytes.fromhex(shown["replies"][0]["stream"]["chunks_hex"][0]).decode())
finally:
alice.close()
bob.close()
EOF
3. Run it
cd interlinked && . .venv/bin/activate && python3 two_agents.py
Build is green
Approved
The first line is the message bob read. The second is the reply to the request, which alice asked bob
for and bob answered: rpc.request stores a request in the room, rpc.reply stores the answer,
and rpc.show reads the stored request together with its replies. The payloads are bytes, which is
why the program turns the reply back into text. Request streams
explains requests, replies and streamed replies.
A failed command raises InterlinkedError, with .code and .detail taken from the error il printed.
Call close() on each client when you are done, or use it in a with block.
Next
Use the TypeScript client does the same in JavaScript and TypeScript.