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/04-operate/03-versions.md.

Usage

API:

- fn.greet:
    subject: string
  ->:
    Ok_:
      message: string

Server:

from telepact import Client, FunctionRouter, Message, Serializer, Server, TelepactSchema

schema = TelepactSchema.from_directory('/directory/containing/api/files')

# 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
function_router = FunctionRouter({'fn.greet': greet})
server = Server(schema, function_router, 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.0a326.tar.gz (58.1 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.0a326-py3-none-any.whl (172.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: telepact-1.0.0a326.tar.gz
  • Upload date:
  • Size: 58.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","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.0a326.tar.gz
Algorithm Hash digest
SHA256 bd94caea9a071e9f2a7f100f1b98ecba38ee8ef10d91eea71dca1ba7c87edbad
MD5 052cf2aa078f1732c97510a0b7d5a800
BLAKE2b-256 0112443a3c96c8ac1b629d4341e56d7cb6eaf30978ecd4ac2b8ca6216b4180cb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telepact-1.0.0a326-py3-none-any.whl
  • Upload date:
  • Size: 172.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","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.0a326-py3-none-any.whl
Algorithm Hash digest
SHA256 82471951e14059ce2abca369e35c2933c19c3431902c153b6a67722298561a03
MD5 f1fb5e9c9dadebf7b597c49021d603f4
BLAKE2b-256 0ce8a14d2aa9f7d0e8cba69c857b3f5b6137a7d649aa9525c6a30d00875c4e98

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