From f45dd933a0a12de3229d91ad9ba66be2f4d22af0 Mon Sep 17 00:00:00 2001
From: tm689 <tm689@cam.ac.uk>
Date: Tue, 4 Jul 2023 13:33:26 +0100
Subject: [PATCH] fix: handle KEY column, handle empty request result

---
 coordinator/ProcessorSurveysWRT.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/coordinator/ProcessorSurveysWRT.py b/coordinator/ProcessorSurveysWRT.py
index 62b86da..f3a6d6a 100644
--- a/coordinator/ProcessorSurveysWRT.py
+++ b/coordinator/ProcessorSurveysWRT.py
@@ -114,7 +114,6 @@ def nested_to_flattened(df):
             'live_stemrust_samples_count',
             'dead_yellowrust_samples_count',
             'SET-OF-live_leafrust_samples',
-            'KEY',
             'other_diseases_group-other_diseases',
             'survey_infromation-location-Altitude',
             'SET-OF-dead_stemrust_samples',
@@ -234,7 +233,11 @@ def get_WRT_form_as_csv(form_credentials: dict, jobPath: str, config: dict, stat
         logger.debug('Saving raw csv file')
 
         df_raw_filename = f"{output_path}/{csv_filename}"
-        dataframe_raw = json_normalize(request.json()["listObservation"])
+        # if request is not empty, than convert it to a normalized dataframe, otherwise create an empty dataframe
+        if request.status_code == 200:
+            dataframe_raw = json_normalize(request.json()["listObservation"])
+        if request.status_code == 204:
+            dataframe_raw = json_normalize([])
 
         dataframe_raw.to_csv(df_raw_filename,index=False,quoting=csv.QUOTE_MINIMAL)
 
-- 
GitLab