99AppSync and DynamoDB implementations while maintaining the same interface.
1010"""
1111
12- import os
1312import logging
13+ import os
1414from typing import Optional , Union
1515
1616from idp_common .appsync import DocumentAppSyncService
3030class DocumentServiceFactory :
3131 """
3232 Factory class for creating document services based on configuration.
33-
33+
3434 This factory allows switching between AppSync and DynamoDB implementations
3535 while maintaining the same interface for document operations.
3636 """
37-
37+
3838 @staticmethod
3939 def create_service (
40- mode : Optional [str ] = None ,
41- ** kwargs
40+ mode : Optional [str ] = None , ** kwargs
4241 ) -> Union [DocumentAppSyncService , DocumentDynamoDBService ]:
4342 """
4443 Create a document service based on the specified mode.
45-
44+
4645 Args:
4746 mode: Optional mode override. If not provided, uses DOCUMENT_TRACKING_MODE
4847 environment variable, defaulting to 'appsync'
4948 **kwargs: Additional arguments passed to the service constructor
50-
49+
5150 Returns:
5251 DocumentAppSyncService or DocumentDynamoDBService instance
53-
52+
5453 Raises:
5554 ValueError: If an unsupported mode is specified
56-
55+
5756 Examples:
5857 # Use environment variable (default behavior)
5958 service = DocumentServiceFactory.create_service()
60-
59+
6160 # Override mode explicitly
6261 service = DocumentServiceFactory.create_service(mode='dynamodb')
63-
62+
6463 # Pass additional arguments
6564 service = DocumentServiceFactory.create_service(
6665 mode='appsync',
@@ -72,16 +71,16 @@ def create_service(
7271 mode = os .environ .get ("DOCUMENT_TRACKING_MODE" , DEFAULT_MODE ).lower ()
7372 else :
7473 mode = mode .lower ()
75-
74+
7675 # Validate mode
7776 if mode not in SUPPORTED_MODES :
7877 raise ValueError (
7978 f"Unsupported document tracking mode: '{ mode } '. "
8079 f"Supported modes are: { ', ' .join (SUPPORTED_MODES )} "
8180 )
82-
81+
8382 logger .info (f"Creating document service with mode: { mode } " )
84-
83+
8584 # Create the appropriate service
8685 if mode == APPSYNC_MODE :
8786 return DocumentAppSyncService (** kwargs )
@@ -90,32 +89,32 @@ def create_service(
9089 else :
9190 # This should never happen due to validation above, but included for completeness
9291 raise ValueError (f"Unsupported mode: { mode } " )
93-
92+
9493 @staticmethod
9594 def get_current_mode () -> str :
9695 """
9796 Get the current document tracking mode from environment variable.
98-
97+
9998 Returns:
10099 Current mode string ('appsync' or 'dynamodb')
101100 """
102101 return os .environ .get ("DOCUMENT_TRACKING_MODE" , DEFAULT_MODE ).lower ()
103-
102+
104103 @staticmethod
105104 def is_appsync_mode () -> bool :
106105 """
107106 Check if current mode is AppSync.
108-
107+
109108 Returns:
110109 True if current mode is AppSync, False otherwise
111110 """
112111 return DocumentServiceFactory .get_current_mode () == APPSYNC_MODE
113-
112+
114113 @staticmethod
115114 def is_dynamodb_mode () -> bool :
116115 """
117116 Check if current mode is DynamoDB.
118-
117+
119118 Returns:
120119 True if current mode is DynamoDB, False otherwise
121120 """
@@ -124,29 +123,28 @@ def is_dynamodb_mode() -> bool:
124123
125124# Convenience function for creating services
126125def create_document_service (
127- mode : Optional [str ] = None ,
128- ** kwargs
126+ mode : Optional [str ] = None , ** kwargs
129127) -> Union [DocumentAppSyncService , DocumentDynamoDBService ]:
130128 """
131129 Convenience function to create a document service.
132-
130+
133131 This is a shorthand for DocumentServiceFactory.create_service().
134-
132+
135133 Args:
136134 mode: Optional mode override. If not provided, uses DOCUMENT_TRACKING_MODE
137135 environment variable, defaulting to 'appsync'
138136 **kwargs: Additional arguments passed to the service constructor
139-
137+
140138 Returns:
141139 DocumentAppSyncService or DocumentDynamoDBService instance
142-
140+
143141 Examples:
144142 # Simple usage
145143 service = create_document_service()
146-
144+
147145 # With mode override
148146 service = create_document_service(mode='dynamodb')
149-
147+
150148 # With additional parameters
151149 service = create_document_service(
152150 mode='appsync',
0 commit comments