This topic was automatically generated from Slack. You can find the original thread here.
Can you see anything wrong with this code?
import { axios } from "@pipedream/platform";
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function makeRequestWithRetry($, url, headers, body, delay, max_retries) {
let retries = 0;
while (retries < max_retries) {
try {
const response = await axios($, {
method: "POST",
url: url,
headers: headers,
data: body,
});
return response;
} catch (error) {
if (error.response && error.response.status === 429) { // Rate limit error
const retry_after = error.response.headers['retry-after'] ? parseInt(error.response.headers['retry-after']) * 1000 : delay;
console.log(`Rate limit hit, retrying after ${retry_after} milliseconds...`);
await sleep(retry_after);
retries++;
} else {
throw error;
}
}
}
throw new Error("Max retries reached");
}
export default defineComponent({
props: {
hubspot: {
type: "app",
app: "hubspot",
},
},
async run({steps, $}) {
const url = `https://api.hubapi.com/crm/v3/objects/line_items/search`;
const headers = {
Authorization: `Bearer ${this.hubspot.$auth.oauth_access_token}`,
"Content-Type": "application/json",
};
const body = {
filterGroups: [],
properties: ["name", "quantity", "price", "hs_product_id"],
limit: 100,
};
let line_items = [];
let has_more = true;
let after = 0;
const delay = 1000; // Initial delay of 1 second
const max_retries = 5;
while (has_more) {
body["after"] = after;
const response = await makeRequestWithRetry($, url, headers, body, delay, max_retries);
const data = response.data;
if (data && data.results) {
for (const item of data.results) {
const item_data = item.properties;
item_data["id"] = item.id;
// Fetch associations separately
const assoc_url = `https://api.hubapi.com/crm/v3/objects/line_items/${item['id']}/associations/deals`;
try {
const assoc_response = await axios($, {
method: "GET",
url: assoc_url,
headers: headers,
});
const assoc_data = assoc_response.data;
if (assoc_data && assoc_data.results && assoc_data.results.length > 0) {
item_data["deal_id"] = assoc_data.results[0].id;
} else {
item_data["deal_id"] = null;
}
} catch (error) {
console.log(`Failed to fetch associations for line item ${item['id']}`);
item_data["deal_id"] = null;
}
line_items.push(item_data);
}
}
const paging = data.paging || {};
has_more = "next" in paging;
after = paging.next ? paging.next.after : 0;
await sleep(delay); // Wait for the delay before the next iteration
}
line_items = line_items.filter(item => "deal_id" in item);
return line_items;
},
});