Skip to content

Commit

Permalink
DEVEXP-406: Add support for SIP Trunks (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch authored Apr 30, 2024
1 parent 4226f20 commit 28f5668
Show file tree
Hide file tree
Showing 26 changed files with 1,024 additions and 8 deletions.
15 changes: 15 additions & 0 deletions examples/simple-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ FAX_SERVICE_ID=serviceId to fill with one the fax services created with the Fax
FAX_ID=id from a sendFax response
FAX_CALLBACK_URL=callback url to override the one defined in the default service or specified service
FAX_EMAIL=email to associate with a phone number to use the fax-to-email functionality
## Elastic SIP Trunking API
SIP_TRUNK_ID=sipTrunkId to fill with one of the SIP trunk created with the Elastic SIP Trunking API
ACL_ID=accessControlList Id to fill with one of the access control list created with the Elastic SIP Trunking API
```

**Note**: If you prefer using environment variables, the sample app is also supporting them: they take precedence over the value from the `.env` file.
Expand Down Expand Up @@ -270,3 +273,15 @@ yarn run numbers:regions:list
| | [./src/fax/emails/update.ts](./src/fax/emails/update.ts) | `FAX_EMAIL` + `PHONE_NUMBER` |
| | [./src/fax/emails/delete.ts](./src/fax/emails/delete.ts) | `FAX_EMAIL` |

### Elastic SIP Trunk

| Service | Sample application name and location | Required parameters |
|------------|----------------------------------------------------------------------------------------------------------|----------------------|
| SIP Trunks | [./src/elastic-sip-trunking/sip-trunks/create.ts](./src/elastic-sip-trunking/sip-trunks/create.ts) | |
| | [./src/elastic-sip-trunking/sip-trunks/get.ts](./src/elastic-sip-trunking/sip-trunks/get.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/list.ts](./src/elastic-sip-trunking/sip-trunks/list.ts) | |
| | [./src/elastic-sip-trunking/sip-trunks/update.ts](./src/elastic-sip-trunking/sip-trunks/update.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/delete.ts](./src/elastic-sip-trunking/sip-trunks/delete.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/addACL.ts](./src/elastic-sip-trunking/sip-trunks/addACL.ts) | SIP_TRUNK_ID, ACL_ID |
| | [./src/elastic-sip-trunking/sip-trunks/listACLs.ts](./src/elastic-sip-trunking/sip-trunks/listACLs.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/deleteACL.ts](./src/elastic-sip-trunking/sip-trunks/deleteACL.ts) | SIP_TRUNK_ID, ACL_ID |
8 changes: 8 additions & 0 deletions examples/simple-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
"conversation:templatev2:listTranslations": "ts-node src/conversation/templates-v2/list-translations.ts",
"conversation:templatev2:update": "ts-node src/conversation/templates-v2/update.ts",
"conversation:templatev2:delete": "ts-node src/conversation/templates-v2/delete.ts",
"elasticSipTrunks:sipTrunk:create": "ts-node src/elastic-sip-trunking/sip-trunks/create.ts",
"elasticSipTrunks:sipTrunk:get": "ts-node src/elastic-sip-trunking/sip-trunks/get.ts",
"elasticSipTrunks:sipTrunk:list": "ts-node src/elastic-sip-trunking/sip-trunks/list.ts",
"elasticSipTrunks:sipTrunk:update": "ts-node src/elastic-sip-trunking/sip-trunks/update.ts",
"elasticSipTrunks:sipTrunk:delete": "ts-node src/elastic-sip-trunking/sip-trunks/delete.ts",
"elasticSipTrunks:sipTrunk:addACL": "ts-node src/elastic-sip-trunking/sip-trunks/addACL.ts",
"elasticSipTrunks:sipTrunk:listACLs": "ts-node src/elastic-sip-trunking/sip-trunks/listACLs.ts",
"elasticSipTrunks:sipTrunk:deleteACL": "ts-node src/elastic-sip-trunking/sip-trunks/deleteACL.ts",
"fax:services:create": "ts-node src/fax/services/create.ts",
"fax:services:get": "ts-node src/fax/services/get.ts",
"fax:services:list": "ts-node src/fax/services/list.ts",
Expand Down
15 changes: 14 additions & 1 deletion examples/simple-examples/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
SmsService,
VerificationService,
VoiceService,
ElasticSipTrunkingService,
} from '@sinch/sdk-core';
require('dotenv').config();

const initClient = (): Pick<SinchClient, 'conversation' | 'fax' | 'numbers' | 'sms'> => {
const initClient = (): Pick<SinchClient, 'conversation' | 'elasticSipTrunking' | 'fax' | 'numbers' | 'sms'> => {
const keyId = process.env.SINCH_KEY_ID || '';
const keySecret = process.env.SINCH_KEY_SECRET || '';
const projectId = process.env.SINCH_PROJECT_ID || '';
Expand All @@ -21,6 +22,10 @@ export const initConversationService = (): ConversationService => {
return initClient().conversation;
};

export const initElasticSipTrunkingService = (): ElasticSipTrunkingService => {
return initClient().elasticSipTrunking;
};

export const initFaxService = (): FaxService => {
return initClient().fax;
};
Expand Down Expand Up @@ -198,6 +203,14 @@ export const getFaxEmailFromConfig = () => {
return readVariable('FAX_EMAIL');
};

export const getSipTrunkIdFromConfig = () => {
return readVariable('SIP_TRUNK_ID');
};

export const getAccessControlListIdFromConfig = () => {
return readVariable('ACL_ID');
};

const readVariable = ( name: string): string => {
const value = process.env[name];
if (!value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import {
getAccessControlListIdFromConfig,
getPrintFormat,
getSipTrunkIdFromConfig,
initElasticSipTrunkingService,
printFullResponse,
} from '../../config';

(async () => {
console.log('*******************************');
console.log('* addAccessControlListToTrunk *');
console.log('*******************************');

const sipTrunkId = getSipTrunkIdFromConfig();
const aclId = getAccessControlListIdFromConfig();

const requestData: ElasticSipTrunking.AddAccessControlListToTrunkRequestData = {
trunkId: sipTrunkId,
addAccessControlListToTrunkRequestBody: {
accessControlListIds: [
aclId,
],
},
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
const response = await elasticSipTrunkingService.sipTrunks.addAccessControlList(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`The SIP trunk with the id '${requestData.trunkId}' contains the following ACL IDs:\n - ${response.accessControlListIds?.join('\n - ')}`);
} else {
printFullResponse(response);
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import { getPrintFormat, initElasticSipTrunkingService, printFullResponse } from '../../config';

(async () => {
console.log('******************');
console.log('* createSipTrunk *');
console.log('******************');

const requestData: ElasticSipTrunking.CreateSipTrunkRequestData = {
createSipTrunkRequestBody: {
name: 'Node.js SDK Sinch Trunk',
hostName: 'node-js-sdk-sinch',
},
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
const response = await elasticSipTrunkingService.sipTrunks.create(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`New SIP trunk created with the id '${response.id}'`);
} else {
printFullResponse(response);
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import { getSipTrunkIdFromConfig, initElasticSipTrunkingService } from '../../config';

(async () => {
console.log('******************');
console.log('* deleteSipTrunk *');
console.log('******************');

const sipTrunkId = getSipTrunkIdFromConfig();

const requestData: ElasticSipTrunking.DeleteSipTrunkRequestData = {
sipTrunkId,
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
await elasticSipTrunkingService.sipTrunks.delete(requestData);

console.log(`The SIP trunk with the id '${requestData.sipTrunkId}' has been deleted.`);

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import {
getAccessControlListIdFromConfig,
getSipTrunkIdFromConfig,
initElasticSipTrunkingService,
} from '../../config';

(async () => {
console.log('************************************');
console.log('* deleteAccessControlListFromTrunk *');
console.log('************************************');

const sipTrunkId = getSipTrunkIdFromConfig();
const aclId = getAccessControlListIdFromConfig();

const requestData: ElasticSipTrunking.DeleteAccessControlListFromTrunkRequestData = {
trunkId: sipTrunkId,
accessControlListId: aclId,
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
await elasticSipTrunkingService.sipTrunks.deleteAccessControlList(requestData);

console.log(`The ACL '${requestData.accessControlListId}' has been removed fromm the SIP trunk '${requestData.trunkId}'`);

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import {
getPrintFormat,
getSipTrunkIdFromConfig,
initElasticSipTrunkingService,
printFullResponse,
} from '../../config';

(async () => {
console.log('*******************');
console.log('* getSipTrunkById *');
console.log('*******************');

const sipTrunkId = getSipTrunkIdFromConfig();

const requestData: ElasticSipTrunking.GetSipTrunkRequestData = {
sipTrunkId,
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
const response = await elasticSipTrunkingService.sipTrunks.get(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`The SIP trunk with the id '${response.id}' is named '${response.name}' and has been created at '${response.createTime?.toISOString()}'`);
} else {
printFullResponse(response);
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ElasticSipTrunking, PageResult } from '@sinch/sdk-core';
import { getPrintFormat, initElasticSipTrunkingService, printFullResponse } from '../../config';

const populateSipTrunksList = (
sipTrunkPage: PageResult<ElasticSipTrunking.SipTrunk>,
sipTrunksList: ElasticSipTrunking.SipTrunk[],
sipTrunksDetailsList: string[],
) => {
sipTrunkPage.data.map((sipTrunk: ElasticSipTrunking.SipTrunk) => {
sipTrunksList.push(sipTrunk);
sipTrunksDetailsList.push(`${sipTrunk.id} - ${sipTrunk.name}`);
});
};

(async () => {
console.log('****************');
console.log('* getSipTrunks *');
console.log('****************');

const requestData: ElasticSipTrunking.ListSipTrunksRequestData = {};

const elasticSipTrunkingService = initElasticSipTrunkingService();

// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await elasticSipTrunkingService.sipTrunks.list(requestData);

const sipTrunksList: ElasticSipTrunking.SipTrunk[] = [];
const sipTrunksDetailsList: string[] = [];

// Loop on all the pages to get all the active numbers
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateSipTrunksList(response, sipTrunksList, sipTrunksDetailsList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(sipTrunksDetailsList.length > 0
? 'List of SIP trunks:\n' + sipTrunksDetailsList.join('\n')
: 'Sorry, no SIP trunks were found.');
} else {
printFullResponse(sipTrunksList);
}

// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const sipTrunk of elasticSipTrunkingService.sipTrunks.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`${sipTrunk.id} - ${sipTrunk.name}`);
} else {
console.log(sipTrunk);
}
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import { getSipTrunkIdFromConfig, initElasticSipTrunkingService, printFullResponse } from '../../config';

(async () => {
console.log('*********************************');
console.log('* getAccessControlListsForTrunk *');
console.log('*********************************');

const sipTrunkId = getSipTrunkIdFromConfig();

const requestData: ElasticSipTrunking.ListAccessControlListsForTrunkRequestData = {
trunkId: sipTrunkId,
};

const elasticSipTrunkingService = initElasticSipTrunkingService();

// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await elasticSipTrunkingService.sipTrunks.listAccessControlLists(requestData);

const aclList: string[] = [];

// Loop on all the pages to get all the active numbers
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
aclList.push(...response.data);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}

printFullResponse(aclList);

// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const acl of elasticSipTrunkingService.sipTrunks.listAccessControlLists(requestData)) {
console.log(acl);
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import {
getPrintFormat,
getSipTrunkIdFromConfig,
initElasticSipTrunkingService,
printFullResponse,
} from '../../config';

(async () => {
console.log('******************');
console.log('* updateSipTrunk *');
console.log('******************');

const sipTrunkId = getSipTrunkIdFromConfig();

const requestData: ElasticSipTrunking.UpdateSipTrunkRequestData = {
sipTrunkId,
updateSipTrunkRequestBody: {
name: 'Node.js SDK Sinch Trunk Reloaded',
hostName: 'node-js-sdk-sinch-reloaded',
},
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
const response = await elasticSipTrunkingService.sipTrunks.update(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`The SIP trunk with the id '${response.id}' has been updated at '${response.updateTime?.toISOString()}'. Its new name is '${response.name}'`);
} else {
printFullResponse(response);
}

})();
1 change: 1 addition & 0 deletions packages/elastic-sip-trunking/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * as ElasticSipTrunking from './models';
export * from './rest';
export * from '@sinch/sdk-client';
1 change: 1 addition & 0 deletions packages/elastic-sip-trunking/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './v1';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export interface AddAccessControlListToTrunk {

/** Array of AccessControlList ids */
accessControlListIds?: string[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { AddAccessControlListToTrunk } from './add-access-control-list-to-trunk';
4 changes: 4 additions & 0 deletions packages/elastic-sip-trunking/src/models/v1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './add-access-control-list-to-trunk';
export * from './sip-trunk';

export * from './requests';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './sip-trunks/sip-trunks-request-data';
Loading

0 comments on commit 28f5668

Please sign in to comment.