setting up the SearchEpc class

This commit is contained in:
Khalim Conn-Kowlessar 2023-12-06 16:50:53 +00:00
parent fa5148228b
commit 55b8a0ace8
4 changed files with 84 additions and 4 deletions

2
.idea/Model.iml generated
View file

@ -7,7 +7,7 @@
<sourceFolder url="file://$MODULE_DIR$/open_uprn" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/recommendations" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="ha_15_32_eligibility" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.10 (model_data)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyNamespacePackagesService">

2
.idea/misc.xml generated
View file

@ -3,7 +3,7 @@
<component name="Black">
<option name="sdkName" value="Python 3.10 (backend)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="ha_15_32_eligibility" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (model_data)" project-jdk-type="Python SDK" />
<component name="PythonCompatibilityInspectionAdvertiser">
<option name="version" value="3" />
</component>

View file

@ -1,4 +1,10 @@
import os
import time
from epc_api.client import EpcClient
from utils.logger import setup_logger
logger = setup_logger()
class SearchEpc:
"""
@ -12,7 +18,29 @@ class SearchEpc:
combinations about the home to find the property
"""
def __init__(self, address1, postcode, address2=None, address3=None, address4=None):
MAX_RETRIES = 5
SUCCESS = {
"status": 200,
"message": "success",
"error": None
}
NODATA = {
"status": 201,
"message": "No data",
"error": None
}
def __init__(
self,
address1: str,
postcode: str,
address2: str = None,
address3: str = None,
address4: str = None,
max_retries: int = None
):
"""
Address lines 1 and postcode are mandatory fields. The other address lines are optional
but can be used to find the epc for the home, if address1 and postcode are insufficient
@ -29,4 +57,51 @@ class SearchEpc:
self.address3 = address3
self.address4 = address4
self.max_retries = max_retries if max_retries is not None else self.MAX_RETRIES
self.client = EpcClient(auth_token=os.getenv("EPC_AUTH_TOKEN"))
self.data = None
def search(self):
# Get the EPC data with retries
response = {}
for retry in range(self.max_retries):
try:
response = self.client.domestic.search(
params={"address": self.address1, "postcode": self.postcode}
)
if response:
self.data = response
return self.SUCCESS
if retry > 0:
print("Failed previous attempt but retry successful")
# If we got nothing, final try
if not response:
raise NotImplementedError("Implement me")
# response = client.domestic.search(
# params={"address": " ".join([home["Dwelling num"], home["Street"]]),
# "postcode": home["Postcode"]}
# )
# TODO: Eventually, if we have nothing, we should exit with a 201 or 202, saying that
# there is not data for this property
return {
"status": 200,
"message": "success",
"error": None
}
except Exception as e:
if retry < self.max_retries - 1:
# If not the last retry, wait for 3 seconds before retrying
time.sleep(3)
else:
# If it's the last retry, we continue
return {
"status": 500,
"message": "Could not retrieve EPC data",
"error": str(e)
}

View file

@ -3,12 +3,17 @@ This process has been created to compare the model based eligibility process aga
used by the Warmfront team, to identify which properties are eligible for ECO4 and GBIS funding. This
work is being done in December 2023, prior to completion of acquisition
"""
from pathlib import Path
import pandas as pd
import numpy as np
from utils.logger import setup_logger
from dotenv import load_dotenv
from backend.SearchEpc import SearchEpc
ENV_FILE = Path(__file__).parent / "etl" / "eligibility" / "ha_15_32" / ".env"
logger = setup_logger()
load_dotenv(ENV_FILE)
def load_data():