Skip to content

Reg-Kris/pyairtable-python-shared

Repository files navigation

PyAirtable Python Shared Library

A comprehensive shared library for PyAirtable microservices, providing common functionality and utilities for building robust, scalable Python services.

Features

Core Packages

  • config: Configuration management with pydantic-settings
  • database: SQLAlchemy setup with async support
  • cache: Redis client with async support
  • middleware: FastAPI middleware (auth, logging, metrics, rate limiting)
  • errors: Standardized error handling
  • logger: Structured logging with structlog
  • metrics: Prometheus metrics helpers
  • health: Health check utilities
  • utils: Common utilities

Shared Models

  • User, Tenant, Workspace models: Core domain models
  • Common DTOs and responses: Standardized API responses
  • Pagination helpers: Consistent pagination across services
  • Event schemas: Event-driven architecture support

Client Libraries

  • HTTP clients: For Go services (auth, permission, etc.)
  • Event bus client: Kafka integration
  • Service discovery: Service discovery helpers

Testing Utilities

  • Test database setup: Automated test database management
  • Mock generators: Factory-based test data generation
  • Async test helpers: Utilities for async testing

Requirements

  • Python 3.11+
  • FastAPI
  • SQLAlchemy 2.0+
  • Redis
  • PostgreSQL (for async database support)

Installation

pip install pyairtable-python-shared

For development:

pip install pyairtable-python-shared[test,dev]

Quick Start

Configuration

from pyairtable_shared.config import settings

# Access configuration
print(settings.database_url)
print(settings.redis_url)

Database

from pyairtable_shared.database import get_async_session

async def example():
    async with get_async_session() as session:
        # Use session for database operations
        pass

Cache

from pyairtable_shared.cache import get_redis_client

async def example():
    redis = await get_redis_client()
    await redis.set("key", "value")
    value = await redis.get("key")

FastAPI Middleware

from fastapi import FastAPI
from pyairtable_shared.middleware import (
    add_auth_middleware,
    add_logging_middleware,
    add_metrics_middleware,
    add_rate_limiting_middleware,
)

app = FastAPI()

# Add middleware
add_auth_middleware(app)
add_logging_middleware(app)
add_metrics_middleware(app)
add_rate_limiting_middleware(app)

Logging

from pyairtable_shared.logger import get_logger

logger = get_logger(__name__)

logger.info("Service started", service="my-service")
logger.error("Error occurred", error="details", user_id="123")

Models

from pyairtable_shared.models import User, Tenant, Workspace
from pyairtable_shared.models.dto import UserResponse, PaginatedResponse

# Use shared models
user = User(email="user@example.com", name="John Doe")
response = UserResponse.from_orm(user)

Development

Setup

git clone https://github.com/pyairtable/pyairtable-python-shared.git
cd pyairtable-python-shared
pip install -e .[test,dev]

Testing

# Run all tests
pytest

# Run with coverage
pytest --cov=src/pyairtable_shared

# Run only unit tests
pytest -m unit

# Run only integration tests
pytest -m integration

Code Quality

# Format code
ruff format

# Lint code
ruff check

# Type checking
mypy src/

Pre-commit

pre-commit install
pre-commit run --all-files

Architecture

The library is organized into several packages:

src/pyairtable_shared/
├── __init__.py
├── config/           # Configuration management
├── database/         # Database utilities
├── cache/           # Redis cache utilities
├── middleware/      # FastAPI middleware
├── errors/          # Error handling
├── logger/          # Structured logging
├── metrics/         # Prometheus metrics
├── health/          # Health checks
├── utils/           # Common utilities
├── models/          # Shared models
├── clients/         # HTTP clients
└── testing/         # Test utilities

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make changes with tests
  4. Run the test suite
  5. Submit a pull request

License

MIT License - see LICENSE file for details.

About

Shared Python libraries and utilities for PyAirtable services

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published