mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
back to origional format
This commit is contained in:
parent
124a34597a
commit
1b9c26a2b6
4 changed files with 22 additions and 76 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
Loading…
Add table
Reference in a new issue