This commit is contained in:
Родитель
7df3c89871
Коммит
fa9ab74b83
|
@ -0,0 +1,151 @@
|
|||
const axios = require("axios");
|
||||
const qs = require("qs");
|
||||
const dotenv = require("dotenv");
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const ApiBaseUrl = "https://sync.twilio.com/v1";
|
||||
|
||||
const TwilioAuth = {
|
||||
username: process.env.TWILIO_SID,
|
||||
password: process.env.TWILIO_AUTH_TOKEN,
|
||||
};
|
||||
|
||||
const postContentTypeHeader = {
|
||||
"content-type": "application/x-www-form-urlencoded;charset=utf-8",
|
||||
};
|
||||
|
||||
// used to proxy through tool like Fiddler
|
||||
const USE_PROXY = process.env.USE_PROXY;
|
||||
const PROXY_URL = process.env.PROXY_URL;
|
||||
const PROXY_PORT = process.env.PROXY_PORT;
|
||||
|
||||
let useProxy = false;
|
||||
if (USE_PROXY.toLowerCase() == "true") {
|
||||
useProxy = true;
|
||||
}
|
||||
|
||||
const ProxyDetails = {
|
||||
host: PROXY_URL,
|
||||
port: PROXY_PORT,
|
||||
};
|
||||
|
||||
// list name
|
||||
const UniqueName = "MyList2";
|
||||
|
||||
(async () => {
|
||||
// get sync services
|
||||
try {
|
||||
console.log("GET: Sync Services");
|
||||
|
||||
let syncServicesUrl = `${ApiBaseUrl}/Services`;
|
||||
const getServicesResponse = await axios.get(syncServicesUrl, {
|
||||
auth: TwilioAuth,
|
||||
proxy: useProxy ? ProxyDetails : "",
|
||||
});
|
||||
|
||||
let friendly_name = getServicesResponse.data.services[0].friendly_name;
|
||||
let url = getServicesResponse.data.services[0].url;
|
||||
let sid = getServicesResponse.data.services[0].sid;
|
||||
|
||||
console.log(` Sync Service SID: ${sid}`);
|
||||
|
||||
let mapsUrl = getServicesResponse.data.services[0].links.maps;
|
||||
let documentsUrl = getServicesResponse.data.services[0].links.documents;
|
||||
let streamsUrl = getServicesResponse.data.services[0].links.streams;
|
||||
let listsUrl = getServicesResponse.data.services[0].links.lists;
|
||||
|
||||
// create list
|
||||
console.log("POST: Create Sync List");
|
||||
|
||||
const postListsResponse = await axios({
|
||||
method: "post",
|
||||
url: listsUrl,
|
||||
data: qs.stringify({
|
||||
UniqueName: UniqueName,
|
||||
}),
|
||||
auth: TwilioAuth,
|
||||
headers: postContentTypeHeader,
|
||||
proxy: useProxy ? ProxyDetails : "",
|
||||
});
|
||||
|
||||
let syncListSID = postListsResponse.data.sid;
|
||||
let syncListUniqueName = postListsResponse.data.unique_name;
|
||||
let syncListItemsUrl = postListsResponse.data.links.items;
|
||||
|
||||
console.log(` Sync List SID: ${syncListSID}`);
|
||||
console.log(` Sync List UniqueName: ${syncListUniqueName}`);
|
||||
|
||||
// create list item 1
|
||||
let jsonItemPost1 = '{ "Hello": "World" }';
|
||||
|
||||
console.log("POST: Create Sync List Item 1");
|
||||
|
||||
const postListItemResponse1 = await axios({
|
||||
method: "post",
|
||||
url: syncListItemsUrl,
|
||||
data: qs.stringify({
|
||||
Data: jsonItemPost1,
|
||||
}),
|
||||
auth: TwilioAuth,
|
||||
headers: postContentTypeHeader,
|
||||
proxy: useProxy ? ProxyDetails : "",
|
||||
});
|
||||
|
||||
let syncListItem1SID = postListItemResponse1.data.list_sid;
|
||||
|
||||
console.log(` Sync List Item SID: ${syncListItem1SID}`);
|
||||
|
||||
// create list item 2
|
||||
let jsonItemPost2 = '{ "GoodNight": "Moon" }';
|
||||
|
||||
console.log("POST: Create Sync List Item 2");
|
||||
|
||||
const postListItemResponse2 = await axios({
|
||||
method: "post",
|
||||
url: syncListItemsUrl,
|
||||
data: qs.stringify({
|
||||
Data: jsonItemPost2,
|
||||
}),
|
||||
auth: TwilioAuth,
|
||||
headers: postContentTypeHeader,
|
||||
proxy: useProxy ? ProxyDetails : "",
|
||||
});
|
||||
|
||||
let syncListItem2SID = postListItemResponse2.data.list_sid;
|
||||
|
||||
console.log(` Sync List Item SID: ${syncListItem2SID}`);
|
||||
|
||||
// get sync list items
|
||||
console.log("GET: Get List Items");
|
||||
|
||||
const getSyncListItemsResponse = await axios.get(`${syncListItemsUrl}`, {
|
||||
auth: TwilioAuth,
|
||||
proxy: useProxy ? ProxyDetails : "",
|
||||
});
|
||||
|
||||
console.log(` # of items: ${getSyncListItemsResponse.data.items.length}`);
|
||||
|
||||
let itemJson1 = getSyncListItemsResponse.data.items[0].data;
|
||||
let hello = JSON.parse(JSON.stringify(itemJson1)).Hello;
|
||||
|
||||
console.log(` List Item 1 Value: ${hello}`);
|
||||
|
||||
let itemJson2 = getSyncListItemsResponse.data.items[1].data;
|
||||
let goodNight = JSON.parse(JSON.stringify(itemJson2)).GoodNight;
|
||||
|
||||
console.log(` List Item 2 Value: ${goodNight}`);
|
||||
|
||||
// delete list
|
||||
|
||||
console.log("DELETE: Delete List");
|
||||
|
||||
const syncListItemUrl = `${listsUrl}/${syncListSID}`;
|
||||
const delListItemResponse = await axios.delete(syncListItemUrl, {
|
||||
auth: TwilioAuth,
|
||||
proxy: useProxy ? ProxyDetails : "",
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
})();
|
|
@ -0,0 +1,242 @@
|
|||
{
|
||||
"name": "twilio-sync",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^0.21.1",
|
||||
"dotenv": "^8.2.0",
|
||||
"form-data": "^3.0.0",
|
||||
"qs": "^6.9.6",
|
||||
"tunnel": "^0.0.6",
|
||||
"url": "^0.11.0"
|
||||
}
|
||||
},
|
||||
"node_modules/asynckit": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
||||
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
|
||||
},
|
||||
"node_modules/axios": {
|
||||
"version": "0.21.1",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
|
||||
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
|
||||
"dependencies": {
|
||||
"follow-redirects": "^1.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/combined-stream": {
|
||||
"version": "1.0.8",
|
||||
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
||||
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
|
||||
"dependencies": {
|
||||
"delayed-stream": "~1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/delayed-stream": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
|
||||
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=",
|
||||
"engines": {
|
||||
"node": ">=0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/dotenv": {
|
||||
"version": "8.2.0",
|
||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
|
||||
"integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/follow-redirects": {
|
||||
"version": "1.13.1",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz",
|
||||
"integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "individual",
|
||||
"url": "https://github.com/sponsors/RubenVerborgh"
|
||||
}
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=4.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"debug": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/form-data": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz",
|
||||
"integrity": "sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==",
|
||||
"dependencies": {
|
||||
"asynckit": "^0.4.0",
|
||||
"combined-stream": "^1.0.8",
|
||||
"mime-types": "^2.1.12"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
}
|
||||
},
|
||||
"node_modules/mime-db": {
|
||||
"version": "1.45.0",
|
||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.45.0.tgz",
|
||||
"integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==",
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/mime-types": {
|
||||
"version": "2.1.28",
|
||||
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.28.tgz",
|
||||
"integrity": "sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==",
|
||||
"dependencies": {
|
||||
"mime-db": "1.45.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/punycode": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz",
|
||||
"integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0="
|
||||
},
|
||||
"node_modules/qs": {
|
||||
"version": "6.9.6",
|
||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.6.tgz",
|
||||
"integrity": "sha512-TIRk4aqYLNoJUbd+g2lEdz5kLWIuTMRagAXxl78Q0RiVjAOugHmeKNGdd3cwo/ktpf9aL9epCfFqWDEKysUlLQ==",
|
||||
"engines": {
|
||||
"node": ">=0.6"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/querystring": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz",
|
||||
"integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=",
|
||||
"engines": {
|
||||
"node": ">=0.4.x"
|
||||
}
|
||||
},
|
||||
"node_modules/tunnel": {
|
||||
"version": "0.0.6",
|
||||
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz",
|
||||
"integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==",
|
||||
"engines": {
|
||||
"node": ">=0.6.11 <=0.7.0 || >=0.7.3"
|
||||
}
|
||||
},
|
||||
"node_modules/url": {
|
||||
"version": "0.11.0",
|
||||
"resolved": "https://registry.npmjs.org/url/-/url-0.11.0.tgz",
|
||||
"integrity": "sha1-ODjpfPxgUh63PFJajlW/3Z4uKPE=",
|
||||
"dependencies": {
|
||||
"punycode": "1.3.2",
|
||||
"querystring": "0.2.0"
|
||||
}
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"asynckit": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
||||
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
|
||||
},
|
||||
"axios": {
|
||||
"version": "0.21.1",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
|
||||
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
|
||||
"requires": {
|
||||
"follow-redirects": "^1.10.0"
|
||||
}
|
||||
},
|
||||
"combined-stream": {
|
||||
"version": "1.0.8",
|
||||
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
||||
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
|
||||
"requires": {
|
||||
"delayed-stream": "~1.0.0"
|
||||
}
|
||||
},
|
||||
"delayed-stream": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
|
||||
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk="
|
||||
},
|
||||
"dotenv": {
|
||||
"version": "8.2.0",
|
||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
|
||||
"integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw=="
|
||||
},
|
||||
"follow-redirects": {
|
||||
"version": "1.13.1",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz",
|
||||
"integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg=="
|
||||
},
|
||||
"form-data": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz",
|
||||
"integrity": "sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==",
|
||||
"requires": {
|
||||
"asynckit": "^0.4.0",
|
||||
"combined-stream": "^1.0.8",
|
||||
"mime-types": "^2.1.12"
|
||||
}
|
||||
},
|
||||
"mime-db": {
|
||||
"version": "1.45.0",
|
||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.45.0.tgz",
|
||||
"integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w=="
|
||||
},
|
||||
"mime-types": {
|
||||
"version": "2.1.28",
|
||||
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.28.tgz",
|
||||
"integrity": "sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==",
|
||||
"requires": {
|
||||
"mime-db": "1.45.0"
|
||||
}
|
||||
},
|
||||
"punycode": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz",
|
||||
"integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0="
|
||||
},
|
||||
"qs": {
|
||||
"version": "6.9.6",
|
||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.6.tgz",
|
||||
"integrity": "sha512-TIRk4aqYLNoJUbd+g2lEdz5kLWIuTMRagAXxl78Q0RiVjAOugHmeKNGdd3cwo/ktpf9aL9epCfFqWDEKysUlLQ=="
|
||||
},
|
||||
"querystring": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz",
|
||||
"integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA="
|
||||
},
|
||||
"tunnel": {
|
||||
"version": "0.0.6",
|
||||
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz",
|
||||
"integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg=="
|
||||
},
|
||||
"url": {
|
||||
"version": "0.11.0",
|
||||
"resolved": "https://registry.npmjs.org/url/-/url-0.11.0.tgz",
|
||||
"integrity": "sha1-ODjpfPxgUh63PFJajlW/3Z4uKPE=",
|
||||
"requires": {
|
||||
"punycode": "1.3.2",
|
||||
"querystring": "0.2.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче