Skip to main content

NetworkPype is a Python library for building efficient network communication pipelines, supporting both REST and WebSocket protocols with built-in rate limiting and time synchronization.

Project description

NetworkPype

CI codecov Python 3.13+ PyPI License: MIT Docs

A Python library for building efficient network communication pipelines, supporting both REST and WebSocket protocols with built-in rate limiting and time synchronization.

Features

  • ๐Ÿ”„ Dual Protocol Support: Unified factory for REST and WebSocket connections
  • ๐Ÿšฆ Async Rate Limiting: Built-in throttler with multiple concurrent limits and weighted consumption
  • โฐ Time Synchronization: Rolling-window server time alignment for timestamp-sensitive APIs
  • ๐Ÿญ Factory Pattern: ConnectionsFactory and ConnectionManagersFactory for clean lifecycle management
  • ๐Ÿ”Œ Processor Pipeline: Composable pre/post processors for request transformation and response handling
  • ๐Ÿ›ก๏ธ Authentication: Abstract Auth base class supporting any scheme (API key, OAuth, HMAC, JWT)
  • ๐Ÿ”’ Type Safe: Fully typed with MyPy strict mode
  • ๐Ÿงช Well Tested: Comprehensive test suite with high coverage

Installation

# Using pip
pip install networkpype

# Using uv
uv add networkpype

Quick Start

import asyncio
from decimal import Decimal

from networkpype.factory import ConnectionManagersFactory
from networkpype.rest.method import RESTMethod
from networkpype.throttler.rate_limit import RateLimit
from networkpype.throttler.throttler import AsyncThrottler


async def main():
    throttler = AsyncThrottler(
        rate_limits=[RateLimit(limit_id="default", limit=10, time_interval=1.0)]
    )
    factory = ConnectionManagersFactory(throttler=throttler)

    manager = await factory.get_rest_manager()
    data = await manager.execute_request(
        url="https://api.example.com/v1/data",
        throttler_limit_id="default",
        method=RESTMethod.GET,
    )
    print(data)

    await factory.close()


asyncio.run(main())

Core Components

  • Factory: Entry point for creating connections

    • ConnectionsFactory: Low-level aiohttp session management
    • ConnectionManagersFactory: High-level managers with auth, throttling, and processors
  • REST: Full HTTP pipeline

    • RESTConnection: Raw aiohttp wrapper
    • RESTManager: Orchestrates throttling, auth, and pre/post processors
    • RESTRequest / RESTResponse: Type-safe request and response containers
  • WebSocket: Persistent connection management

    • WebSocketConnection: Low-level connection with ping/pong handling
    • WebSocketManager: Message pipeline with auth and processors
    • WebSocketJSONRequest / WebSocketPlainTextRequest: Typed message types
  • Throttler: Async rate limiting

    • AsyncThrottler: Multi-limit, weighted rate limiter
    • RateLimit: Declarative limit configuration with linked limits
    • AsyncRequestContext: Context manager for acquiring capacity
  • Auth: Authentication framework

    • Auth: Abstract base โ€” subclass to implement any authentication scheme
  • TimeSynchronizer: Server time alignment

    • Rolling-window offset with median + weighted-average calculation
ConnectionManagersFactory
โ”œโ”€โ”€ RESTManager
โ”‚   โ”œโ”€โ”€ RESTPreProcessor[]   โ”€โ”€ modify requests before sending
โ”‚   โ”œโ”€โ”€ Auth                 โ”€โ”€ sign authenticated requests
โ”‚   โ”œโ”€โ”€ RESTConnection       โ”€โ”€ execute HTTP via aiohttp
โ”‚   โ””โ”€โ”€ RESTPostProcessor[]  โ”€โ”€ transform responses
โ””โ”€โ”€ WebSocketManager
    โ”œโ”€โ”€ WebSocketPreProcessor[]  โ”€โ”€ transform outgoing messages
    โ”œโ”€โ”€ Auth                     โ”€โ”€ sign authenticated messages
    โ”œโ”€โ”€ WebSocketConnection      โ”€โ”€ manage WS lifecycle
    โ””โ”€โ”€ WebSocketPostProcessor[] โ”€โ”€ process incoming messages

AsyncThrottler
โ””โ”€โ”€ RateLimit[]  โ”€โ”€ multiple windows with linked-limit support

Documentation

Full documentation is available at gianlucapagliara.github.io/networkpype.

Development

NetworkPype uses uv for dependency management and packaging:

# Install dependencies
uv sync

# Run tests
uv run pytest

# Run type checks
uv run mypy networkpype

# Run linting
uv run ruff check .

# Run pre-commit hooks
uv run pre-commit run --all-files

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

networkpype-1.1.0.tar.gz (128.5 kB view details)

Uploaded Source

Built Distribution

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

networkpype-1.1.0-py3-none-any.whl (39.2 kB view details)

Uploaded Python 3

File details

Details for the file networkpype-1.1.0.tar.gz.

File metadata

  • Download URL: networkpype-1.1.0.tar.gz
  • Upload date:
  • Size: 128.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for networkpype-1.1.0.tar.gz
Algorithm Hash digest
SHA256 7db1d2e319b895fc67e4b2ef3c4fdfbe4135b61282bdcc911a2c7f6cfac2b81c
MD5 b5289c96e4abb1eecd53e33caf2338b0
BLAKE2b-256 46a25c34f592ed5cf71ceb188338266daa0cad5942cba4c084bf91ce8b60cd16

See more details on using hashes here.

File details

Details for the file networkpype-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: networkpype-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 39.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for networkpype-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1d67c8d81f859fc8600928eb5a40b4b23b83735b088c36f0e27b63af475f25cb
MD5 0fcc1113c5c2b2cf58dd3d3bd7276c89
BLAKE2b-256 581cd96da5432c539d79a667c62b4696cfe60e479b1572b32578938e58e7ca14

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