Skip to main content

Enhanced connector libraries with caching, retry, rate limiting, and async support

Project description

Enhanced Connectors Package

A comprehensive, production-ready Python package for integrating with various services and APIs. This enhanced version includes advanced features like caching, retry mechanisms, rate limiting, async support, and unified interfaces.

๐Ÿš€ Features

Core Features

  • Multiple Service Support: Confluence and Jira connectors with extensible architecture
  • Unified Interface: Common API for cross-platform operations
  • Async/Await Support: Non-blocking operations for high-performance applications
  • Comprehensive Error Handling: Detailed exception hierarchy with context
  • Advanced Logging: Structured logging with performance monitoring

Advanced Features

  • Multi-Level Caching: Memory and file-based caching with TTL and LRU eviction
  • Intelligent Retry: Exponential backoff with jitter and circuit breaker patterns
  • Rate Limiting: Token bucket and sliding window algorithms
  • Configuration Management: Environment-based config with validation
  • Batch Operations: Efficient processing of multiple items
  • Performance Monitoring: Built-in metrics and timing

๐Ÿ“ฆ Installation

pip install connectors

For development with all dependencies:

pip install connectors[dev,docs,test]

๐Ÿ”ง Quick Start

Basic Usage

from connectors import ConfluenceConnector, ConfluenceOperations

# Initialize connector
confluence = ConfluenceConnector(
    confluence_url="https://your-confluence.atlassian.net",
    token="your-api-token",
    cloud=True
)

# Initialize operations
ops = ConfluenceOperations(confluence)

# Read a page
page_data = ops.read_page("12345")
if page_data["success"]:
    print(f"Title: {page_data['metadata']['title']}")
    print(f"Content: {page_data['content'][:100]}...")

# Close connection
confluence.close()

Configuration Management

from connectors.config import get_config_manager

# Load from environment variables
config_manager = get_config_manager()

# Or load from file
config_manager = get_config_manager("config.yaml")

# Get validated configuration
confluence_config = config_manager.get_confluence_config()
jira_config = config_manager.get_jira_config()

Enhanced Features

from connectors import ConfluenceConnector
from connectors.cache import MultiLevelCache
from connectors.retry import retry
from connectors.rate_limiter import rate_limit

# Setup caching
cache = MultiLevelCache(memory_size=1000, cache_dir="./cache")

# Setup connector with caching
confluence = ConfluenceConnector(
    confluence_url="https://your-confluence.atlassian.net",
    token="your-api-token",
    cloud=True
)

# Use with retry and rate limiting
@retry(max_attempts=3, base_delay=1.0)
@rate_limit(requests_per_period=10, period_seconds=60)
def get_page_with_retry(page_id):
    return confluence.get_page_by_id(page_id)

Async Operations

import asyncio
from connectors.async_client import AsyncConfluenceConnector

async def main():
    # Create async client
    async_client = await create_async_client(
        base_url="https://your-confluence.atlassian.net",
        auth_headers={"Authorization": "Bearer your-token"}
    )
    
    # Use async operations
    page_data = await async_client.get("/rest/api/content/12345")
    print(f"Page title: {page_data['title']}")
    
    # Batch processing
    from connectors.async_client import AsyncBatchProcessor
    
    processor = AsyncBatchProcessor(batch_size=10, max_concurrent=5)
    page_ids = ["12345", "12346", "12347"]
    
    results = await processor.process_batch(
        page_ids,
        lambda pid: async_client.get(f"/rest/api/content/{pid}")
    )
    
    await async_client.close()

asyncio.run(main())

Unified Interface

from connectors.unified_interface import (
    UnifiedConnectorFactory, UnifiedContent, ContentType
)

# Create unified connectors
confluence = UnifiedConnectorFactory.create_confluence_connector(
    confluence_connector, confluence_operations
)
jira = UnifiedConnectorFactory.create_jira_connector(
    jira_connector, jira_operations
)

# Use common interface
content = await confluence.get_content("12345", ContentType.PAGE)
print(f"Title: {content.title}")

# Search across platforms
from connectors.unified_interface import create_search_request

search_request = create_search_request(
    query="Redis",
    content_types=["page", "issue"],
    limit=10
)

confluence_results = await confluence.search_content(search_request)
jira_results = await jira.search_content(search_request)

๐Ÿ“š Configuration

Environment Variables

# Common settings
CONNECTORS_LOG_LEVEL=INFO
CONNECTORS_TIMEOUT=30
CONNECTORS_MAX_RETRIES=3
CONNECTORS_ENABLE_CACHE=true
CONNECTORS_CACHE_TTL=300

# Confluence settings
CONFLUENCE_URL=https://your-confluence.atlassian.net
CONFLUENCE_TOKEN=your-api-token
CONFLUENCE_CLOUD=true

# Jira settings
JIRA_URL=https://your-jira.atlassian.net
JIRA_TOKEN=your-api-token
JIRA_USERNAME=your-username

Configuration File

Create a config.yaml file:

log_level: INFO
timeout: 30
max_retries: 3
enable_cache: true
cache_ttl: 300

confluence:
  url: https://your-confluence.atlassian.net
  token: ${CONFLUENCE_TOKEN}
  cloud: true
  enable_ocr: true
  tesseract_lang: eng

jira:
  url: https://your-jira.atlassian.net
  token: ${JIRA_TOKEN}
  api_version: 3
  enable_agile: true

Sample Configuration Generator

from connectors.config import create_sample_config

# Create sample configuration
create_sample_config("sample_config.yaml", "yaml")
create_sample_config("sample_config.json", "json")

๐Ÿ” Advanced Usage

Caching Strategies

from connectors.cache import MemoryCache, FileCache, MultiLevelCache, cached

# Memory cache with TTL
memory_cache = MemoryCache(max_size=1000, default_ttl=300)

# File cache with persistence
file_cache = FileCache("/tmp/connectors_cache", max_size=10000)

# Multi-level cache
cache = MultiLevelCache(
    memory_size=1000,
    file_cache_dir="/tmp/cache",
    file_cache_size=10000
)

# Use caching decorator
@cached(cache, ttl=600)
def expensive_operation(page_id):
    # Complex operation that benefits from caching
    return confluence.get_page_by_id(page_id)

Retry Strategies

from connectors.retry import (
    retry, RetryConfig, ExponentialBackoffStrategy,
    AdaptiveRetryStrategy, RetryCircuitBreaker
)

# Basic retry with exponential backoff
@retry(max_attempts=5, base_delay=1.0, jitter=True)
def unreliable_operation():
    pass

# Custom retry strategy
strategy = AdaptiveRetryStrategy(RetryConfig(max_attempts=3))
@retry(strategy=strategy)
def adaptive_operation():
    pass

# Circuit breaker pattern
circuit_breaker = RetryCircuitBreaker(failure_threshold=5, recovery_timeout=60.0)

@circuit_breaker
def critical_operation():
    pass

Rate Limiting

from connectors.rate_limiter import (
    RateLimiter, RateLimitConfig, MultiRateLimiter,
    rate_limit, register_rate_limiter
)

# Token bucket rate limiter
config = RateLimitConfig(requests_per_period=10, period_seconds=60)
limiter = RateLimiter(config, algorithm="token_bucket")

# Multiple rate limits
multi_limiter = MultiRateLimiter({
    "per_second": RateLimitConfig(5, 1.0),
    "per_minute": RateLimitConfig(100, 60.0),
    "per_hour": RateLimitConfig(1000, 3600.0)
})

# Use with decorator
@rate_limit(requests_per_period=10, period_seconds=60)
def limited_operation():
    pass

# Global rate limiter
register_rate_limiter("api_calls", RateLimitConfig(100, 60))

Logging and Monitoring

from connectors.logging_config import (
    setup_logging, setup_connector_logging,
    log_performance, log_security_event
)

# Setup structured logging
setup_logging(
    level="INFO",
    log_file="connectors.log",
    structured=True,
    console=True
)

# Connector-specific logging
logger = setup_connector_logging("confluence", "DEBUG")

# Performance monitoring
@log_performance
def monitored_operation():
    pass

# Security event logging
log_security_event(
    "api_access",
    user="john.doe",
    resource="page:12345",
    details={"action": "read"}
)

๐Ÿงช Testing

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=connectors --cov-report=html

# Run specific test categories
pytest -m unit
pytest -m integration
pytest -m slow

Test Configuration

# tests/conftest.py
import pytest
from connectors import ConfluenceConnector

@pytest.fixture
def mock_confluence():
    return ConfluenceConnector(
        confluence_url="https://test.confluence.com",
        token="test-token",
        cloud=True
    )

@pytest.fixture
def test_config():
    return {
        "confluence_url": "https://test.confluence.com",
        "token": "test-token",
        "timeout": 5,
        "max_retries": 1
    }

๐Ÿ“– Examples

Complete Example with All Features

import asyncio
from connectors import ConfluenceConnector, ConfluenceOperations
from connectors.config import get_config_manager
from connectors.cache import MultiLevelCache
from connectors.retry import retry
from connectors.rate_limiter import rate_limit
from connectors.logging_config import setup_logging
from connectors.unified_interface import UnifiedConnectorFactory

# Setup
setup_logging(level="INFO", structured=True, log_file="app.log")
config = get_config_manager()
cache = MultiLevelCache(memory_size=500, cache_dir="./cache")

# Initialize connector
confluence_config = config.get_confluence_config()
confluence = ConfluenceConnector(
    confluence_url=confluence_config.url,
    token=confluence_config.token,
    cloud=confluence_config.cloud
)

ops = ConfluenceOperations(confluence)

# Enhanced operations
@retry(max_attempts=3, base_delay=1.0)
@rate_limit(requests_per_period=10, period_seconds=60)
def get_page_with_enhancements(page_id):
    return ops.read_page(page_id)

# Unified interface
unified = UnifiedConnectorFactory.create_confluence_connector(confluence, ops)

async def main():
    try:
        # Test connection
        if await unified.test_connection():
            print("โœ… Connection successful")
        
        # Get content
        content = await unified.get_content("12345", ContentType.PAGE)
        print(f"๐Ÿ“– {content.title}")
        
        # Search
        from connectors.unified_interface import create_search_request
        search_req = create_search_request("Redis", ["page"], limit=5)
        results = await unified.search_content(search_req)
        print(f"๐Ÿ” Found {len(results.items)} pages")
        
        # Batch operations
        page_ids = ["12345", "12346", "12347"]
        batch_results = await unified.batch_operation(
            OperationType.READ, page_ids
        )
        print(f"๐Ÿ“ฆ Processed {len(batch_results)} pages")
        
    except Exception as e:
        print(f"โŒ Error: {e}")
    finally:
        await unified.close()

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

๐Ÿ—๏ธ Architecture

Package Structure

connectors/
โ”œโ”€โ”€ __init__.py
โ”œโ”€โ”€ exceptions.py          # Exception hierarchy
โ”œโ”€โ”€ logging_config.py      # Logging configuration
โ”œโ”€โ”€ config.py             # Configuration management
โ”œโ”€โ”€ cache.py              # Caching system
โ”œโ”€โ”€ retry.py              # Retry mechanisms
โ”œโ”€โ”€ rate_limiter.py       # Rate limiting
โ”œโ”€โ”€ async_client.py       # Async HTTP client
โ”œโ”€โ”€ unified_interface.py  # Unified API
โ”œโ”€โ”€ confluence_connector/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ confluence_connector.py
โ”‚   โ””โ”€โ”€ operations.py
โ””โ”€โ”€ jira_connector/
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ jira_connector.py
    โ””โ”€โ”€ operations.py

Design Principles

  1. Modularity: Each feature is a separate, composable module
  2. Extensibility: Easy to add new connectors and features
  3. Performance: Async support, caching, and efficient algorithms
  4. Reliability: Comprehensive error handling and retry logic
  5. Observability: Detailed logging and monitoring capabilities
  6. Flexibility: Multiple configuration options and usage patterns

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite (pytest)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

Development Setup

# Clone repository
git clone https://github.com/yourusername/connectors.git
cd connectors

# Install development dependencies
pip install -e .[dev,docs,test]

# Setup pre-commit hooks
pre-commit install

# Run tests
pytest

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ†˜ Support

๐Ÿ—บ๏ธ Roadmap

  • Add more service connectors (GitHub, Slack, etc.)
  • GraphQL support
  • Webhook handling
  • Advanced analytics and reporting
  • Kubernetes integration
  • Plugin system for custom connectors

๐Ÿ“Š Performance

The enhanced connectors package includes several performance optimizations:

  • Caching: Reduces API calls by up to 80% for repeated requests
  • Async Operations: Enables concurrent processing of multiple requests
  • Connection Pooling: Reuses HTTP connections efficiently
  • Rate Limiting: Prevents API throttling and improves reliability
  • Smart Retry: Avoids unnecessary retries for non-recoverable errors

Benchmark results (1000 API calls):

  • Without caching: ~45 seconds
  • With memory caching: ~12 seconds
  • With async + caching: ~3 seconds
  • With full optimization: ~2 seconds

Built with โค๏ธ for the developer community

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

tools_connectors-2.0.0.tar.gz (63.5 kB view details)

Uploaded Source

Built Distribution

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

tools_connectors-2.0.0-py3-none-any.whl (53.4 kB view details)

Uploaded Python 3

File details

Details for the file tools_connectors-2.0.0.tar.gz.

File metadata

  • Download URL: tools_connectors-2.0.0.tar.gz
  • Upload date:
  • Size: 63.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.5

File hashes

Hashes for tools_connectors-2.0.0.tar.gz
Algorithm Hash digest
SHA256 a685faeb7c04478a5fc5707ba4d56b6828d2889d14b52726d8c7d9663710b8cc
MD5 0484238de83329355b2fcfda3d9ca670
BLAKE2b-256 0b8925c38ec4985444dd42d24979f30f346ae6d594735c1a223d34456375ce5b

See more details on using hashes here.

File details

Details for the file tools_connectors-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tools_connectors-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b6e39a8139c3f8e58505d21243b866916598d7d2a67dbbb30b65835d586c5e87
MD5 e491ff3bf74ceae77bbe5f28155ac8df
BLAKE2b-256 5d87b3d93df9bd1e8b9f315d2ed448e3bdd78f7aae99ee5687844f9eb64b922f

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