Repository: kiasambrook/tfl-live-tracker
Issue category: Performance Issue

Issue:
Cache Station List
### Summary
Currently the station list is refreshed everytime the program is launched, it would probably be best to cache this to improve performance of app and prevent being rate limited by API.

### Tasks
- Cache the list of stations
- Data stored should include ID, name, and location




Hints:

Patch:
diff --git a/app/api/tfl_api.py b/app/api/tfl_api.py
index 232b626..b688263 100644
--- a/app/api/tfl_api.py
+++ b/app/api/tfl_api.py
@@ -1,4 +1,5 @@
 import requests
+import requests_cache
 
 class TfLAPI:
     def __init__(self, api_key):
@@ -7,9 +8,16 @@ def __init__(self, api_key):
 
     def fetch_all_stop_points(self):
         """Fetch all stop points."""
+        session = requests_cache.CachedSession('stop_points_cache')
         url = f"{self.base_url}/StopPoint/Mode/tube?app_key={self.api_key}"
-        response = requests.get(url)
 
+        # Check if the response is in the cache
+        cached_response = session.cache.get_response(url)
+        if cached_response:
+            return cached_response.json()
+
+        # If not in cache, make the API call
+        response = session.get(url)
         if response.status_code == 200:
             return response.json()
         else:
diff --git a/app/ui/main_window.py b/app/ui/main_window.py
index 266b540..a1eb9e1 100644
--- a/app/ui/main_window.py
+++ b/app/ui/main_window.py
@@ -1,28 +1,33 @@
 from kivy.app import App
 from kivy.lang import Builder
+from kivy.uix.boxlayout import BoxLayout
 from ui.dropdown import Dropdown
+from api.tfl_api import TfLAPI
+from config import TFL_API_KEY
 
 
 class MainWindow(App):
     def build(self):
         # Load the KV file
         Builder.load_file("ui/main_window.kv")
+        main_layout = BoxLayout(orientation="vertical")
+
+        api = TfLAPI(TFL_API_KEY)
+        stop_points = api.fetch_all_stop_points()
 
         # Create the DropDown widget
         dropdown_widget = Dropdown()
+        dropdown_options = {}
 
-        # Define options as text-value pairs
-        dropdown_options = {
-            "Oxford": "1",
-            "Cambridge": "2",
-            "London": "3",
-            "Manchester": "4",
-        }
+        for stop_point in stop_points.get("stopPoints", []):
+            dropdown_options[stop_point.get("commonName")] = stop_point.get("id")
 
-        # Set the dropdown options dynamically
         dropdown_widget.dropdown_options = dropdown_options
 
-        return dropdown_widget
+        # Add the DropDown widget to the main layout
+        main_layout.add_widget(dropdown_widget)
+
+        return main_layout
 
 
 if __name__ == "__main__":
diff --git a/requirements.txt b/requirements.txt
index 4c8f79e..f473bcf 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -51,4 +51,5 @@ Werkzeug==3.1.3
 wsproto==1.2.0
 yarl==1.11.1
 requests-mock
-pytest
\ No newline at end of file
+pytest
+requests-cache
\ No newline at end of file


Test patch: