Model/model_data/Property.py
Khalim Conn-Kowlessar 0398fb6eba setup OpenUprnClient
2023-06-16 15:21:04 +01:00

47 lines
1.7 KiB
Python

from epc_api.client import EpcClient
from model_data.config import EPC_AUTH_TOKEN
from model_data.OpenUprnClient import OpenUprnClient
class Property:
def __init__(self, postcode, address1, epc_client=None, data=None):
self.postcode = postcode
self.address1 = address1
self.data = data
if epc_client:
self.epc_client = epc_client
else:
self.epc_client = EpcClient(auth_token=EPC_AUTH_TOKEN)
def search_address_epc(self):
"""
This method searches for an address in the EPC database and returns the first result
:return: property data
"""
if self.data:
return
# This will fail if a property does not have an EPC - this has been documented as a case to handle
response = self.epc_client.domestic.search(params={"address": self.address1, "postcode": self.postcode})
if len(response["rows"]) > 1:
newest_response = [
r for r in response["rows"] if
r["inspection-date"] == max([x["inspection-date"] for x in response["rows"]])
]
if len(newest_response) > 1:
raise Exception("More than one result found for this address - investigate me")
response["rows"] = newest_response
self.data = response["rows"][0]
def get_coordinates(self, open_oprn_client: OpenUprnClient):
"""
This method utlises the OpenOprnClient to get the coordinates of the property
The OpenOprnClient interfactes with the Ordinance Survey Open UPRN database to extract
property coordinates. This database holds lookups between UPRN and coordinates.
:param open_oprn_client:
:return:
"""