From 55b8a0ace8519bc771108a84825f56c8dd35e874 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Wed, 6 Dec 2023 16:50:53 +0000 Subject: [PATCH] setting up the SearchEpc class --- .idea/Model.iml | 2 +- .idea/misc.xml | 2 +- backend/SearchEpc.py | 77 ++++++++++++++++++++++++++++++++- etl/eligibility/ha_15_32/app.py | 7 ++- 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/.idea/Model.iml b/.idea/Model.iml index 3a3ec5a2..b0f9c00d 100644 --- a/.idea/Model.iml +++ b/.idea/Model.iml @@ -7,7 +7,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 605a6457..1122b380 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,7 +3,7 @@ - + diff --git a/backend/SearchEpc.py b/backend/SearchEpc.py index 41fbbb0f..26682044 100644 --- a/backend/SearchEpc.py +++ b/backend/SearchEpc.py @@ -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) + } diff --git a/etl/eligibility/ha_15_32/app.py b/etl/eligibility/ha_15_32/app.py index 90229801..d037d610 100644 --- a/etl/eligibility/ha_15_32/app.py +++ b/etl/eligibility/ha_15_32/app.py @@ -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():