Skip to main content

Core utilities and models for FireFeed microservices

Project description

FireFeed Core

Core utilities and models for FireFeed microservices architecture.

FireFeed Core is a shared library that provides common components for all FireFeed microservices, ensuring consistency, security, and reliability across the distributed system.

๐Ÿš€ Features

  • API Client: Robust HTTP client with authentication, retry policies, circuit breaker, and rate limiting
  • JWT Authentication: Service-to-service authentication with token management
  • Exception Handling: Comprehensive exception hierarchy for consistent error handling
  • Configuration: Pydantic-based configuration management
  • Interfaces: Abstract interfaces for service contracts
  • Utilities: Common utility functions and helpers

๐Ÿ“ฆ Installation

pip install firefeed-core

๐Ÿ”ง Quick Start

Basic API Client Usage

import asyncio
from firefeed_core import APIClient

async def main():
    # Initialize API client
    async with APIClient(
        base_url="http://firefeed-api:8000",
        token="your-jwt-token",
        service_id="firefeed-rss-parser"
    ) as client:
        
        # Make authenticated requests
        feeds = await client.get("/api/v1/internal/rss/feeds")
        print(f"Found {len(feeds)} feeds")
        
        # Create new RSS item
        new_item = await client.post("/api/v1/internal/rss/items", {
            "title": "Test Article",
            "content": "This is a test article",
            "feed_id": 123
        })

asyncio.run(main())

JWT Token Management

from firefeed_core import ServiceTokenManager

# Generate token for service
token_manager = ServiceTokenManager(
    secret_key="your-secret-key",
    issuer="firefeed-api"
)

token = token_manager.generate_service_token(
    service_id="firefeed-rss-parser",
    audience="firefeed-api",
    scopes=["rss:read", "rss:write"]
)

# Verify token
payload = token_manager.verify_token(token)
print(f"Token for service: {payload.sub}")

Exception Handling

from firefeed_core import APIException, NotFoundException

try:
    result = await client.get("/api/v1/internal/rss/feeds/999")
except NotFoundException as e:
    print(f"Feed not found: {e.message}")
except APIException as e:
    print(f"API error: {e.message}")

๐Ÿ” Authentication

FireFeed Core uses JWT tokens for service-to-service authentication. Each service needs:

  1. Service Token: JWT token with appropriate scopes
  2. Service ID: Unique identifier for the service
  3. Secret Key: Shared secret for token verification

Environment Configuration

Each service should have its own .env file:

# firefeed-rss-parser/.env
FIREFEED_API_URL=http://firefeed-api:8000
FIREFEED_API_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
FIREFEED_RSS_PARSER_SERVICE_ID=rss-parser

# firefeed-telegram-bot/.env
FIREFEED_API_URL=http://firefeed-api:8000
FIREFEED_API_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
FIREFEED_TELEGRAM_BOT_SERVICE_ID=telegram-bot

Token Scopes

Services can be granted specific scopes for different operations:

  • rss:read - Read RSS data
  • rss:write - Create/modify RSS data
  • users:read - Read user data
  • users:write - Modify user data
  • categories:read - Read categories
  • categories:write - Modify categories

๐Ÿ› ๏ธ Advanced Features

Circuit Breaker

Automatically prevents requests to failing services:

from firefeed_core import APIClient

client = APIClient(
    base_url="http://service:8000",
    token="token",
    service_id="my-service",
    circuit_breaker_failure_threshold=5,
    circuit_breaker_timeout=60
)

Retry Policies

Configurable retry with exponential backoff:

client = APIClient(
    base_url="http://service:8000",
    token="token",
    service_id="my-service",
    max_retries=3
)

Rate Limiting

Prevents API abuse:

client = APIClient(
    base_url="http://service:8000",
    token="token",
    service_id="my-service",
    rate_limit_requests=100,
    rate_limit_window=60
)

๐Ÿ“Š Monitoring

Each API client provides comprehensive statistics:

stats = client.get_stats()
print(f"Circuit breaker state: {stats['circuit_breaker']['state']}")
print(f"Rate limit usage: {stats['rate_limiter']['current_requests']}")

๐Ÿ—๏ธ Architecture

FireFeed Core follows these principles:

  1. API-First: All services communicate via HTTP APIs only
  2. Zero Direct Database Access: Services never access databases directly
  3. Token-Based Security: All inter-service communication is authenticated
  4. Fault Tolerance: Circuit breakers, retries, and rate limiting
  5. Consistent Error Handling: Standardized exception hierarchy

Service Communication Flow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    JWT Token    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  firefeed-rss-parserโ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ โ”‚   firefeed-api      โ”‚
โ”‚  (Dumb Service)     โ”‚                 โ”‚  (Smart Service)    โ”‚
โ”‚                     โ”‚ โ†โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚                     โ”‚
โ”‚  - RSS Processing   โ”‚    HTTP API     โ”‚  - Database Access  โ”‚
โ”‚  - No DB Access     โ”‚                 โ”‚  - Business Logic   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿงช Testing

# Install test dependencies
pip install -e ".[test]"

# Run tests
pytest

# Run with coverage
pytest --cov=firefeed_core

๐Ÿ“ Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐Ÿ”— Related Projects


Note: This is a core library intended for use within the FireFeed microservices ecosystem.

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

firefeed_core-1.0.2.tar.gz (108.0 kB view details)

Uploaded Source

Built Distribution

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

firefeed_core-1.0.2-py3-none-any.whl (135.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: firefeed_core-1.0.2.tar.gz
  • Upload date:
  • Size: 108.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for firefeed_core-1.0.2.tar.gz
Algorithm Hash digest
SHA256 93fd3834f0b5e5f365d95182a87c4130d31447aca76e7cf65ea53be426d49d2c
MD5 84c1ff4f71bcdc94e3737832ee7d95ec
BLAKE2b-256 f018b5dd396d52a0bb0f1f9bf978d067fbc81904eb9083cd2c43d36c3f291769

See more details on using hashes here.

Provenance

The following attestation bundles were made for firefeed_core-1.0.2.tar.gz:

Publisher: release.yml on firefeed-net/firefeed-core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: firefeed_core-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 135.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for firefeed_core-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 803159ee37d6d3646d4a02a70ae40f14211de65a264a4a17f711bae3e821d3c6
MD5 7b2fb9f396d503402375de435df7c2b8
BLAKE2b-256 5854707e541afcb44c186daa8c1157d895d1d0ebf53208dbf22af2d6d096477d

See more details on using hashes here.

Provenance

The following attestation bundles were made for firefeed_core-1.0.2-py3-none-any.whl:

Publisher: release.yml on firefeed-net/firefeed-core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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