GuglielmoTor commited on
Commit
284b445
·
verified ·
1 Parent(s): 0612e1d

Update Bubble_API_Calls.py

Browse files
Files changed (1) hide show
  1. Bubble_API_Calls.py +51 -0
Bubble_API_Calls.py CHANGED
@@ -145,3 +145,54 @@ def bulk_upload_to_bubble(data, data_type):
145
  else:
146
  print(f"Failed to upload data to {data_type}. Status Code: {response.status_code}, Response: {response.text}")
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  else:
146
  print(f"Failed to upload data to {data_type}. Status Code: {response.status_code}, Response: {response.text}")
147
 
148
+ def update_record_in_bubble(table_name, record_id, payload_to_update):
149
+ """
150
+ Updates an existing record in a Bubble.io table using a PATCH request.
151
+
152
+ Args:
153
+ table_name (str): The name of the Bubble table (e.g., "User", "Product").
154
+ record_id (str): The unique ID of the record to update.
155
+ payload_to_update (dict): A dictionary where keys are field names (slugs)
156
+ and values are the new values for those fields.
157
+ Returns:
158
+ bool: True if the update was successful, False otherwise.
159
+ """
160
+ bubble_api_key = os.environ.get("Bubble_API")
161
+ if not record_id:
162
+ logging.error(f"Record ID is missing. Cannot update record in Bubble table '{table_name}'.")
163
+ return False
164
+ if not payload_to_update:
165
+ logging.warning(f"Payload to update is empty for record_id '{record_id}' in Bubble table '{table_name}'. Nothing to update.")
166
+ # Consider this a success as there's no error, just no action.
167
+ # Depending on desired behavior, you might return False or raise an error.
168
+ return True
169
+
170
+ # Construct the API endpoint for a specific record
171
+ # Example: https://<app_name>.bubbleapps.io/api/1.1/obj/<table_name>/<record_id>
172
+ api_endpoint = f"https://app.ingaze.ai/version-test/api/1.1/obj/{table_name}/{record_id}"
173
+ headers = {
174
+ "Authorization": f"Bearer {bubble_api_key}",
175
+ "Content-Type": "application/json"
176
+ }
177
+
178
+ logging.debug(f"Attempting to update record '{record_id}' in table '{table_name}' at endpoint '{api_endpoint}' with payload: {payload_to_update}")
179
+
180
+ try:
181
+ response = requests.patch(api_endpoint, json=payload_to_update, headers=headers)
182
+ response.raise_for_status() # Raises an HTTPError for bad responses (4XX or 5XX)
183
+ logging.info(f"Successfully updated record '{record_id}' in Bubble table '{table_name}'.")
184
+ return True
185
+ except requests.exceptions.HTTPError as http_err:
186
+ # Log more details from the response if available
187
+ error_details = ""
188
+ try:
189
+ error_details = response.json() # Bubble often returns JSON errors
190
+ except ValueError: # If response is not JSON
191
+ error_details = response.text
192
+ logging.error(f"HTTP error occurred while updating record '{record_id}' in '{table_name}': {http_err}. Response: {error_details}")
193
+ except requests.exceptions.RequestException as req_err:
194
+ logging.error(f"Request exception occurred while updating record '{record_id}' in '{table_name}': {req_err}")
195
+ except Exception as e:
196
+ logging.error(f"An unexpected error occurred while updating record '{record_id}' in '{table_name}': {e}", exc_info=True)
197
+
198
+ return False