From 84156a1ce9016eac0ee34fc29fac2a20ff55d5e72f932db36d5001382d682537 Mon Sep 17 00:00:00 2001
From: Moses Rolston <moses.rolston@gmail.com>
Date: Sat, 8 Mar 2025 15:13:55 -0800
Subject: [PATCH] legislation cache working now too

---
 api/endpoints/get_sponsored.py | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/api/endpoints/get_sponsored.py b/api/endpoints/get_sponsored.py
index 12b1b90..f78991f 100644
--- a/api/endpoints/get_sponsored.py
+++ b/api/endpoints/get_sponsored.py
@@ -61,7 +61,14 @@ def get_sponsored():
             neo4j_logger.error(f"Failed to fetch sponsored legislation for bioguideId {current_bioguideId}: Status Code {response.status_code}, Response: {response.text}")
             continue
 
-        sponsored_legislation = response.json().get('sponsoredLegislation', [])
+        response_data = response.json()
+
+        # Ensure the response contains 'sponsoredLegislation' key
+        if 'sponsoredLegislation' not in response_data:
+            neo4j_logger.error(f"Missing 'sponsoredLegislation' key in response for bioguideId {current_bioguideId}: {response_data}")
+            continue
+
+        sponsored_legislation = response_data['sponsoredLegislation']
 
         # Step 3: Store each piece of legislation in the cache along with the sponsor bioguideId
         for legislation in sponsored_legislation:
@@ -74,15 +81,23 @@ def get_sponsored():
 
             if key not in cache:
                 # Ensure all nested dictionaries are handled properly
+                latest_action = legislation.get('latestAction')
+
+                # Check if latest_action is None before calling .get()
+                if latest_action is None:
+                    latest_action = {}
+
+                policy_area = legislation.get('policyArea', {})
+
                 legislation_info = {
                     'bioguideId': current_bioguideId,
                     **legislation,
                     'latestAction': {
-                        'actionDate': legislation['latestAction']['actionDate'],
-                        'text': legislation['latestAction']['text']
+                        'actionDate': latest_action.get('actionDate'),
+                        'text': latest_action.get('text')
                     },
                     'policyArea': {
-                        'name': legislation['policyArea']['name']
+                        'name': policy_area.get('name')
                     }
                 }