Spaces:
Sleeping
Sleeping
Update apis/Bubble_API_Calls.py
Browse files- apis/Bubble_API_Calls.py +20 -18
apis/Bubble_API_Calls.py
CHANGED
@@ -151,13 +151,11 @@ def fetch_linkedin_posts_data_from_bubble(org_urn: str, data_type:str):
|
|
151 |
def bulk_upload_to_bubble(data, data_type):
|
152 |
"""
|
153 |
Uploads a list of dictionaries to a specified Bubble data type using the bulk endpoint.
|
154 |
-
|
155 |
Args:
|
156 |
data (list): A list of dictionaries, where each dictionary represents a record.
|
157 |
data_type (str): The name of the Bubble data type (table) to upload to.
|
158 |
-
|
159 |
Returns:
|
160 |
-
list: A list of
|
161 |
bool: False if the upload fails.
|
162 |
"""
|
163 |
api_token = os.environ.get("Bubble_API")
|
@@ -168,35 +166,39 @@ def bulk_upload_to_bubble(data, data_type):
|
|
168 |
url = f"https://app.ingaze.ai/version-test/api/1.1/obj/{data_type}/bulk"
|
169 |
headers = {
|
170 |
"Authorization": f"Bearer {api_token}",
|
171 |
-
|
172 |
-
"Content-Type": "text/plain"
|
173 |
}
|
174 |
-
|
175 |
-
# Convert the list of dictionaries into a single string,
|
176 |
-
# with each JSON object separated by a newline character.
|
177 |
payload = "\n".join(json.dumps(item) for item in data)
|
178 |
-
|
179 |
logging.info(f"Sending bulk payload to Bubble data type: {data_type}")
|
180 |
-
# print("Payload being sent:\n" + payload) # Uncomment for deep debugging
|
181 |
|
182 |
try:
|
183 |
response = requests.post(url, headers=headers, data=payload.encode('utf-8'))
|
184 |
-
|
185 |
-
# Raise an exception for bad status codes (4xx or 5xx)
|
186 |
response.raise_for_status()
|
187 |
|
188 |
-
#
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
except requests.exceptions.HTTPError as http_err:
|
195 |
logger.error(f"HTTP error occurred: {http_err}")
|
196 |
logger.error(f"Failed to upload data to {data_type}. Status Code: {response.status_code}, Response: {response.text}")
|
197 |
return False
|
|
|
|
|
|
|
|
|
198 |
except Exception as err:
|
199 |
-
logger.error(f"An other error occurred: {err}")
|
200 |
return False
|
201 |
|
202 |
def update_record_in_bubble(table_name, record_id, payload_to_update):
|
|
|
151 |
def bulk_upload_to_bubble(data, data_type):
|
152 |
"""
|
153 |
Uploads a list of dictionaries to a specified Bubble data type using the bulk endpoint.
|
|
|
154 |
Args:
|
155 |
data (list): A list of dictionaries, where each dictionary represents a record.
|
156 |
data_type (str): The name of the Bubble data type (table) to upload to.
|
|
|
157 |
Returns:
|
158 |
+
list: A list of dictionaries (each containing an 'id') for the created records if successful.
|
159 |
bool: False if the upload fails.
|
160 |
"""
|
161 |
api_token = os.environ.get("Bubble_API")
|
|
|
166 |
url = f"https://app.ingaze.ai/version-test/api/1.1/obj/{data_type}/bulk"
|
167 |
headers = {
|
168 |
"Authorization": f"Bearer {api_token}",
|
169 |
+
"Content-Type": "text/plain"
|
|
|
170 |
}
|
|
|
|
|
|
|
171 |
payload = "\n".join(json.dumps(item) for item in data)
|
|
|
172 |
logging.info(f"Sending bulk payload to Bubble data type: {data_type}")
|
|
|
173 |
|
174 |
try:
|
175 |
response = requests.post(url, headers=headers, data=payload.encode('utf-8'))
|
|
|
|
|
176 |
response.raise_for_status()
|
177 |
|
178 |
+
# FIX: Handle the newline-delimited JSON response from Bubble.
|
179 |
+
response_text = response.text.strip()
|
180 |
+
if not response_text:
|
181 |
+
logger.warning(f"Upload to {data_type} was successful but returned an empty response.")
|
182 |
+
return [] # Return an empty list for success with no content
|
183 |
+
|
184 |
+
created_records = []
|
185 |
+
for line in response_text.splitlines():
|
186 |
+
if line: # Ensure the line is not empty
|
187 |
+
created_records.append(json.loads(line))
|
188 |
+
|
189 |
+
logging.info(f"Successfully uploaded {len(created_records)} records to {data_type}.")
|
190 |
+
return created_records
|
191 |
|
192 |
except requests.exceptions.HTTPError as http_err:
|
193 |
logger.error(f"HTTP error occurred: {http_err}")
|
194 |
logger.error(f"Failed to upload data to {data_type}. Status Code: {response.status_code}, Response: {response.text}")
|
195 |
return False
|
196 |
+
except json.JSONDecodeError as json_err:
|
197 |
+
# This error is what you were seeing. We log it in case the format changes again.
|
198 |
+
logger.error(f"JSON decoding failed: {json_err}. Response text: {response.text}")
|
199 |
+
return False
|
200 |
except Exception as err:
|
201 |
+
logger.error(f"An other error occurred: {err}", exc_info=True)
|
202 |
return False
|
203 |
|
204 |
def update_record_in_bubble(table_name, record_id, payload_to_update):
|