-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_numbers.js
31 lines (26 loc) · 975 Bytes
/
list_numbers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use strict'
const rp = require('request-promise-native')
const fs = require('fs')
const file = 'numbers.txt'
const username = process.env['PLIVO_AUTH_ID']
const password = process.env['PLIVO_AUTH_TOKEN']
const auth = Buffer.from(`${username}:${password}`).toString('base64')
const initial = `/v1/Account/${username}/Number/`
const host = `https://api.plivo.com`
const headers = { 'Authorization': `Basic ${auth}` }
const defaults = {
method: 'GET',
headers,
json: true,
}
const fields = ['number', 'alias', 'number_type', 'application']
fs.appendFileSync(file, `${fields.join(',')}\n`)
const request = async (path) => {
if (path == null) return
const options = Object.assign({ uri: `${host}${path}` }, defaults)
const response = await rp(options)
const lines = response.objects.map(o => fields.map(f => o[f]).join(','))
fs.appendFileSync(file, `${lines.join('\n')}\n`)
request(response.meta.next)
}
request(initial).catch(console.error.bind(console))