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_client_python-1.0.2.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

middleman_client_python-1.0.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file middleman_client_python-1.0.2.tar.gz.

File metadata

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

File hashes

Hashes for middleman_client_python-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f68e7f48dddd66bbad393865e073c5bf78f272ed053391ce785d505716d9103c
MD5 89b8c631abce5601d621fceb19fde6c3
BLAKE2b-256 485549dda6d85554eb2e1abaeb21d4e8e84f3f2ae7e29581c41f9cbe3c4a401e

See more details on using hashes here.

File details

Details for the file middleman_client_python-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for middleman_client_python-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f70b49e771315f0cd8f553fe3fb3ab05300b5f294b2a1d354da52b4508881687
MD5 84183d455c5ee309aab9f6b64715c6d1
BLAKE2b-256 d813d968b18e889b66d83837c7fb8961411e9c8f6d075023b123fd3f25831526

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