Created
November 22, 2017 14:59
-
-
Save mderazon/58f12a7f225b4a83f86fb20e45d67c66 to your computer and use it in GitHub Desktop.
Downloads all DMARC reports from Postmark in XML format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const req = require('superagent'); | |
const writeFile = require('util').promisify(require('fs').writeFile); | |
const apiToken = process.env.API_TOKEN; | |
const baseUrl = 'https://dmarc.postmarkapp.com'; | |
(async function() { | |
const reports = []; | |
// get a list of all reports by id and create time | |
async function getReportList(path) { | |
if (path === '/records/my/reports?') { | |
// this means we are finished and no more entries | |
return; | |
} | |
console.log('GET ' + baseUrl + path); | |
try { | |
let res = await req.get(baseUrl + path).set('X-API-Token', apiToken); | |
res.body.entries.forEach(e => { | |
reports.push({ | |
id: e.id, | |
time: e.created_at, | |
org: e.organization_name | |
}); | |
}); | |
await getReportList(res.body.meta.next_url); | |
} catch (err) { | |
console.error(err.message); | |
} | |
} | |
// download a report in xml format | |
async function getReport(report) { | |
try { | |
let res = await req | |
.get(`${baseUrl}/records/my/reports/${report.id}`) | |
.set('X-API-Token', apiToken) | |
.set('Accept', 'application/xml') | |
.buffer() | |
.type('xml'); | |
await writeFile( | |
`${__dirname}/${report.time}-${report.org}-${report.id}.xml`, | |
res.text | |
); | |
} catch (err) { | |
console.error(err.message); | |
} | |
} | |
try { | |
console.log('Getting a list of all reports...'); | |
await getReportList('/records/my/reports'); | |
const gets = reports.map(e => getReport(e)); | |
console.log('Downloading reports...'); | |
await Promise.all(gets); | |
} catch (err) { | |
console.error(err.message); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment