Skip to main content

Library to work with RPC

Project description

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())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kode_rpc-0.0.8.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

kode_rpc-0.0.8-py3-none-any.whl (6.9 kB view hashes)

Uploaded Python 3

Supported by

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