|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: MIT-0 |
| 3 | + |
| 4 | +""" |
| 5 | +Document service factory module for IDP Common package. |
| 6 | +
|
| 7 | +This module provides a factory function to create document services based on |
| 8 | +the DOCUMENT_TRACKING_MODE environment variable. It allows switching between |
| 9 | +AppSync and DynamoDB implementations while maintaining the same interface. |
| 10 | +""" |
| 11 | + |
| 12 | +import logging |
| 13 | +import os |
| 14 | +from typing import Optional, Union |
| 15 | + |
| 16 | +from idp_common.appsync import DocumentAppSyncService |
| 17 | +from idp_common.dynamodb import DocumentDynamoDBService |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | +# Supported document tracking modes |
| 22 | +APPSYNC_MODE = "appsync" |
| 23 | +DYNAMODB_MODE = "dynamodb" |
| 24 | +SUPPORTED_MODES = [APPSYNC_MODE, DYNAMODB_MODE] |
| 25 | + |
| 26 | +# Default mode |
| 27 | +DEFAULT_MODE = APPSYNC_MODE |
| 28 | + |
| 29 | + |
| 30 | +class DocumentServiceFactory: |
| 31 | + """ |
| 32 | + Factory class for creating document services based on configuration. |
| 33 | +
|
| 34 | + This factory allows switching between AppSync and DynamoDB implementations |
| 35 | + while maintaining the same interface for document operations. |
| 36 | + """ |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def create_service( |
| 40 | + mode: Optional[str] = None, **kwargs |
| 41 | + ) -> Union[DocumentAppSyncService, DocumentDynamoDBService]: |
| 42 | + """ |
| 43 | + Create a document service based on the specified mode. |
| 44 | +
|
| 45 | + Args: |
| 46 | + mode: Optional mode override. If not provided, uses DOCUMENT_TRACKING_MODE |
| 47 | + environment variable, defaulting to 'appsync' |
| 48 | + **kwargs: Additional arguments passed to the service constructor |
| 49 | +
|
| 50 | + Returns: |
| 51 | + DocumentAppSyncService or DocumentDynamoDBService instance |
| 52 | +
|
| 53 | + Raises: |
| 54 | + ValueError: If an unsupported mode is specified |
| 55 | +
|
| 56 | + Examples: |
| 57 | + # Use environment variable (default behavior) |
| 58 | + service = DocumentServiceFactory.create_service() |
| 59 | +
|
| 60 | + # Override mode explicitly |
| 61 | + service = DocumentServiceFactory.create_service(mode='dynamodb') |
| 62 | +
|
| 63 | + # Pass additional arguments |
| 64 | + service = DocumentServiceFactory.create_service( |
| 65 | + mode='appsync', |
| 66 | + api_url='https://example.appsync-api.us-east-1.amazonaws.com/graphql' |
| 67 | + ) |
| 68 | + """ |
| 69 | + # Determine the mode |
| 70 | + if mode is None: |
| 71 | + mode = os.environ.get("DOCUMENT_TRACKING_MODE", DEFAULT_MODE).lower() |
| 72 | + else: |
| 73 | + mode = mode.lower() |
| 74 | + |
| 75 | + # Validate mode |
| 76 | + if mode not in SUPPORTED_MODES: |
| 77 | + raise ValueError( |
| 78 | + f"Unsupported document tracking mode: '{mode}'. " |
| 79 | + f"Supported modes are: {', '.join(SUPPORTED_MODES)}" |
| 80 | + ) |
| 81 | + |
| 82 | + logger.info(f"Creating document service with mode: {mode}") |
| 83 | + |
| 84 | + # Create the appropriate service |
| 85 | + if mode == APPSYNC_MODE: |
| 86 | + return DocumentAppSyncService(**kwargs) |
| 87 | + elif mode == DYNAMODB_MODE: |
| 88 | + return DocumentDynamoDBService(**kwargs) |
| 89 | + else: |
| 90 | + # This should never happen due to validation above, but included for completeness |
| 91 | + raise ValueError(f"Unsupported mode: {mode}") |
| 92 | + |
| 93 | + @staticmethod |
| 94 | + def get_current_mode() -> str: |
| 95 | + """ |
| 96 | + Get the current document tracking mode from environment variable. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + Current mode string ('appsync' or 'dynamodb') |
| 100 | + """ |
| 101 | + return os.environ.get("DOCUMENT_TRACKING_MODE", DEFAULT_MODE).lower() |
| 102 | + |
| 103 | + @staticmethod |
| 104 | + def is_appsync_mode() -> bool: |
| 105 | + """ |
| 106 | + Check if current mode is AppSync. |
| 107 | +
|
| 108 | + Returns: |
| 109 | + True if current mode is AppSync, False otherwise |
| 110 | + """ |
| 111 | + return DocumentServiceFactory.get_current_mode() == APPSYNC_MODE |
| 112 | + |
| 113 | + @staticmethod |
| 114 | + def is_dynamodb_mode() -> bool: |
| 115 | + """ |
| 116 | + Check if current mode is DynamoDB. |
| 117 | +
|
| 118 | + Returns: |
| 119 | + True if current mode is DynamoDB, False otherwise |
| 120 | + """ |
| 121 | + return DocumentServiceFactory.get_current_mode() == DYNAMODB_MODE |
| 122 | + |
| 123 | + |
| 124 | +# Convenience function for creating services |
| 125 | +def create_document_service( |
| 126 | + mode: Optional[str] = None, **kwargs |
| 127 | +) -> Union[DocumentAppSyncService, DocumentDynamoDBService]: |
| 128 | + """ |
| 129 | + Convenience function to create a document service. |
| 130 | +
|
| 131 | + This is a shorthand for DocumentServiceFactory.create_service(). |
| 132 | +
|
| 133 | + Args: |
| 134 | + mode: Optional mode override. If not provided, uses DOCUMENT_TRACKING_MODE |
| 135 | + environment variable, defaulting to 'appsync' |
| 136 | + **kwargs: Additional arguments passed to the service constructor |
| 137 | +
|
| 138 | + Returns: |
| 139 | + DocumentAppSyncService or DocumentDynamoDBService instance |
| 140 | +
|
| 141 | + Examples: |
| 142 | + # Simple usage |
| 143 | + service = create_document_service() |
| 144 | +
|
| 145 | + # With mode override |
| 146 | + service = create_document_service(mode='dynamodb') |
| 147 | +
|
| 148 | + # With additional parameters |
| 149 | + service = create_document_service( |
| 150 | + mode='appsync', |
| 151 | + api_url='https://example.appsync-api.us-east-1.amazonaws.com/graphql' |
| 152 | + ) |
| 153 | + """ |
| 154 | + return DocumentServiceFactory.create_service(mode=mode, **kwargs) |
| 155 | + |
| 156 | + |
| 157 | +# Convenience functions for mode checking |
| 158 | +def get_document_tracking_mode() -> str: |
| 159 | + """Get the current document tracking mode.""" |
| 160 | + return DocumentServiceFactory.get_current_mode() |
| 161 | + |
| 162 | + |
| 163 | +def is_appsync_mode() -> bool: |
| 164 | + """Check if current mode is AppSync.""" |
| 165 | + return DocumentServiceFactory.is_appsync_mode() |
| 166 | + |
| 167 | + |
| 168 | +def is_dynamodb_mode() -> bool: |
| 169 | + """Check if current mode is DynamoDB.""" |
| 170 | + return DocumentServiceFactory.is_dynamodb_mode() |
| 171 | + |
| 172 | + |
| 173 | +__all__ = [ |
| 174 | + "DocumentServiceFactory", |
| 175 | + "create_document_service", |
| 176 | + "get_document_tracking_mode", |
| 177 | + "is_appsync_mode", |
| 178 | + "is_dynamodb_mode", |
| 179 | + "APPSYNC_MODE", |
| 180 | + "DYNAMODB_MODE", |
| 181 | + "DEFAULT_MODE", |
| 182 | +] |
0 commit comments