Skip to content

Commit e17613e

Browse files
authored
Merge pull request #19 from drift-labs:delist-recommender
Refactor delist recommender to utilize centralized API data fetching
2 parents 941e786 + e156c19 commit e17613e

File tree

1 file changed

+9
-24
lines changed

1 file changed

+9
-24
lines changed

src/page/delist_recommender.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import streamlit as st
2-
import requests
32
import pandas as pd
43
import numpy as np
5-
import os
6-
from dotenv import load_dotenv
7-
8-
# Load environment variables from .env file
9-
load_dotenv()
10-
11-
# Get backend URL from environment variable or use default
12-
BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000")
13-
API_ENDPOINT = f"{BACKEND_URL}/api/delist-recommender/recommendations"
4+
from lib.api import fetch_api_data
145

156
# --- Helper Functions ---
167

@@ -60,22 +51,16 @@ def style_recommendation(rec):
6051
def fetch_delist_recommendations():
6152
"""Fetches delist recommendations from the backend API."""
6253
try:
63-
print(f"Fetching data from: {API_ENDPOINT}") # Debug print
64-
response = requests.get(API_ENDPOINT, timeout=120) # Increased timeout to 120s
65-
response.raise_for_status() # Raise an exception for bad status codes
54+
print("Fetching delist recommendations from API") # Debug print
55+
response = fetch_api_data(
56+
"delist-recommender",
57+
"recommendations",
58+
retry=True
59+
)
6660
print("Successfully fetched data from API.") # Debug print
67-
return response.json()
68-
except requests.exceptions.Timeout:
69-
st.error(f"Error: Timeout connecting to backend API at {API_ENDPOINT}. The request took longer than 120 seconds.")
70-
return None
71-
except requests.exceptions.ConnectionError:
72-
st.error(f"Error: Could not connect to backend API at {API_ENDPOINT}. Please ensure the backend is running.")
73-
return None
74-
except requests.exceptions.RequestException as e:
75-
st.error(f"Error connecting to backend API: {e}")
76-
return None
61+
return response
7762
except Exception as e:
78-
st.error(f"An unexpected error occurred while fetching data: {e}")
63+
st.error(f"An error occurred while fetching data: {e}")
7964
return None
8065

8166
# --- Streamlit Page ---

0 commit comments

Comments
 (0)