I can't understand this after a lot of head-scratching. What am I missing?
Pagerduty returns data when doing a call on Postman (in this example returns 2 objects)
But when doing the same call on a NodeJs app, it returns no data and with reseted created_at_start and created_at_end values
$ node gen.js -r incident --serviceId=PM7EKHT
Fetching incident...
Incident Data:
{
aggregate_unit: null,
data: [],
filters: {
created_at_end: '2023-09-14T17:32:38.170522Z',
created_at_start: '2023-09-13T17:32:38.170482Z'
},
order: 'desc',
order_by: 'total_incident_count',
time_zone: 'Etc/UTC'
}
My JS code is this:
async function fetchIncidentsByService(serviceId) {
try {
const loadingInterval = loadingAnimation('Fetching incident...');
const postData = {
filters: {
created_at_start: "2023-08-01T00:00:00-05:00",
created_at_end: "2023-08-31T00:00:00-05:00",
service_ids: [ serviceId ]
},
aggregate_unit: "week",
time_zone: "Etc/UTC"
}
const response = await makeRequest(apiKey, '/analytics/metrics/incidents/services', 'POST', postData);
clearInterval(loadingInterval);
console.log('\nIncident Data:');
console.log(response);
} catch (error) {
console.error(error);
}
}
With the makeRequest function on a separate module:
const https = require('https');
async function makeRequest(apiKey, path, method = 'GET', postData = null) {
return new Promise((resolve, reject) => {
const options = {
hostname: 'api.pagerduty.com',
path: path,
method: method,
headers: {
'Authorization': `Token token=${apiKey}`,
'Accept': 'application/vnd.pagerduty+json;version=2',
'Content-type': 'application/json',
'X-EARLY-ACCESS': 'analytics-v2'
},
};
if (method === 'POST' && postData !== null ) {
options.body = JSON.stringify(postData);
}
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(JSON.parse(data));
} else {
reject(new Error(`Request failed with status code ${res.statusCode}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
req.end();
});
}
module.exports = { makeRequest };
