Skip to main content

Async Python client for RPC transport interoperability with NestJS microservices.

Project description

nest-rpc-client

PyPI Python

Async Python client for RPC transport interoperability with NestJS microservices. Supports RabbitMQ, Redis, NATS, TCP (and planned support for Kafka, MQTT).

Features

  • Fully async transports
  • Pluggable transport architecture
  • Compatible with NestJS microservices patterns (send, emit)
  • Extras-based installation

Installation

Install with a specific transport:

# TCP uses asyncio
pip install "nest-rpc-client[rabbitmq]"
pip install "nest-rpc-client[redis]"
pip install "nest-rpc-client[nats]"
pip install "nest-rpc-client[all]"

Usage

from nest_rpc_client.client import Client
from nest_rpc_client.transports.rabbitmq import RabbitMQTransport
from nest_rpc_client.config.rabbitmq import RabbitMQConfig

transport = RabbitMQTransport(RabbitMQConfig(url="amqp://guest:guest@localhost/", queue="rpc_queue"))

async with Client(transport) as client:
    result = await client.send("get_user", {"id": 123})
    await client.emit("user_created", {"id": 123})

You can also use only transport:

from nest_rpc_client.transports.rabbitmq import RabbitMQTransport
from nest_rpc_client.config.rabbitmq import RabbitMQConfig

transport = RabbitMQTransport(RabbitMQConfig(url="amqp://guest:guest@localhost/", queue="rpc_queue"))

await transport.connect()

result = await transport.send("get_user", {"id": 123})
await transport.emit("user_created", {"id": 123})

await transport.close()

Pattern usage with dictionaries

If your pattern is a dictionary (object in js, for example: {cmd: "sum"}), you need to serialize it to a string before using it. Example:

pattern = json.dumps({"cmd": "sum"}) # '{"cmd": "sum"}'
await client.send(pattern, data)

Custom Transport

nest-rpc-client allows you to use your own custom transports. To do this, inherit from the Transport base class and implement its abstract methods:

from nest_rpc_client.transport import Transport

class MyCustomTransport(Transport):
    async def connect(self) -> None:
        # Initialize any connections or resources
        ...

    async def close(self) -> None:
        # Clean up resources or close connections
        ...

    async def send(self, pattern: str, data: dict) -> dict:
        # Implement the RPC request (expects a response)
        ...

    async def emit(self, pattern: str, data: dict) -> None:
        # Implement the fire-and-forget event
        ...

You can then use your custom transport exactly like the built-in ones:

transport = MyCustomTransport()
async with Client(transport) as client:
    result = await client.send("my_pattern", {"foo": "bar"})

Exception handling

nest-rpc-client raises RpcException whenever the RPC server returns an error. Example:

from nest_rpc_client.client import Client
from nest_rpc_client.transports.rabbitmq import RabbitMQTransport
from nest_rpc_client.config.rabbitmq import RabbitMQConfig
from nest_rpc_client.exceptions import RpcException

transport = RabbitMQTransport(RabbitMQConfig(url="amqp://guest:guest@localhost/", queue="rpc_queue"))

async with Client(transport) as client:
    try:
        result = await client.send("get_user", {"id": 123})
    except RpcException as e:
        print(f"RPC Error: {e.err}")

When the RPC response includes an err field (following the NestJS microservice pattern), the client automatically raises RpcException with the contents of err as its attribute:

{
    "id": "correlation-id",
    "err": {
        "message": "User not found",
        "code": 404
    }
}

This allows consistent error handling across all transports.

Tests:

This project includes both unit tests and full integration tests.

  • Unit tests are included in this repository and can be run with: pytest
  • Full integration tests are available in a separate repository: nest-rpc-client-tests

TODO:

  • Implement kafka transport
  • Implement mqtt transport

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

nest_rpc_client-1.3.0b0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

nest_rpc_client-1.3.0b0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file nest_rpc_client-1.3.0b0.tar.gz.

File metadata

  • Download URL: nest_rpc_client-1.3.0b0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.2 Linux/6.1.0-31-amd64

File hashes

Hashes for nest_rpc_client-1.3.0b0.tar.gz
Algorithm Hash digest
SHA256 aeddc9e6593ada1fc1a59c38ec53820e9a0cb13bfc9ebcea71c863958df473a3
MD5 e5b4dd8cdc16f651fb7c671dfdab083d
BLAKE2b-256 55a7cf5b04ced5a051183cd93fc7d3c6853f1fdc77c122d3a09e394a755a2c6a

See more details on using hashes here.

File details

Details for the file nest_rpc_client-1.3.0b0-py3-none-any.whl.

File metadata

  • Download URL: nest_rpc_client-1.3.0b0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.2 Linux/6.1.0-31-amd64

File hashes

Hashes for nest_rpc_client-1.3.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 39de1d7b7a1d5c2a8f6a0cb737524e32a8d1015df82ffb27b08a7acadb67beb2
MD5 75af6f582e1b1ae7de92b861804f0ae2
BLAKE2b-256 caf4fb338a8aeb9bd3da193acdf2c330fb4d3f8ebbf8c8de902ebce41a62242c

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