Skip to main content

A lightweight, production-ready asynchronous HTTP client.

Project description

Enhanced HTTP Client

License: MIT
Python Version

Enhanced HTTP Client is a lightweight, production-ready library for handling asynchronous HTTP and WebSocket connections. Built using Python’s standard libraries, it features connection pooling, caching, retries, WebSocket communication, and advanced middleware support.


Features

  • 🌐 HTTP 1.1 Support: Efficient HTTP client built on asyncio.
  • 📡 WebSocket Communication: Send and receive messages with ping/pong handling.
  • 🔁 Retries with Exponential Backoff: Robust handling of transient errors.
  • 🚀 Connection Pooling: Reuse connections to reduce latency.
  • 💾 In-Memory Caching: Key-value caching with configurable TTL.
  • 🔧 Pluggable Middleware: Add custom request/response handlers.
  • 📊 Metrics: Track requests, retries, and failures for better insights.
  • 🔒 Secure Defaults: TLS/SSL support for HTTPS and WebSocket communication.

Installation

With pip

pip install enhanced-http

Manual Installation

  1. Clone the Repository
git clone https://github.com/yourusername/enhanced-http.git
cd enhanced-http
  1. Install dependencies
pip install -r requirements.txt

Usage

HTTP Client

from enhanced_http.client import EnhancedHttpClient
import asyncio

async def main():
    client = EnhancedHttpClient()
    
    # Basic GET request
    response = await client.request("GET", "https://httpbin.org/get")
    print(response)  # {'status_code': 200, 'headers': {...}, 'body': {...}}
    
    # POST request with JSON body
    response = await client.request(
        "POST",
        "https://httpbin.org/post",
        headers={"Content-Type": "application/json"},
        body={"key": "value"}
    )
    print(response)
    
    await client.close()  # Close all connections

asyncio.run(main())

WebSocket Client

from enhanced_http.websocket import WebSocketClient
import asyncio

async def main():
    ws_client = WebSocketClient()
    ws_conn = await ws_client.connect("wss://echo.websocket.org")
    
    # Send a message
    await ws_conn.send("Hello, WebSocket!")
    
    # Receive a message
    response = await ws_conn.receive()
    print(response)  # Should echo "Hello, WebSocket!"
    
    await ws_conn.close()

asyncio.run(main())

Caching

from enhanced_http.cache import InMemoryCache
import asyncio

async def main():
    cache = InMemoryCache()
    
    # Set a value with TTL
    await cache.set("key1", "value1", ttl=5)
    
    # Get the cached value
    value = await cache.get("key1")
    print(value)  # "value1"
    
    # Wait for the TTL to expire
    await asyncio.sleep(6)
    value = await cache.get("key1")
    print(value)  # None

asyncio.run(main())

Middleware

Features of the Robust Middleware

1. Prioritized Middleware

Middleware can now be assigned a priority (lower numbers execute first). For example:

client.middleware.add_request_middleware(logging_middleware_request, priority=50)
client.middleware.add_request_middleware(add_default_headers_middleware, priority=10)

2. Prebuilt Middleware

Logging Middleware: Logs requests and responses. Exception Handling Middleware: Catches and processes exceptions during middleware execution. Default Headers Middleware: Adds default headers like User-Agent.

3. Synchronous and Asynchronous Middleware Support

Both synchronous and asynchronous middleware functions are supported.

4. Flexible Chains

Request and response middleware are managed separately, so you can preprocess requests and post-process responses independently.

Example Usage

from enhanced_http.middleware import MiddlewareManager
import asyncio

async def log_request(method, url, headers, body):
    print(f"Request: {method} {url}")
    return method, url, headers, body

async def log_response(response):
    print(f"Response: {response['status_code']}")
    return response

async def main():
    # Initialize middleware
    middleware = MiddlewareManager()
    middleware.add_request_middleware(log_request)
    middleware.add_response_middleware(log_response)

    # Use middleware with an HTTP client
    client = EnhancedHttpClient()
    response = await client.request("GET", "https://httpbin.org/get")
    await client.close()

asyncio.run(main())

Advanced Features

Retires with Exponential Backoff

Handle transient errors like timeouts with automatic retries.

async def main():
    client = EnhancedHttpClient(max_retries=3, backoff_factor=0.5)
    
    # Will retry on failure
    response = await client.request("GET", "https://httpbin.org/status/500")
    print(response)
    
    await client.close()

asyncio.run(main())

Metrics

Track the number of requests, retries, and failures.

client = EnhancedHttpClient()
metrics = client.get_metrics()
print(metrics)  # {'requests': 10, 'retries': 2, 'failures': 1}

Development

Running Tests

  1. Install development dependencies:
pip install pytest pytest-asyncio
  1. Run the tests:
pytest enhanced_http/tests/

Code Formatting

This project uses black for code formatting:

pip install black
black .

Roadmap

Here are the planned features and enhancements for the Enhanced HTTP Client:

  • Add HTTP/2 and HTTP/3 support
  • Implement exponential backoff and retry logic
  • Add distributed caching support (e.g., Redis backend)
  • Support circuit breaker middleware
  • Integrate request tracing with OpenTelemetry
  • Enhance logging and monitoring capabilities
  • Add prebuilt authentication middleware (e.g., OAuth2, API keys)
  • Provide detailed documentation and tutorials

Want to see more features? Submit a feature request or vote on existing ones in the issues.


Contributing

We welcome contributions to Enhanced HTTP! Here’s how you can help:

  1. Fork the repository.
  2. Create a new branch:
   git checkout -b feature-name
  1. Make your changes and commit:
git commit -m "Description of changes"
  1. Push to your fork:
git push origin feature-name
  1. Create a pull request. Need help? Open an issue or contact us at dgoss@treytek.com
Feel free to copy and paste this into your `README.md` file! Let me know if youd like further refinements.

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

enhanced_http-1.0.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

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

enhanced_http-1.0.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file enhanced_http-1.0.0.tar.gz.

File metadata

  • Download URL: enhanced_http-1.0.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for enhanced_http-1.0.0.tar.gz
Algorithm Hash digest
SHA256 10546c480dd40d755be95490017cfd381251ed99d55d415113891223bf07ece0
MD5 c61d7311d6fe8cd247ee6bd0f77bfe89
BLAKE2b-256 a04bdc332fec2e0e056f76c8a9bda6c73a1b4b2c163041ef2e2d13087f1ee445

See more details on using hashes here.

File details

Details for the file enhanced_http-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: enhanced_http-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for enhanced_http-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 657b899da33140253e463569f7be56b230df969d0f588b1df2044a041c2afdaa
MD5 dbc2ab6580b266a984e405573cdce653
BLAKE2b-256 65b342f63d9a5356742bf564d349ab1065c55f566696041668f383f5cf9e170f

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