Updating the API functions to make the retry.

This commit is contained in:
Scott Idem
2025-05-07 16:17:34 -04:00
parent 7ea65ee495
commit cdc4ee1af9
2 changed files with 56 additions and 26 deletions

View File

@@ -22,7 +22,7 @@ export let get_object = async function get_object(
// The task_id value should be a random string that is unique to the task. This is used to identify the task in the message event. // The task_id value should be a random string that is unique to the task. This is used to identify the task in the message event.
task_id = crypto.randomUUID(), task_id = crypto.randomUUID(),
log_lvl = 0, log_lvl = 0,
retry_count = 3 // Number of retry attempts retry_count = 5 // Number of retry attempts
}: { }: {
api_cfg: any, api_cfg: any,
endpoint: string, endpoint: string,

View File

@@ -3,22 +3,30 @@ import axios from 'axios';
// Updated 2024-05-23 // Updated 2024-05-23
export let patch_object = async function patch_object( export let patch_object = async function patch_object(
{ {
api_cfg=null, api_cfg = null,
endpoint='', endpoint = '',
params={}, params = {},
data={}, data = {},
return_meta=false, return_meta = false,
log_lvl = 3 log_lvl = 2,
retry_count = 5 // Number of retry attempts
}: { }: {
api_cfg: any, api_cfg: any,
endpoint: string, endpoint: string,
params?: any, params?: any,
data?: any, data?: any,
return_meta?: boolean, return_meta?: boolean,
log_lvl?: number log_lvl?: number,
retry_count?: number
} }
) { ) {
console.log('*** patch_object() XXXX ***'); if (log_lvl) {
console.log(`*** patch_object() *** Endpoint: ${endpoint}`);
console.log('Params:', params);
if (log_lvl > 1) {
console.log('Data:', data);
}
}
if (log_lvl) { if (log_lvl) {
// console.log(api_cfg); // console.log(api_cfg);
@@ -31,30 +39,52 @@ if (log_lvl) {
// console.log(as_list); // console.log(as_list);
} }
if (!api_cfg) {
console.error('No API Config was provided. Returning false.');
return false;
}
let axios_api = axios.create({ let axios_api = axios.create({
baseURL: api_cfg['base_url'], baseURL: api_cfg['base_url'],
/* other custom settings */ /* other custom settings */
}); });
axios_api.defaults.headers = api_cfg['headers']; axios_api.defaults.headers = api_cfg['headers'];
let response_data = await axios_api.patch(endpoint, data, { params: params }) for (let attempt = 1; attempt <= retry_count; attempt++) {
.then(function (response) { try {
console.log(response.data); const response = await axios_api.patch(endpoint, data, { params: params });
return response.data['data']; if (log_lvl) {
//return response.data; console.log(`Response: status=${response.status} attempt=${attempt}`);
}) }
.catch(function (error) { if (log_lvl > 1) {
if (error.response && error.response.status === 404) { console.log('Response Data:', response.data);
return null; // Returning null since there were no results }
// Return the response data
return response.data['data'];
} catch (error) {
if (log_lvl) {
console.error(`Error on attempt ${attempt}:`, error);
}
// Handle specific errors (e.g., 404)
if (error.response && error.response.status === 404) {
console.warn('404 Not Found. Returning null.');
return null; // Returning null since there were no results
}
// If this is the last attempt, return false
if (attempt === retry_count) {
console.error('Max retry attempts reached. Returning false.');
return false;
}
// Log retry information
if (log_lvl) {
console.log(`Retrying... (${attempt}/${retry_count})`);
}
} }
console.log(error);
return false; // Returning false since something may have gone wrong. Also more in line with what the API returns.
// return error;
});
if (log_lvl > 1) {
console.log(response_data);
} }
return response_data; // return response_data;
} }