Skip to content

Commit 8002143

Browse files
author
Rafał Pawłaszek
committed
feat: document tracking choice - appsync or ddb
1 parent dd5c266 commit 8002143

File tree

47 files changed

+1890
-104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1890
-104
lines changed

lib/idp_common_pkg/idp_common/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ def __getattr__(name):
1313
if name in [
1414
"bedrock",
1515
"s3",
16+
"dynamodb",
17+
"appsync",
18+
"docs_service",
1619
"metrics",
1720
"image",
1821
"utils",
@@ -45,6 +48,9 @@ def __getattr__(name):
4548
__all__ = [
4649
"bedrock",
4750
"s3",
51+
"dynamodb",
52+
"appsync",
53+
"docs_service",
4854
"metrics",
4955
"image",
5056
"utils",
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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 os
13+
import logging
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,
41+
**kwargs
42+
) -> Union[DocumentAppSyncService, DocumentDynamoDBService]:
43+
"""
44+
Create a document service based on the specified mode.
45+
46+
Args:
47+
mode: Optional mode override. If not provided, uses DOCUMENT_TRACKING_MODE
48+
environment variable, defaulting to 'appsync'
49+
**kwargs: Additional arguments passed to the service constructor
50+
51+
Returns:
52+
DocumentAppSyncService or DocumentDynamoDBService instance
53+
54+
Raises:
55+
ValueError: If an unsupported mode is specified
56+
57+
Examples:
58+
# Use environment variable (default behavior)
59+
service = DocumentServiceFactory.create_service()
60+
61+
# Override mode explicitly
62+
service = DocumentServiceFactory.create_service(mode='dynamodb')
63+
64+
# Pass additional arguments
65+
service = DocumentServiceFactory.create_service(
66+
mode='appsync',
67+
api_url='https://example.appsync-api.us-east-1.amazonaws.com/graphql'
68+
)
69+
"""
70+
# Determine the mode
71+
if mode is None:
72+
mode = os.environ.get("DOCUMENT_TRACKING_MODE", DEFAULT_MODE).lower()
73+
else:
74+
mode = mode.lower()
75+
76+
# Validate mode
77+
if mode not in SUPPORTED_MODES:
78+
raise ValueError(
79+
f"Unsupported document tracking mode: '{mode}'. "
80+
f"Supported modes are: {', '.join(SUPPORTED_MODES)}"
81+
)
82+
83+
logger.info(f"Creating document service with mode: {mode}")
84+
85+
# Create the appropriate service
86+
if mode == APPSYNC_MODE:
87+
return DocumentAppSyncService(**kwargs)
88+
elif mode == DYNAMODB_MODE:
89+
return DocumentDynamoDBService(**kwargs)
90+
else:
91+
# This should never happen due to validation above, but included for completeness
92+
raise ValueError(f"Unsupported mode: {mode}")
93+
94+
@staticmethod
95+
def get_current_mode() -> str:
96+
"""
97+
Get the current document tracking mode from environment variable.
98+
99+
Returns:
100+
Current mode string ('appsync' or 'dynamodb')
101+
"""
102+
return os.environ.get("DOCUMENT_TRACKING_MODE", DEFAULT_MODE).lower()
103+
104+
@staticmethod
105+
def is_appsync_mode() -> bool:
106+
"""
107+
Check if current mode is AppSync.
108+
109+
Returns:
110+
True if current mode is AppSync, False otherwise
111+
"""
112+
return DocumentServiceFactory.get_current_mode() == APPSYNC_MODE
113+
114+
@staticmethod
115+
def is_dynamodb_mode() -> bool:
116+
"""
117+
Check if current mode is DynamoDB.
118+
119+
Returns:
120+
True if current mode is DynamoDB, False otherwise
121+
"""
122+
return DocumentServiceFactory.get_current_mode() == DYNAMODB_MODE
123+
124+
125+
# Convenience function for creating services
126+
def create_document_service(
127+
mode: Optional[str] = None,
128+
**kwargs
129+
) -> Union[DocumentAppSyncService, DocumentDynamoDBService]:
130+
"""
131+
Convenience function to create a document service.
132+
133+
This is a shorthand for DocumentServiceFactory.create_service().
134+
135+
Args:
136+
mode: Optional mode override. If not provided, uses DOCUMENT_TRACKING_MODE
137+
environment variable, defaulting to 'appsync'
138+
**kwargs: Additional arguments passed to the service constructor
139+
140+
Returns:
141+
DocumentAppSyncService or DocumentDynamoDBService instance
142+
143+
Examples:
144+
# Simple usage
145+
service = create_document_service()
146+
147+
# With mode override
148+
service = create_document_service(mode='dynamodb')
149+
150+
# With additional parameters
151+
service = create_document_service(
152+
mode='appsync',
153+
api_url='https://example.appsync-api.us-east-1.amazonaws.com/graphql'
154+
)
155+
"""
156+
return DocumentServiceFactory.create_service(mode=mode, **kwargs)
157+
158+
159+
# Convenience functions for mode checking
160+
def get_document_tracking_mode() -> str:
161+
"""Get the current document tracking mode."""
162+
return DocumentServiceFactory.get_current_mode()
163+
164+
165+
def is_appsync_mode() -> bool:
166+
"""Check if current mode is AppSync."""
167+
return DocumentServiceFactory.is_appsync_mode()
168+
169+
170+
def is_dynamodb_mode() -> bool:
171+
"""Check if current mode is DynamoDB."""
172+
return DocumentServiceFactory.is_dynamodb_mode()
173+
174+
175+
__all__ = [
176+
"DocumentServiceFactory",
177+
"create_document_service",
178+
"get_document_tracking_mode",
179+
"is_appsync_mode",
180+
"is_dynamodb_mode",
181+
"APPSYNC_MODE",
182+
"DYNAMODB_MODE",
183+
"DEFAULT_MODE",
184+
]

0 commit comments

Comments
 (0)