Updating the API functions to make the retry.
This commit is contained in:
@@ -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.
|
||||
task_id = crypto.randomUUID(),
|
||||
log_lvl = 0,
|
||||
retry_count = 3 // Number of retry attempts
|
||||
retry_count = 5 // Number of retry attempts
|
||||
}: {
|
||||
api_cfg: any,
|
||||
endpoint: string,
|
||||
|
||||
@@ -3,22 +3,30 @@ import axios from 'axios';
|
||||
// Updated 2024-05-23
|
||||
export let patch_object = async function patch_object(
|
||||
{
|
||||
api_cfg=null,
|
||||
endpoint='',
|
||||
params={},
|
||||
data={},
|
||||
return_meta=false,
|
||||
log_lvl = 3
|
||||
api_cfg = null,
|
||||
endpoint = '',
|
||||
params = {},
|
||||
data = {},
|
||||
return_meta = false,
|
||||
log_lvl = 2,
|
||||
retry_count = 5 // Number of retry attempts
|
||||
}: {
|
||||
api_cfg: any,
|
||||
endpoint: string,
|
||||
params?: any,
|
||||
data?: any,
|
||||
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) {
|
||||
// console.log(api_cfg);
|
||||
@@ -31,30 +39,52 @@ if (log_lvl) {
|
||||
// console.log(as_list);
|
||||
}
|
||||
|
||||
if (!api_cfg) {
|
||||
console.error('No API Config was provided. Returning false.');
|
||||
return false;
|
||||
}
|
||||
|
||||
let axios_api = axios.create({
|
||||
baseURL: api_cfg['base_url'],
|
||||
/* other custom settings */
|
||||
});
|
||||
axios_api.defaults.headers = api_cfg['headers'];
|
||||
|
||||
let response_data = await axios_api.patch(endpoint, data, { params: params })
|
||||
.then(function (response) {
|
||||
console.log(response.data);
|
||||
return response.data['data'];
|
||||
//return response.data;
|
||||
})
|
||||
.catch(function (error) {
|
||||
if (error.response && error.response.status === 404) {
|
||||
return null; // Returning null since there were no results
|
||||
for (let attempt = 1; attempt <= retry_count; attempt++) {
|
||||
try {
|
||||
const response = await axios_api.patch(endpoint, data, { params: params });
|
||||
if (log_lvl) {
|
||||
console.log(`Response: status=${response.status} attempt=${attempt}`);
|
||||
}
|
||||
if (log_lvl > 1) {
|
||||
console.log('Response Data:', response.data);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user