-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.ts
63 lines (53 loc) · 2.12 KB
/
list.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { ElasticSipTrunking, PageResult } from '@sinch/sdk-core';
import { getPrintFormat, initElasticSipTrunkingService, printFullResponse } from '../../config';
const populateACLsList = (
aclPage: PageResult<ElasticSipTrunking.AccessControlList>,
aclsList: ElasticSipTrunking.AccessControlList[],
aclsDetailsList: string[],
) => {
aclPage.data.map((acl: ElasticSipTrunking.AccessControlList) => {
aclsList.push(acl);
aclsDetailsList.push(`${acl.id} - ${acl.name}`);
});
};
(async () => {
console.log('************************');
console.log('* getAccessControlList *');
console.log('************************');
const requestData: ElasticSipTrunking.ListAccessControlListRequestData = {};
const elasticSipTrunkingService = initElasticSipTrunkingService();
// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await elasticSipTrunkingService.accessControlList.list(requestData);
const aclsList: ElasticSipTrunking.AccessControlList[] = [];
const aclsDetailsList: string[] = [];
// Loop on all the pages to get all the active numbers
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateACLsList(response, aclsList, aclsDetailsList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}
const printFormat = getPrintFormat(process.argv);
if (printFormat === 'pretty') {
console.log(aclsDetailsList.length > 0
? 'List of ACLs:\n' + aclsDetailsList.join('\n')
: 'Sorry, no ACLs were found.');
} else {
printFullResponse(aclsList);
}
// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const acl of elasticSipTrunkingService.accessControlList.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`${acl.id} - ${acl.name}`);
} else {
console.log(acl);
}
}
})();