Skip to main content

kode_rpc

Server Usage

from typing import Mapping
import asyncio
import logging

from kode_rpc.method import RPCMethod, method
from kode_rpc.server import RPCServer, RPCRequestInfo


@method(version=1)
async def some_rpc_method(param: str):
    return {'result': True}


async def rpc_logger(rpc_method: RPCMethod, result: Mapping, request_info: RPCRequestInfo):
    logging.info('rpc_log', extra={
        'rpc_name': rpc_method.name,
        'rpc_result': result,
        'trace_id': request_info.trace_id,
        'master': request_info.master
    })


async def rpc_exception_handler(rpc_method: RPCMethod, exc: BaseException, request_info: RPCRequestInfo) -> Mapping:
    if isinstance(exc, RuntimeError):
        return {
            'error': {
                'code': 'RuntimeError',
                'trace_id': request_info.trace_id
            }
        }
    else:
        return {
            'error': {
                'code': 'ServerError',
                'trace_id': request_info.trace_id
            }
        }


async def main():
    logging.basicConfig(level=logging.INFO)

    server = RPCServer('my_service', rabbitmq_host='localhost', rabbitmq_user='guest', rabbitmq_password='guest')
    server.method_logger(rpc_logger)
    server.method_exception_handler(rpc_exception_handler)

    await server.register_methods([some_rpc_method])
    await server.serve()


if __name__ == '__main__':
    asyncio.run(main())

Client Usage

import asyncio

from kode_rpc.client import RPCClient

# To access rpc_client from anywhere it must be initialized at top level module
rpc_client = RPCClient('client_name')


async def main():
    # Connect to rpc broker once at startup
    await rpc_client.connect(rabbitmq_host='localhost', rabbitmq_user='guest', rabbitmq_password='guest')

    # ...
    # Somewhere in logic
    result = await rpc_client.call(
        service_name='my_service', 
        method='rpc_method', 
        payload={'param': 'any'}
    )
    # => {'result': True}

    # When application is shutting down do not forget to disconnect from rpc broker
    await rpc_client.disconnect()


if __name__ == '__main__':
    asyncio.run(main())

Metadata

Release files for kode-rpc 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kode-rpc 0.0.8
File Size Uploaded
kode_rpc-0.0.8.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kode-rpc 0.0.8
File Interpreter ABI Platform
kode_rpc-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 12.0 kB

Release files / kode_rpc-0.0.8.tar.gz

Download URL kode_rpc-0.0.8.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
496f4de686f412182752c1a1bcde1a184313f4793482a6b5597af4c85e34e1b2
BLAKE2b-256 checksum
How to use checksums
3c029a04a29dcd15a6a8d8092d2a47a9030b67ef1abac75d6ce5aed73989d612
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

Release files / kode_rpc-0.0.8-py3-none-any.whl

Download URL kode_rpc-0.0.8-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc2146e3249ffa215583c34137ac02b02245158632781361d0b72232d809d325
BLAKE2b-256 checksum
How to use checksums
857ae952227df525a3fb7afb3679877f4c6685d18b7b01309706b482507822f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page