|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
| 3 | +from uuid import uuid4 |
3 | 4 | from black import Any |
4 | 5 | from wasabi import msg |
5 | 6 | import pandas as pd |
@@ -214,6 +215,44 @@ def post_associations( |
214 | 215 | ) |
215 | 216 | return api_response |
216 | 217 |
|
| 218 | + def post_records(self, records: List[Dict[str, Any]]): |
| 219 | + """Posts records to the server. |
| 220 | +
|
| 221 | + Args: |
| 222 | + records (List[Dict[str, str]]): List of records to post. |
| 223 | + """ |
| 224 | + request_uuid = str(uuid4()) |
| 225 | + url = settings.get_import_json_url(self.project_id) |
| 226 | + |
| 227 | + batch_responses = [] |
| 228 | + for records_batch in util.batch(records, settings.BATCH_SIZE_DEFAULT): |
| 229 | + api_response = api_calls.post_request( |
| 230 | + url, |
| 231 | + { |
| 232 | + "request_uuid": request_uuid, |
| 233 | + "records": records_batch, |
| 234 | + "is_last": False, |
| 235 | + }, |
| 236 | + self.session_token, |
| 237 | + ) |
| 238 | + batch_responses.append(api_response) |
| 239 | + time.sleep(0.5) # wait half a second to avoid server overload |
| 240 | + api_calls.post_request( |
| 241 | + url, |
| 242 | + {"request_uuid": request_uuid, "records": [], "is_last": True}, |
| 243 | + self.session_token, |
| 244 | + ) |
| 245 | + return batch_responses |
| 246 | + |
| 247 | + def post_df(self, df: pd.DataFrame): |
| 248 | + """Posts a DataFrame to the server. |
| 249 | +
|
| 250 | + Args: |
| 251 | + df (pd.DataFrame): DataFrame to post. |
| 252 | + """ |
| 253 | + records = df.to_dict(orient="records") |
| 254 | + return self.post_records(records) |
| 255 | + |
217 | 256 | def post_file_import( |
218 | 257 | self, path: str, import_file_options: Optional[str] = "" |
219 | 258 | ) -> bool: |
@@ -246,7 +285,7 @@ def post_file_import( |
246 | 285 | endpoint = config_api_response.get("KERN_S3_ENDPOINT") |
247 | 286 |
|
248 | 287 | # credentials |
249 | | - credentials_url = settings.get_import_url(self.project_id) |
| 288 | + credentials_url = settings.get_import_file_url(self.project_id) |
250 | 289 | credentials_api_response = api_calls.post_request( |
251 | 290 | credentials_url, |
252 | 291 | { |
|
0 commit comments