diff --git a/backend/DbClient.py b/backend/DbClient.py new file mode 100644 index 00000000..2ee01349 --- /dev/null +++ b/backend/DbClient.py @@ -0,0 +1,7 @@ +class DbClient: + + def __init__(self): + """ + This class handles interaction with the database + """ + pass diff --git a/backend/OrdnanceSurvey.py b/backend/OrdnanceSurvey.py index 774fbaaa..afb57620 100644 --- a/backend/OrdnanceSurvey.py +++ b/backend/OrdnanceSurvey.py @@ -1,21 +1,50 @@ +from functools import lru_cache +import urllib.parse +import requests +from utils.logger import setup_logger + +logger = setup_logger() + + class OrdnanceSuveyClient: - def __init__(self, address, postcode): + def __init__(self, address, postcode, api_key): """ This class is tasked with interaction with the ordnance survey API. - :param address: - :param postcode: + :param address: The address for the property to search for + :param postcode: The postcode for the property to search for """ self.address = address self.postcode = postcode + self.full_address = ", ".join([self.address, self.postcode]) + self.api_key = api_key + self.results = None + + @lru_cache(maxsize=128) def get_places_api(self): """ This method is tasked with getting the places api from the Ordnance Survey. - :return: """ - pass + + if not self.api_key: + raise ValueError("Ordnance Survey API key not specified") + + encoded_address_query = urllib.parse.quote(self.full_address) + url = (f"https://api.os.uk/search/places/v1/find?query={encoded_address_query}&key=" + f"{self.api_key}") + response = requests.get(url) + if response.status_code == 200: + data = response.json() + + # Extract the UPRN from the data + # Note: This example assumes that the first result is the correct one, which might not always be the case. + results = data['results'] + self.results = results + else: + logger.info("Could not find any results for the provided address and postcode") + return @staticmethod def parse_classification_code(classification_code: str): diff --git a/backend/SearchEpc.py b/backend/SearchEpc.py index ed6857c7..a63437dd 100644 --- a/backend/SearchEpc.py +++ b/backend/SearchEpc.py @@ -2007,9 +2007,8 @@ class SearchEpc: from backend.OrdnanceSurvey import OrdnanceSuveyClient os_property_type, os_built_form = OrdnanceSuveyClient.parse_classification_code( - property_os_place["CLASSIFICATION_CODE"]) - # TODO: Then, if the property is semi-detatched, but all of the neighbours are terraced, we should update the - # property type to end-terraced + property_os_place["CLASSIFICATION_CODE"] + ) # We firstly get the first 100 properties for the postcode, from the EPC api epc_reponse = self.client.domestic.search(params={"postcode": self.postcode}, size=100)