import time from typing import Any, List, Mapping from backend.pashub_fetcher.job import Job from backend.pashub_fetcher.pashub_client import PashubClient, UnauthorizedError from backend.pashub_fetcher.token_getter import get_token_from_local_storage from utils.logger import setup_logger from utils.sharepoint.domna_sharepoint_client import DomnaSharepointClient from utils.sharepoint.domna_sites import DomnaSites logger = setup_logger() def handler(event: Mapping[str, Any], context: Any) -> None: pas_hub_email = "random@test.com" pas_hub_password = "my_fake_password" try: token: str = get_token_from_local_storage(pas_hub_email, pas_hub_password) logger.info(f"Token extracted successfully") except: logger.error("Error getting auth token from Pas Hub") raise pashub_client = PashubClient(token=token) sharepoint_client = DomnaSharepointClient( sharepoint_location=DomnaSites.SOCIAL_HOUSING_WAVE_3 ) jobs: List[Job] = [ { "id": "5abf6e27-e4c4-4ba8-b69d-9e34939e0002", "address": "FLAT 11 Abbey View, Garsmouth Way, Watford, WD25 9DY", }, { "id": "047f4455-85e2-4293-97b1-6b460137d33e", "address": "FLAT 14 Abbey View, Garsmouth Way, Watford, WD25 9DY", }, ] # TODO: get these from request body or spreadsheet sharepoint_client.makedir("Watford Test", "/JTK Test Folder") saved_file_paths: List[str] = [] for job in jobs: try: job_files: List[str] = pashub_client.get_core_evidence_files_by_job_id( job["id"] ) # Upload files to sharepoint sharepoint_client.makedir(job["address"], "/JTK Test Folder/Watford Test") for file_path in job_files: sharepoint_client.upload_file( file_path, f"/JTK Test Folder/Watford Test/{job['address']}", file_path.split("/")[-1], ) saved_file_paths.extend(job_files) except UnauthorizedError: logger.warning("Token expired - refreshing") token = get_token_from_local_storage(pas_hub_email, pas_hub_password) pashub_client = PashubClient(token=token) # retry once saved_file_paths.extend( pashub_client.get_core_evidence_files_by_job_id(job["id"]) ) time.sleep(10) # Simulate manual download print(f"saved {len(saved_file_paths)} files")