Skip to content

Commit

Permalink
more robust api fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
nils committed Jan 2, 2024
1 parent 064b2c0 commit c0c72ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion motie-cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ done

echo "Start caching from https://gegevensmagazijn.tweedekamer.nl/OData/v4/2.0"

node motie-cache2.js 50
node motie-cache2.js
26 changes: 18 additions & 8 deletions motie-cache2.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ const motieCard = `---
nextLink = await fetch(nextLink)
.then(response => {
if (response.status != 200) {
console.log(response);
console.dir(response, {'depth': null});
return Promise.reject(response);
}
return response;
return response.json();
})
.then(response => response.json())
.then(odata => {
if (typeof odata.value === 'undefined') {
console.log(odata);
return Promise.reject(odata.error);
}
let nextLink = undefined;
if (typeof odata['@odata.nextLink'] !== 'undefined') {
// If there is more data to retrieve, nextLink is set with the correct $skip value
Expand Down Expand Up @@ -136,10 +140,10 @@ const motieCard = `---
// No file, return empty
resolve({});
} else {
reject(err); // in the case of error, control flow goes to the catch block with the error occured.
reject(err);
}
} else {
resolve(JSON.parse(data)); // in the case of success, control flow goes to the then block with the content of the file.
resolve(JSON.parse(data));
}
});
})
Expand All @@ -154,14 +158,20 @@ const motieCard = `---
}

return nextLink;
}).catch(error => {
// Catch it to retry, seems to happen every now and then
console.log(error);
if (typeof error.cause !== 'undefined' && error.cause.code === 'ECONNRESET') {
console.log(`Error while fetching ${url}, returning it to try again`)
return url;
} else {
throw error;
}
})


// Stop if there are no more odata links, or motieTotal has been reached
if (typeof nextLink == 'undefined' || motieCount > motieTotal) {
break;
}
}
// fs.writeFile(`_data/moties/${year}.json`,
// JSON.stringify(motieCache), 'utf8', (err) => { if (err) throw err });
})();

0 comments on commit c0c72ad

Please sign in to comment.