plug everything into in handler

This commit is contained in:
Daniel Roth 2026-02-04 15:14:26 +00:00
parent 476c9f9c7a
commit 981b6358c4
5 changed files with 24 additions and 16 deletions

View file

@ -10,5 +10,7 @@ class ConditionFileType(Enum):
class ConditionTriggerRequest(BaseModel):
file_type: ConditionFileType
trigger_file_path: str # TODO: split into bucket/prefix?
uprn_lookup_file_path: str # TODO: split into bucket/prefix?
trigger_file_bucket: str # TODO: get this from settings
trigger_file_key: str
uprn_lookup_file_bucket: str # TODO: get this from settings
uprn_lookup_file_key: str

View file

@ -1,19 +1,18 @@
import asyncio
import json
from typing import Mapping, Any
from io import BytesIO
from backend.condition.condition_trigger_request import ConditionTriggerRequest
from backend.condition.lookups.uprn_lookup_s3 import UprnLookupS3
from utils.logger import setup_logger
from backend.condition.processor import process_file
from utils.logger import setup_logger
from utils.s3 import read_io_from_s3
logger = setup_logger()
def handler(event: Mapping[str, Any], context: Any) -> None:
# Temporary stub for PoC wiring
uprn_lookup = UprnLookupS3(
bucket="", key=""
) # TODO: replace with postgres implementation
@ -23,13 +22,16 @@ def handler(event: Mapping[str, Any], context: Any) -> None:
body_dict = json.loads(record["body"])
payload = ConditionTriggerRequest.model_validate(body_dict)
# fetch file from s3
file_bytes: BytesIO = read_io_from_s3(
bucket_name=payload.trigger_file_bucket,
file_key=payload.trigger_file_key,
)
# open file and send bytes to processor
process_file(
file_stream=file_bytes,
file_type=payload.file_type,
uprn_lookup=uprn_lookup,
)
except Exception as e:
logger.error(f"Failed to process record: {e}")
# dummy_stream = BytesIO(b"")
# process_file(dummy_stream, source_key)

View file

@ -1,7 +1,9 @@
import csv
from io import TextIOWrapper
from io import BytesIO, TextIOWrapper
from typing import BinaryIO, Dict, TextIO
from backend.condition.lookups.uprn_lookup import UprnLookup
from utils.s3 import read_io_from_s3
class UprnLookupS3(UprnLookup):
@ -10,9 +12,13 @@ class UprnLookupS3(UprnLookup):
self.key = key
def get_property_ref_to_uprn_lookup(self) -> Dict[str, int]:
raise NotImplementedError()
file_bytes: BytesIO = read_io_from_s3(
bucket_name=self.bucket, file_key=self.key
)
def _parse_csv(self, file_stream: BinaryIO) -> Dict[str, int]:
return self._parse_csv_bytes(file_bytes)
def _parse_csv_bytes(self, file_stream: BinaryIO) -> Dict[str, int]:
text_stream: TextIO = TextIOWrapper(file_stream, encoding="utf-8")
mapping: Dict[str, int] = {}
reader = csv.DictReader(text_stream)

View file

@ -18,7 +18,6 @@ class LbwfParser(Parser):
def parse(
self,
file_stream: BinaryIO,
location_ref_to_uprn_map: Optional[Dict[str, int]] = None,
) -> Any:
wb: Workbook = load_workbook(file_stream)
address_to_uprn_map: Dict[str, int] = LbwfParser._generate_address_to_uprn_dict(

View file

@ -8,6 +8,5 @@ class Parser(ABC):
def parse(
self,
file_stream: BinaryIO,
location_ref_to_uprn_map: Optional[Dict[str, int]] = None,
) -> Any:
pass