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') } }