Skip to main content

Core utilities for building OAuth-based integrations in FastAPI applications with use of NoSql db

Project description

NoSQL Integration Core

A comprehensive library for building OAuth-based integration services with NoSQL databases in FastAPI applications.

Overview

NoSQL Integration Core extends the base Integration Core library to provide specialized support for NoSQL databases like MongoDB. It simplifies the process of managing OAuth tokens, API credentials, and integration state while leveraging the flexibility and scalability of NoSQL databases.

Installation

# Using pip
pip install abs-nosql-integration-core

# Using Poetry
poetry add abs-nosql-integration-core

Features

  • Complete OAuth 2.0 flow management
  • Secure token storage with encryption
  • Automatic token refresh handling
  • NoSQL database integration with Beanie ODM
  • Asynchronous API with FastAPI compatibility
  • Built-in MongoDB integration

Architecture

The package follows a modular architecture:

  • Repository Layer: Handles persistence with NoSQL databases
  • Service Layer: Implements OAuth flows and business logic
  • Models: Defines document models for NoSQL storage
  • Schemas: Defines data models and validation

Usage

Basic Setup

from abs_nosql_integration_core.service import IntegrationBaseService
from abs_nosql_integration_core.repository import IntegrationRepository
from abs_integration_core.utils.encryption import Encryption

# Create an integration service
class MyIntegrationService(IntegrationBaseService):
    def __init__(
        self,
        provider_name: str,
        integration_repository: IntegrationRepository,
        encryption: Encryption
    ):
        super().__init__(provider_name, integration_repository, encryption)
        # Additional provider-specific configuration

Dependency Injection Setup

from dependency_injector import containers, providers
from abs_nosql_integration_core.repository import IntegrationRepository
from abs_integration_core.utils.encryption import Encryption

class Container(containers.DeclarativeContainer):
    # MongoDB initialization
    mongodb_client = providers.Singleton(
        MongoClient, 
        connection_string=settings.MONGODB_CONNECTION_STRING
    )
    
    # Encryption service
    encryption = providers.Singleton(
        Encryption,
        secret_key=settings.ENCRYPTION_KEY
    )
    
    # Repository layer
    integration_repository = providers.Singleton(IntegrationRepository)
    
    # Service layer
    my_integration_service = providers.Singleton(
        MyIntegrationService,
        provider_name="my_provider",
        integration_repository=integration_repository,
        encryption=encryption
    )

FastAPI Integration

from fastapi import Depends, FastAPI, APIRouter
from dependency_injector.wiring import inject, Provide

router = APIRouter()

@router.get("/connect")
async def connect(
    service: IntegrationBaseService = Depends(get_integration_service),
):
    auth_url = service.get_auth_url()
    return {"auth_url": auth_url}

@router.get("/callback")
async def callback(
    code: str,
    service: IntegrationBaseService = Depends(get_integration_service),
):
    token_data = await service.handle_oauth_callback(code)
    return {"status": "connected"}

Document Schema

The integration document model includes:

class IntegrationDocument(BaseDraftDocument):
    provider_name: str  # Name of the integration provider
    access_token: str   # OAuth access token (encrypted)
    refresh_token: str  # OAuth refresh token (encrypted)
    expires_at: datetime  # Token expiration date

Repository Methods

The IntegrationRepository class provides:

  • create_integration: Create a new integration record
  • refresh_token: Update access and refresh tokens
  • get_query_by_provider: Find an integration by provider name
  • get_integration: Get a single integration by provider
  • get_all_integrations: Get all integrations
  • delete_integration: Remove an integration

Service Methods

The IntegrationBaseService class provides:

  • get_auth_url: Generate OAuth authorization URL
  • handle_oauth_callback: Process OAuth callback and store tokens
  • refresh_token: Refresh expired access tokens
  • get_integration: Get current integration details
  • delete_integration: Remove integration and related tokens

Exception Handling

The library uses a standard exception hierarchy:

  • NotFoundError: Integration not found
  • BadRequestError: Invalid request parameters
  • DuplicatedError: Integration already exists
  • UnauthorizedError: Authentication issues

MongoDB Configuration

The package supports MongoDB through Beanie ODM:

from beanie import init_beanie
from motor.motor_asyncio import AsyncIOMotorClient
from abs_nosql_integration_core.model import IntegrationDocument

async def init_mongodb():
    # Create Motor client
    client = AsyncIOMotorClient(MONGODB_URL)
    
    # Initialize Beanie with document models
    await init_beanie(
        database=client.db_name,
        document_models=[IntegrationDocument]
    )

Best Practices

  • Always encrypt sensitive tokens using the provided encryption utilities
  • Use dependency injection for better testability
  • Implement proper error handling for OAuth edge cases
  • Consider rate limiting for API calls
  • Implement proper logging for debugging
  • Use async context managers for database operations

Contributing

Contributions are welcome! Please feel free to submit pull requests.

License

This project is licensed under the MIT License.

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

abs_nosql_integration_core-0.1.11.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

abs_nosql_integration_core-0.1.11-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file abs_nosql_integration_core-0.1.11.tar.gz.

File metadata

  • Download URL: abs_nosql_integration_core-0.1.11.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.9 Darwin/23.6.0

File hashes

Hashes for abs_nosql_integration_core-0.1.11.tar.gz
Algorithm Hash digest
SHA256 78234bfccecc1aa3d75c324a03825f3acc42e2b39854699a8e6281487faf83ac
MD5 ba778a526ddf7c9cefb140a1ea647c3c
BLAKE2b-256 2d8a4be5733139507a8e9d535534b9476c60534678ac54435f6f45e5e35fa9b4

See more details on using hashes here.

File details

Details for the file abs_nosql_integration_core-0.1.11-py3-none-any.whl.

File metadata

File hashes

Hashes for abs_nosql_integration_core-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 7cba592cd0cf7e7483d1b7c59ace6f53bd6c267c449de4ef57be6bc1ddba5ea5
MD5 207aae5b385f5f5fc27906d851d7739c
BLAKE2b-256 158ee7f2a098f67595faadc3f0e7a351143174bd04da2958d19c059127371582

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