GuglielmoTor commited on
Commit
627b699
·
verified ·
1 Parent(s): f61a34c

Update apis/Bubble_API_Calls.py

Browse files
Files changed (1) hide show
  1. apis/Bubble_API_Calls.py +62 -10
apis/Bubble_API_Calls.py CHANGED
@@ -126,25 +126,77 @@ def fetch_linkedin_posts_data_from_bubble(org_urn: str, data_type:str):
126
  return None, error_msg
127
 
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  def bulk_upload_to_bubble(data, data_type):
 
 
 
 
 
 
 
 
 
 
 
130
  api_token = os.environ.get("Bubble_API")
 
 
 
 
131
  url = f"https://app.ingaze.ai/version-test/api/1.1/obj/{data_type}/bulk"
132
  headers = {
133
  "Authorization": f"Bearer {api_token}",
134
- "Content-Type": "text/plain"
 
135
  }
136
 
137
- # Convert list of dicts to newline-separated JSON strings
 
138
  payload = "\n".join(json.dumps(item) for item in data)
139
- response = requests.post(url, headers=headers, data=payload)
140
 
141
- print("Payload being sent:")
142
- print(payload)
143
-
144
- if response.status_code == 200:
145
- print(f"Successfully uploaded {len(data)} records to {data_type}.")
146
- else:
147
- print(f"Failed to upload data to {data_type}. Status Code: {response.status_code}, Response: {response.text}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
  def update_record_in_bubble(table_name, record_id, payload_to_update):
150
  """
 
126
  return None, error_msg
127
 
128
 
129
+ # def bulk_upload_to_bubble(data, data_type):
130
+ # api_token = os.environ.get("Bubble_API")
131
+ # url = f"https://app.ingaze.ai/version-test/api/1.1/obj/{data_type}/bulk"
132
+ # headers = {
133
+ # "Authorization": f"Bearer {api_token}",
134
+ # "Content-Type": "text/plain"
135
+ # }
136
+
137
+ # # Convert list of dicts to newline-separated JSON strings
138
+ # payload = "\n".join(json.dumps(item) for item in data)
139
+ # response = requests.post(url, headers=headers, data=payload)
140
+
141
+ # print("Payload being sent:")
142
+ # print(payload)
143
+
144
+ # if response.status_code == 200:
145
+ # print(f"Successfully uploaded {len(data)} records to {data_type}.")
146
+ # else:
147
+ # print(f"Failed to upload data to {data_type}. Status Code: {response.status_code}, Response: {response.text}")
148
+
149
+ #versione f49ffdd ultima che funzionava per upload dati linkedin
150
  def bulk_upload_to_bubble(data, data_type):
151
+ """
152
+ Uploads a list of dictionaries to a specified Bubble data type using the bulk endpoint.
153
+
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
+
158
+ Returns:
159
+ list: A list of unique Bubble IDs for the created records if successful.
160
+ bool: False if the upload fails.
161
+ """
162
  api_token = os.environ.get("Bubble_API")
163
+ if not api_token:
164
+ logger.error("Bubble_API environment variable not set.")
165
+ return False
166
+
167
  url = f"https://app.ingaze.ai/version-test/api/1.1/obj/{data_type}/bulk"
168
  headers = {
169
  "Authorization": f"Bearer {api_token}",
170
+ # The bulk endpoint expects newline-delimited JSON objects with a text/plain Content-Type
171
+ "Content-Type": "text/plain"
172
  }
173
 
174
+ # Convert the list of dictionaries into a single string,
175
+ # with each JSON object separated by a newline character.
176
  payload = "\n".join(json.dumps(item) for item in data)
 
177
 
178
+ logger.info(f"Sending bulk payload to Bubble data type: {data_type}")
179
+ # print("Payload being sent:\n" + payload) # Uncomment for deep debugging
180
+
181
+ try:
182
+ response = requests.post(url, headers=headers, data=payload.encode('utf-8'))
183
+
184
+ # Raise an exception for bad status codes (4xx or 5xx)
185
+ response.raise_for_status()
186
+
187
+ # If the request was successful (200 OK), Bubble returns a JSON array of the new record IDs.
188
+ # We parse the JSON response to get this list.
189
+ created_ids = response.json()
190
+ logger.info(f"Successfully uploaded {len(data)} records to {data_type}.")
191
+ return created_ids
192
+
193
+ except requests.exceptions.HTTPError as http_err:
194
+ logger.error(f"HTTP error occurred: {http_err}")
195
+ logger.error(f"Failed to upload data to {data_type}. Status Code: {response.status_code}, Response: {response.text}")
196
+ return False
197
+ except Exception as err:
198
+ logger.error(f"An other error occurred: {err}")
199
+ return False
200
 
201
  def update_record_in_bubble(table_name, record_id, payload_to_update):
202
  """