Skip to main content

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 union.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.0a241.tar.gz (57.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telepact-1.0.0a241-py3-none-any.whl (169.1 kB view details)

Uploaded Python 3

File details

Details for the file telepact-1.0.0a241.tar.gz.

File metadata

  • Download URL: telepact-1.0.0a241.tar.gz
  • Upload date:
  • Size: 57.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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

Hashes for telepact-1.0.0a241.tar.gz
Algorithm Hash digest
SHA256 3ce92aba80bce42f399c104a6a10fbfde5c8c6229ac2a49c2b1b767ec126ae3d
MD5 8a5696844b417ffc66baa309db5d196c
BLAKE2b-256 e495cb055e0a3f141c826ef4f0d2c1eef744b4855a48d8106fd7a8854e30ab07

See more details on using hashes here.

File details

Details for the file telepact-1.0.0a241-py3-none-any.whl.

File metadata

  • Download URL: telepact-1.0.0a241-py3-none-any.whl
  • Upload date:
  • Size: 169.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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

Hashes for telepact-1.0.0a241-py3-none-any.whl
Algorithm Hash digest
SHA256 2ad9b11b161183e08024d5d4bb2a66202102479eb1bebb6a6c9810e46095682d
MD5 94b655bb6ab2c6edc283c7e02d893659
BLAKE2b-256 56cafca4a996651ae5a5987a8bb852fa67c8df128de5b0341c3819aef78a291c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page