Skip to main content

Python client library for MiddleMan applications

Project description

MiddleMan Client - Python

A Python client library for connecting to MiddleMan applications using SignalR WebSocket connections.

Installation

Install from source (editable - useful for development):

pip install -e .

Or build and install the wheel/sdist locally:

python -m pip install --upgrade build
python -m build
pip install dist/middleman_client_python-*.whl

If you just want to install runtime dependencies from the repository, you can still use:

pip install -r requirements.txt

Quick Start

import asyncio
from middleman_client import ClientConnectionBuilderFactory
from middleman_client.method_processing.method_discovery import middleman_method

class MyService:
    @middleman_method
    def my_method(self, value: str) -> str:
        return f"Processed: {value}"

async def main():
    # Create connection
    connection = (ClientConnectionBuilderFactory.create()
                 .with_host("ws://localhost:5000/middlemanhub")
                 .with_token("your-token")
                 .with_reconnect()
                 .build())
    
    # Register service
    service = MyService()
    connection.add_method_calling_handler(MyService, service)
    
    # Use current module for method discovery
    import sys
    connection.use_module(sys.modules[__name__])
    
    # Start connection
    await connection.start_async()
    
    # Keep alive
    while True:
        await asyncio.sleep(1)

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

Features

  • SignalR Integration: Built on top of signalrcore for reliable WebSocket communication
  • Method Discovery: Automatic discovery of methods marked with @middleman_method decorator
  • Type Safety: Full type hint support for method parameters and return values
  • Async Support: Native support for both synchronous and asynchronous methods
  • Auto Reconnection: Optional automatic reconnection on connection loss
  • Builder Pattern: Fluent API for configuring connections

API Reference

ClientConnectionBuilderFactory

Factory class for creating connection builders.

  • create() - Creates a new IClientConnectionBuilder instance

IClientConnectionBuilder

Builder interface for configuring client connections.

  • with_host(host: str) - Set the SignalR hub URL
  • with_token(token: str) - Set authentication token
  • with_reconnect() - Enable automatic reconnection
  • build() - Create the ClientConnection instance

ClientConnection

Main class for managing the SignalR connection and method registration.

  • add_method_calling_handler(handler_type, handler) - Register a service instance
  • use_module(module) - Set the module to discover methods from
  • start_async() - Start the connection and register methods

@middleman_method

Decorator to mark methods as callable via the MiddleMan server.

@middleman_method
def my_method(self, param: int) -> str:
    return f"Result: {param}"

Method Discovery

The client automatically discovers methods marked with the @middleman_method decorator. Methods can be:

  • Instance methods in classes
  • Static methods
  • Module-level functions

Type Support

The client supports the following Python types:

  • Primitives: int, float, str, bool
  • Collections: List[T]
  • Optional types: Optional[T]
  • Custom classes with type annotations
  • Async return types

Error Handling

The client includes proper error handling for:

  • Connection failures
  • Invalid client options
  • Method invocation errors
  • Serialization/deserialization errors

Requirements

  • Python 3.8+
  • signalrcore>=0.9.5
  • websockets>=11.0
  • typing-extensions>=4.0.0

License

This project is licensed under the MIT License.

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

middleman_danel-1.0.4.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

middleman_danel-1.0.4-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file middleman_danel-1.0.4.tar.gz.

File metadata

  • Download URL: middleman_danel-1.0.4.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for middleman_danel-1.0.4.tar.gz
Algorithm Hash digest
SHA256 320575c1468285f7c1800de936abba1a8883708652b290f3a78f5642e54e2843
MD5 c44d5b27eb9d353e5de40eedf6ff68e8
BLAKE2b-256 f937099af6fec4ad5dac97d0bdbd6d3d54bfb0d8063594abaf6da99d0d502165

See more details on using hashes here.

File details

Details for the file middleman_danel-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for middleman_danel-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 596db3524972c22c938010328459643bbda9dadf941d1dff4f82950d6c36fa0a
MD5 b164d0be041dcd507c457c975783f8cc
BLAKE2b-256 48fd0f5487bd83176a61420dd13a19f4ee0ac18061b18e63abb319379f63b2ee

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