From 1b9c26a2b62cb32e3bffdfbc1035954acfc6bebb Mon Sep 17 00:00:00 2001 From: Jun-te Kim Date: Mon, 9 Mar 2026 12:32:20 +0000 Subject: [PATCH] back to origional format --- backend/app/config.py | 1 - backend/ordnanceSurvey/helpers.py | 32 +++++----------------- backend/ordnanceSurvey/main.py | 21 ++++++++++----- backend/ordnanceSurvey/types.py | 44 ------------------------------- 4 files changed, 22 insertions(+), 76 deletions(-) delete mode 100644 backend/ordnanceSurvey/types.py diff --git a/backend/app/config.py b/backend/app/config.py index e87f8374..b5b29137 100644 --- a/backend/app/config.py +++ b/backend/app/config.py @@ -64,7 +64,6 @@ class Settings(BaseSettings): ENERGY_ASSESSMENTS_BUCKET: str = "changeme" ORDNANCE_SURVEY_API_KEY: str = "changeme" - PLAN_TRIGGER_BUCKET: str = "changeme" # Optional AWS creds (only required in local) AWS_ACCESS_KEY_ID: Optional[str] = None diff --git a/backend/ordnanceSurvey/helpers.py b/backend/ordnanceSurvey/helpers.py index d59060b8..fcaa148a 100644 --- a/backend/ordnanceSurvey/helpers.py +++ b/backend/ordnanceSurvey/helpers.py @@ -23,44 +23,26 @@ def os_places_results_to_dataframe(data: dict) -> pd.DataFrame: return pd.DataFrame(rows) -import urllib.parse -import requests -import logging -from pydantic import ValidationError - -logger = logging.getLogger(__name__) - - -def lookup_os_places(postcode: str, api_key: str) -> PostcodeResponse: +def lookup_os_places(postcode: str, api_key: str) -> dict: """ Lookup a postcode using the OS Places API. - Returns a validated PostcodeResponse. - Raises exceptions on failure. + Returns the full API response data or an error dict. """ if not api_key: - raise ValueError("Ordnance Survey API key not specified") + return {"error": "Ordnance Survey API key not specified", "status": 400} encoded_postcode = urllib.parse.quote(postcode) - url = ( f"https://api.os.uk/search/places/v1/postcode?postcode={encoded_postcode}" f"&dataset=DPA,LPI&key={api_key}" ) response = requests.get(url) - if response.status_code != 200: logger.error( - "OS Places API error for postcode %s: %s", - postcode, - response.status_code, + f"OS Places API error for postcode {postcode}: {response.status_code}" ) - raise RuntimeError(f"OS Places lookup failed for postcode {postcode}") + return {"error": "Failed to fetch address data", "status": response.status_code} - try: - raw = response.json() - return PostcodeResponse.model_validate(raw) - - except ValidationError as e: - logger.error("OS Places response validation failed: %s", e) - raise RuntimeError("Invalid response format from OS Places API") from e + data = response.json() + return {"data": data, "status": 200} diff --git a/backend/ordnanceSurvey/main.py b/backend/ordnanceSurvey/main.py index 835910f5..bf8cfdaf 100644 --- a/backend/ordnanceSurvey/main.py +++ b/backend/ordnanceSurvey/main.py @@ -1,6 +1,5 @@ from typing import Any import json -from backend.ordnanceSurvey.types import PostcodeResponse from utils.logger import setup_logger import logging from backend.utils.subtasks import subtask_handler @@ -12,7 +11,7 @@ from utils.s3 import ( from backend.utils.addressMatch import addressMatch from backend.app.db.connection import get_db_session from backend.app.db.models.postcode_search import PostcodeSearchModel -from backend.ordnanceSurvey.helpers import ( +from backend.utils.ordnance_survey import ( lookup_os_places, os_places_results_to_dataframe, ) @@ -28,7 +27,6 @@ logger: logging.Logger = setup_logger() def check_if_post_code_exists_in_db_cache(postcode): - postcode = "SE22 9AL" with get_db_session() as session: result = ( @@ -45,12 +43,16 @@ def check_if_post_code_exists_in_db_cache(postcode): # Cache miss — fetch from OS Places API api_key = get_settings().ORDNANCE_SURVEY_API_KEY - response: PostcodeResponse = lookup_os_places(postcode, api_key) + response = lookup_os_places(postcode, api_key) + + if response.get("status") != 200 or "data" not in response: + logger.error(f"OS Places API failed for {postcode}: {response}") + return pd.DataFrame() # Save to cache new_record = PostcodeSearchModel( postcode=postcode, - result_data=response.results, + result_data=response["data"], ) session.add(new_record) session.commit() @@ -58,6 +60,13 @@ def check_if_post_code_exists_in_db_cache(postcode): return os_places_results_to_dataframe(response["data"]) +def get_ordance_survey_record(row, cache=None): + if cache is None: + cache = check_if_post_code_exists_in_db_cache(postcode) + + # process cache with row + + def save_results_to_s3( results_df: pd.DataFrame, task_id: str, sub_task_id: str, bucket_name: str = None ) -> bool: @@ -91,7 +100,7 @@ def save_results_to_s3( logger.info(f"Successfully saved results to s3://{bucket_name}/{file_key}") return True else: - logger.error(f"Failed to save results to S3 {bucket_name}/{file_key}") + logger.error(f"Failed to save results to S3") return False except Exception as e: diff --git a/backend/ordnanceSurvey/types.py b/backend/ordnanceSurvey/types.py deleted file mode 100644 index 0631ff67..00000000 --- a/backend/ordnanceSurvey/types.py +++ /dev/null @@ -1,44 +0,0 @@ -from pydantic import BaseModel -from typing import List - - -class OrdnanceSurveyResponse(BaseModel): - RPC: str - UPRN: str - MATCH: int - UDPRN: str - STATUS: str - ADDRESS: str - LANGUAGE: str - POSTCODE: str - POST_TOWN: str - WARD_CODE: str - ENTRY_DATE: str - COUNTRY_CODE: str - X_COORDINATE: int - Y_COORDINATE: int - BUILDING_NAME: str - BLPU_STATE_CODE: str - BLPU_STATE_DATE: str - LAST_UPDATE_DATE: str - MATCH_DESCRIPTION: str - THOROUGHFARE_NAME: str - CLASSIFICATION_CODE: str - LOGICAL_STATUS_CODE: str - POSTAL_ADDRESS_CODE: str - LOCAL_CUSTODIAN_CODE: int - DELIVERY_POINT_SUFFIX: str - TOPOGRAPHY_LAYER_TOID: str - COUNTRY_CODE_DESCRIPTION: str - BLPU_STATE_CODE_DESCRIPTION: str - CLASSIFICATION_CODE_DESCRIPTION: str - POSTAL_ADDRESS_CODE_DESCRIPTION: str - LOCAL_CUSTODIAN_CODE_DESCRIPTION: str - - -class Result(BaseModel): - DPA: OrdnanceSurveyResponse - - -class PostcodeResponse(BaseModel): - results: List[Result]