Hasan Iqbal
commited on
Added dynamodb interface and base model
Browse files
src/openfactcheck/services/__init__.py
ADDED
|
File without changes
|
src/openfactcheck/services/dynamodbinterface.py
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional, TypeVar, Type
|
| 2 |
+
|
| 3 |
+
from botocore.client import BaseClient
|
| 4 |
+
from boto3.dynamodb.types import TypeSerializer, TypeDeserializer
|
| 5 |
+
from botocore.exceptions import ClientError
|
| 6 |
+
|
| 7 |
+
from openfactcheck.utils import logging
|
| 8 |
+
from openfactcheck.services.model import DynamoDBBaseModel
|
| 9 |
+
|
| 10 |
+
logger = logging.get_logger(__name__)
|
| 11 |
+
|
| 12 |
+
T = TypeVar("T", bound=DynamoDBBaseModel)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class DynamoDBInterface:
|
| 16 |
+
"""
|
| 17 |
+
Interface to interact with AWS DynamoDB service.
|
| 18 |
+
|
| 19 |
+
Provides methods to create, update, and fetch items from a DynamoDB table.
|
| 20 |
+
|
| 21 |
+
Parameters
|
| 22 |
+
----------
|
| 23 |
+
dynamodb_svc : botocore.client.BaseClient
|
| 24 |
+
Boto3 DynamoDB service client.
|
| 25 |
+
table : str
|
| 26 |
+
Name of the DynamoDB table.
|
| 27 |
+
|
| 28 |
+
Attributes
|
| 29 |
+
----------
|
| 30 |
+
logger : logging.Logger
|
| 31 |
+
Logger instance for the class.
|
| 32 |
+
table : str
|
| 33 |
+
Name of the DynamoDB table.
|
| 34 |
+
dynamodb_svc : botocore.client.BaseClient
|
| 35 |
+
Boto3 DynamoDB service client.
|
| 36 |
+
|
| 37 |
+
Methods
|
| 38 |
+
-------
|
| 39 |
+
create_or_update(storable: DynamoDBBaseModel) -> None:
|
| 40 |
+
Create or update a DynamoDB item based on the provided storable object.
|
| 41 |
+
fetch(pk: str, model: Type[T]) -> Optional[T]:
|
| 42 |
+
Fetch a DynamoDB item by primary key and deserialize it into the provided model.
|
| 43 |
+
|
| 44 |
+
Raises
|
| 45 |
+
------
|
| 46 |
+
ClientError
|
| 47 |
+
If the DynamoDB service reports an error.
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
def __init__(self, dynamodb_svc: BaseClient, table: str) -> None:
|
| 51 |
+
self.logger = logger
|
| 52 |
+
self.table: str = table
|
| 53 |
+
self.dynamodb_svc: BaseClient = dynamodb_svc
|
| 54 |
+
|
| 55 |
+
def _serialize_item(self, storable: DynamoDBBaseModel) -> dict[str, Any]:
|
| 56 |
+
"""
|
| 57 |
+
Serialize a DynamoDBBaseModel instance to a dictionary format for DynamoDB storage.
|
| 58 |
+
|
| 59 |
+
Parameters
|
| 60 |
+
----------
|
| 61 |
+
storable : DynamoDBBaseModel
|
| 62 |
+
The object to serialize.
|
| 63 |
+
|
| 64 |
+
Returns
|
| 65 |
+
-------
|
| 66 |
+
Dict[str, Any]
|
| 67 |
+
The serialized item ready to be stored in DynamoDB.
|
| 68 |
+
"""
|
| 69 |
+
serializer = TypeSerializer()
|
| 70 |
+
item_dict = storable.model_dump(exclude_unset=True, by_alias=True)
|
| 71 |
+
av = {k: serializer.serialize(v) for k, v in item_dict.items()}
|
| 72 |
+
|
| 73 |
+
# Add the primary key
|
| 74 |
+
av["PK"] = serializer.serialize(storable.PK)
|
| 75 |
+
|
| 76 |
+
# Optionally add the sort key, if present
|
| 77 |
+
if storable.SK is not None:
|
| 78 |
+
av["SK"] = serializer.serialize(storable.SK)
|
| 79 |
+
|
| 80 |
+
# Optionally add the GS1 partition key, if present
|
| 81 |
+
if storable.GS1PK is not None:
|
| 82 |
+
av["GS1PK"] = serializer.serialize(storable.GS1PK)
|
| 83 |
+
|
| 84 |
+
return av
|
| 85 |
+
|
| 86 |
+
def _deserialize_item(self, item: dict[str, Any], model: Type[T]) -> T:
|
| 87 |
+
"""
|
| 88 |
+
Deserialize a DynamoDB item into an instance of the provided model.
|
| 89 |
+
|
| 90 |
+
Parameters
|
| 91 |
+
----------
|
| 92 |
+
item : dict
|
| 93 |
+
The DynamoDB item to deserialize.
|
| 94 |
+
model : Type[T]
|
| 95 |
+
The model class to instantiate with the deserialized data.
|
| 96 |
+
|
| 97 |
+
Returns
|
| 98 |
+
-------
|
| 99 |
+
T
|
| 100 |
+
An instance of the model class populated with data from the item.
|
| 101 |
+
"""
|
| 102 |
+
deserializer = TypeDeserializer()
|
| 103 |
+
attributes = {k: deserializer.deserialize(v) for k, v in item.items()}
|
| 104 |
+
return model(**attributes)
|
| 105 |
+
|
| 106 |
+
def _paged_scan(self) -> list[dict[str, Any]]:
|
| 107 |
+
"""
|
| 108 |
+
Perform a paginated scan of the DynamoDB table and return all items.
|
| 109 |
+
|
| 110 |
+
Returns
|
| 111 |
+
-------
|
| 112 |
+
list of dict
|
| 113 |
+
A list of items retrieved from the DynamoDB table.
|
| 114 |
+
|
| 115 |
+
Raises
|
| 116 |
+
------
|
| 117 |
+
ClientError
|
| 118 |
+
If the DynamoDB service reports an error.
|
| 119 |
+
"""
|
| 120 |
+
try:
|
| 121 |
+
items = []
|
| 122 |
+
scan_kwargs = {"TableName": self.table}
|
| 123 |
+
while True:
|
| 124 |
+
response = self.dynamodb_svc.scan(**scan_kwargs)
|
| 125 |
+
items.extend(response.get("Items", []))
|
| 126 |
+
self.logger.debug(f"Fetched {len(response.get('Items', []))} items in this page.")
|
| 127 |
+
if "LastEvaluatedKey" in response:
|
| 128 |
+
scan_kwargs["ExclusiveStartKey"] = response["LastEvaluatedKey"]
|
| 129 |
+
else:
|
| 130 |
+
break
|
| 131 |
+
self.logger.info(f"Total {len(items)} items fetched from table {self.table}.")
|
| 132 |
+
return items
|
| 133 |
+
except ClientError as e:
|
| 134 |
+
self.logger.error(f"DynamoDBInterface._paged_scan failed: {e}")
|
| 135 |
+
raise
|
| 136 |
+
|
| 137 |
+
def create_or_update(self, storable: DynamoDBBaseModel) -> None:
|
| 138 |
+
"""
|
| 139 |
+
Create or update a DynamoDB item based on the provided storable object.
|
| 140 |
+
|
| 141 |
+
Parameters
|
| 142 |
+
----------
|
| 143 |
+
storable : DynamoDBBaseModel
|
| 144 |
+
The object to create or update in DynamoDB.
|
| 145 |
+
|
| 146 |
+
Raises
|
| 147 |
+
------
|
| 148 |
+
ClientError
|
| 149 |
+
If the DynamoDB service reports an error.
|
| 150 |
+
"""
|
| 151 |
+
try:
|
| 152 |
+
item = self._serialize_item(storable)
|
| 153 |
+
self.dynamodb_svc.put_item(TableName=self.table, Item=item)
|
| 154 |
+
self.logger.info(f"Item with PK={storable.PK} created/updated successfully.")
|
| 155 |
+
except ClientError as e:
|
| 156 |
+
self.logger.error(f"DynamoDBInterface.create_or_update failed: {e}")
|
| 157 |
+
raise
|
| 158 |
+
|
| 159 |
+
def fetch(self, pk: str, model: Type[T]) -> Optional[T]:
|
| 160 |
+
"""
|
| 161 |
+
Fetch a DynamoDB item by primary key and deserialize it into the provided model.
|
| 162 |
+
|
| 163 |
+
Parameters
|
| 164 |
+
----------
|
| 165 |
+
pk : str
|
| 166 |
+
The primary key of the item to fetch.
|
| 167 |
+
model : Type[T]
|
| 168 |
+
The model class to deserialize the item into.
|
| 169 |
+
|
| 170 |
+
Returns
|
| 171 |
+
-------
|
| 172 |
+
Optional[T]
|
| 173 |
+
An instance of the model if found; otherwise, None.
|
| 174 |
+
|
| 175 |
+
Raises
|
| 176 |
+
------
|
| 177 |
+
ClientError
|
| 178 |
+
If the DynamoDB service reports an error.
|
| 179 |
+
"""
|
| 180 |
+
try:
|
| 181 |
+
key = {"PK": {"S": pk}}
|
| 182 |
+
response = self.dynamodb_svc.get_item(TableName=self.table, Key=key)
|
| 183 |
+
if "Item" not in response:
|
| 184 |
+
self.logger.info(f"No item found with PK={pk}.")
|
| 185 |
+
return None
|
| 186 |
+
self.logger.info(f"Item with PK={pk} fetched successfully.")
|
| 187 |
+
return self._deserialize_item(response["Item"], model)
|
| 188 |
+
except ClientError as e:
|
| 189 |
+
self.logger.error(f"DynamoDBInterface.fetch failed: {e}")
|
| 190 |
+
raise
|
| 191 |
+
|
| 192 |
+
def delete(self, pk: str) -> None:
|
| 193 |
+
"""
|
| 194 |
+
Delete a DynamoDB item by primary key.
|
| 195 |
+
|
| 196 |
+
Parameters
|
| 197 |
+
----------
|
| 198 |
+
pk : str
|
| 199 |
+
The primary key of the item to delete.
|
| 200 |
+
|
| 201 |
+
Raises
|
| 202 |
+
------
|
| 203 |
+
ClientError
|
| 204 |
+
If the DynamoDB service reports an error.
|
| 205 |
+
"""
|
| 206 |
+
try:
|
| 207 |
+
key = {"PK": {"S": pk}}
|
| 208 |
+
self.dynamodb_svc.delete_item(TableName=self.table, Key=key)
|
| 209 |
+
self.logger.info(f"Item with PK={pk} deleted successfully.")
|
| 210 |
+
except ClientError as e:
|
| 211 |
+
self.logger.error(f"DynamoDBInterface.delete failed: {e}")
|
| 212 |
+
raise
|
| 213 |
+
|
| 214 |
+
def list(self, model: Type[T]) -> Optional[list[T]]:
|
| 215 |
+
"""
|
| 216 |
+
List all items in the DynamoDB table and deserialize them into the provided model.
|
| 217 |
+
|
| 218 |
+
Parameters
|
| 219 |
+
----------
|
| 220 |
+
model : Type[T]
|
| 221 |
+
The model class to deserialize the items into.
|
| 222 |
+
|
| 223 |
+
Returns
|
| 224 |
+
-------
|
| 225 |
+
Optional[List[T]]
|
| 226 |
+
A list of instances of the model class if items are found; otherwise, None.
|
| 227 |
+
|
| 228 |
+
Raises
|
| 229 |
+
------
|
| 230 |
+
ClientError
|
| 231 |
+
If the DynamoDB service reports an error.
|
| 232 |
+
"""
|
| 233 |
+
try:
|
| 234 |
+
items = self._paged_scan()
|
| 235 |
+
if not items:
|
| 236 |
+
self.logger.info(f"No items found in table {self.table}.")
|
| 237 |
+
return None
|
| 238 |
+
self.logger.info("Items fetched successfully.")
|
| 239 |
+
return [self._deserialize_item(item, model) for item in items]
|
| 240 |
+
except ClientError as e:
|
| 241 |
+
self.logger.error(f"DynamoDBInterface.list failed: {e}")
|
| 242 |
+
raise
|
src/openfactcheck/services/model.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
from abc import ABC, abstractmethod
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class DynamoDBBaseModel(BaseModel, ABC):
|
| 7 |
+
"""
|
| 8 |
+
Base model for DynamoDB items, requiring a primary key (PK).
|
| 9 |
+
Optional sort key (SK) and global secondary index partition key (GS1PK) can be defined.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
@property
|
| 13 |
+
@abstractmethod
|
| 14 |
+
def PK(self) -> str:
|
| 15 |
+
"""Primary Key for DynamoDB storage. Must be implemented by subclasses."""
|
| 16 |
+
pass
|
| 17 |
+
|
| 18 |
+
@property
|
| 19 |
+
def SK(self) -> Optional[str]:
|
| 20 |
+
"""Sort Key for DynamoDB storage. Optional."""
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
@property
|
| 24 |
+
def GS1PK(self) -> Optional[str]:
|
| 25 |
+
"""Global Secondary Index 1 Partition Key for DynamoDB. Optional."""
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
class Config:
|
| 29 |
+
# Updated configuration key for Pydantic v2
|
| 30 |
+
populate_by_name = True
|
| 31 |
+
|
| 32 |
+
# Allow extra fields (useful for DynamoDB metadata)
|
| 33 |
+
extra = "allow"
|