The Python Telepact library
Project description
Telepact Library for Python
Installation
pip install --pre telepact
Published PyPI releases are currently prereleases. To pin a specific release, use the exact version from doc/versions.md.
Usage
API:
- fn.greet:
subject: string
->:
Ok_:
message: string
Server:
from telepact import Client, Message, Serializer, Server, TelepactSchema, TelepactSchemaFiles
files = TelepactSchemaFiles('/directory/containing/api/files')
schema = TelepactSchema.from_file_json_map(files.filenames_to_json)
# The schema directory may contain multiple *.telepact.yaml and
# *.telepact.json files. Subdirectories are rejected.
async def greet(function_name: str, request_message: 'Message') -> 'Message':
arguments = request_message.body[function_name]
subject = arguments['subject']
return Message({}, {'Ok_': {'message': f'Hello {subject}!'}})
async def middleware(request_message: 'Message', function_router) -> 'Message':
function_name = request_message.get_body_target()
try:
log.info("Function started", {'function': function_name})
return await function_router.route(request_message)
finally:
log.info("Function finished", {'function': function_name})
options = Server.Options()
# Set this to False when your schema does not define union.Auth_.
options.auth_required = False
options.middleware = middleware
server = Server(schema, {'fn.greet': greet}, options)
# Wire up request/response bytes from your transport of choice
async def transport_handler(request_bytes: bytes) -> bytes:
response = await server.process(request_bytes)
return response.bytes
transport.receive(transport_handler)
Client:
async def adapter(m: Message, s: Serializer) -> Message:
request_bytes = s.serialize(m)
# Wire up request/response bytes to your transport of choice
response_bytes = await transport.send(request_bytes)
return s.deserialize(response_bytes)
options = Client.Options()
client = Client(adapter, options)
# Inside your async application code:
request = Message({}, {'fn.greet': {'subject': 'World'}})
response = await client.request(request)
if response.get_body_target() == 'Ok_':
ok_payload = response.get_body_payload()
print(ok_payload['message'])
else:
raise RuntimeError(f"Unexpected response: {response.body}")
For more concrete usage examples, see the tests.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
telepact-1.0.0a247.tar.gz
(57.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
telepact-1.0.0a247-py3-none-any.whl
(169.3 kB
view details)
File details
Details for the file telepact-1.0.0a247.tar.gz.
File metadata
- Download URL: telepact-1.0.0a247.tar.gz
- Upload date:
- Size: 57.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.5 {"installer":{"name":"uv","version":"0.11.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81a3dafe0bce9c8ffe74d47032d548b9cfac9e1cb3bcb29cfe3b44c56e269125
|
|
| MD5 |
f5d3231c3c22cd44177516730b1908b4
|
|
| BLAKE2b-256 |
3f8e595058ed3d3568952a2277b2ac619c36bce2b178fd8c1b794ee9b05fbd05
|
File details
Details for the file telepact-1.0.0a247-py3-none-any.whl.
File metadata
- Download URL: telepact-1.0.0a247-py3-none-any.whl
- Upload date:
- Size: 169.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.5 {"installer":{"name":"uv","version":"0.11.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f65190f5b7293f75be72e5d5269b0d7deecdc1a0bf60ee0444804745e513c67
|
|
| MD5 |
dbe4d919898dc8d5ee16e306f91aefaf
|
|
| BLAKE2b-256 |
2832320bd29dda190ae9dc8e021634e3328428b8ddcdb2d7078ab9afd37ea3d0
|