pulling together OS client and DBClient

This commit is contained in:
Khalim Conn-Kowlessar 2023-12-30 20:48:40 +00:00
parent 9f3853c2b1
commit 05cce77f16
3 changed files with 43 additions and 8 deletions

7
backend/DbClient.py Normal file
View file

@ -0,0 +1,7 @@
class DbClient:
def __init__(self):
"""
This class handles interaction with the database
"""
pass

View file

@ -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):

View file

@ -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)