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 handler(request_message: 'Message') -> 'Message':
function_name = next(iter(request_message.body.keys()))
arguments = request_message.body[function_name]
try:
# Early in the handler, perform any pre-flight "middleware" operations, such as
# authentication, tracing, or logging.
log.info("Function started", {'function': function_name})
# Dispatch request to appropriate function handling code.
# (This example uses manual dispatching, but you can also use a more advanced pattern.)
if function_name == 'fn.greet':
subject = arguments['subject']
return Message({}, {'Ok_': {'message': f'Hello {subject}!'}})
raise Exception('Function not found')
finally:
# At the end the handler, perform any post-flight "middleware" operations
log.info("Function finished", {'function': function_name})
options = Server.Options()
# Set this to False when your schema does not define struct.Auth_.
options.auth_required = False
server = Server(schema, handler, 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.0a211.tar.gz
(50.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.0a211-py3-none-any.whl
(167.3 kB
view details)
File details
Details for the file telepact-1.0.0a211.tar.gz.
File metadata
- Download URL: telepact-1.0.0a211.tar.gz
- Upload date:
- Size: 50.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.12.3 Linux/6.14.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6aa63036e832515b971fad34cd3e0a85adab8c54f875e3a9a44b404c0c7fff40
|
|
| MD5 |
40d2b171629fd65712cdaa002532d5af
|
|
| BLAKE2b-256 |
814031071ef22f2b28d0ef8b4e1f1f4e9a17a74f4be429497a45ca7d259a1460
|
File details
Details for the file telepact-1.0.0a211-py3-none-any.whl.
File metadata
- Download URL: telepact-1.0.0a211-py3-none-any.whl
- Upload date:
- Size: 167.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.12.3 Linux/6.14.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ae01eaba444782029e3b60c6fd667ae2b3966471dd0c9e59cd715fcc82b5f3c
|
|
| MD5 |
cdb8fe76b2026930cb3f7300e17a7cdd
|
|
| BLAKE2b-256 |
6296f7fecef502a5bfec3b9b2d23e71d19d19b085181d6caa96c4886846e9e03
|