This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.coffee
146 lines (141 loc) · 5.21 KB
/
api.coffee
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
js2xmlparser = require 'js2xmlparser'
request = require 'request'
{getTimestamp, getAuthorization, getSig, merge} = require './util'
init = (config) ->
accountSid = config.accountSid
authToken = config.authToken
appId = config.appId
baseUrl = config.baseUrl
softVersion = config.softVersion
if config.log then log = config.log
else
log = (e, r, b, action) ->
if e then console.log '\n' + e
else
if action then console.log '\nResult of ' + action + ':'
console.log '\n'
console.dir b
callApi = (args) ->
unless args.format
if (/\/ivr\//i).test args.api
args.format = 'XML'
else
args.format = 'JSON'
else
args.format = args.format.toUpperCase()
unless args.method
args.method = 'POST'
else
args.method = args.method.toUpperCase()
if (/{accountSid}/i).test args.api
sid = accountSid
token = authToken
else if (/{subAccountSid}/i).test args.api
sid = args.sid
token = args.token
if not sid or not token
throw new Error 'subaccount sid and token are required.'
uri = baseUrl +
args.api.replace(/{SoftVersion}/i, softVersion)
.replace(/{(sub)?accountSid}/i, sid)
timestamp = getTimestamp()
headers =
'Authorization': getAuthorization(sid, timestamp)
'Accept': 'application/json'
if args.format is 'JSON'
headers['Content-Type'] ='application/json;charset=utf-8;'
else if args.format is 'XML'
headers['Content-Type'] ='application/xml;charset=utf-8;'
options =
method: args.method
uri: uri
followAllRedirects: true
rejectUnauthorized: false
timeout: 5000
headers: headers
unless args.params then args.params = {}
if args.method is 'GET'
args.params.appId = appId
options.qs = args.params
else
if args.format is 'JSON'
args.params.appId = appId
options.json = args.params
else if args.format is 'XML'
unless args.params.data then pargs.arams.data = {}
args.params.data['Appid'] = appId
options.body = js2xmlparser args.params.root or 'Request', args.params.data
unless options.qs then options.qs = {}
options.qs.sig = getSig(sid, token, timestamp)
request options, (e, r, b) ->
if typeof b is 'string'
try
b = JSON.parse b
catch e
return callback? new Error 'Parse response error: ' + b
args.callback? e, r, b
apiFactory = (name, api, method) ->
(args, callback) ->
if typeof args is 'function'
callback = args
args = {}
callApi
method: method or 'POST'
api: api
params: args
callback: (e, r, b) ->
log e, r, b, name
callback? e, b, r
confApiFactory = (action) ->
params = {}
if action.toLowerCase() is 'createconf'
api = '/{SoftVersion}/Accounts/{accountSid}/ivr/createconf'
else
api = '/{SoftVersion}/Accounts/{accountSid}/ivr/conf?confid={id}'
(args, callback) ->
if typeof args is 'function'
callback = args
args = {}
params.root = 'Request'
params.data = {}
params.data[action] = {}
params.data[action]['@'] = args
callApi
api: api.replace '{id}', args.confid
params: params
callback: (e, r, b) ->
log e, r, b, action
callback? e, b, r
# Account
accountInfo: apiFactory 'AccountInfo', '/{SoftVersion}/Accounts/{accountSid}/AccountInfo', 'get'
createSubAccount: apiFactory 'CreateSubAccount', '/{SoftVersion}/Accounts/{accountSid}/SubAccounts'
getSubAccounts: apiFactory 'GetSubAccounts', '/{SoftVersion}/Accounts/{accountSid}/GetSubAccounts'
querySubAccountByName: apiFactory 'QuerySubAccountByName', '/{SoftVersion}/Accounts/{accountSid}/QuerySubAccountByName'
closeSubAccount: apiFactory 'CloseSubAccount', '/{SoftVersion}/Accounts/{accountSid}/CloseSubAccount'
# Bill
billRecords: apiFactory 'BillRecords', '/{SoftVersion}/Accounts/{accountSid}/BillRecords'
# SMS
message: apiFactory 'Message', '/{SoftVersion}/Accounts/{accountSid}/SMS/Messages'
templateSMS: apiFactory 'TemplateSMS', '/{SoftVersion}/Accounts/{accountSid}/SMS/TemplateSMS'
# Call
callback: apiFactory 'Callback', '/{SoftVersion}/SubAccounts/{subAccountSid}/Calls/Callback'
landingCalls: apiFactory 'LandingCalls', '/{SoftVersion}/Accounts/{accountSid}/Calls/LandingCalls'
voiceVerify: apiFactory 'VoiceVerify', '/{SoftVersion}/Accounts/{accountSid}/Calls/VoiceVerify'
# IVR Conf
createConf: confApiFactory 'CreateConf'
dismissConf: confApiFactory 'DismissConf'
joinConf: confApiFactory 'JoinConf'
inviteJoinConf: confApiFactory 'InviteJoinConf'
quitConf: confApiFactory 'QuitConf'
confMute: confApiFactory 'ConfMute'
confUnMute: confApiFactory 'ConfUnMute'
confPlay: confApiFactory 'ConfPlay'
confStopPlay: confApiFactory 'ConfStopPlay'
confRecord: confApiFactory 'ConfRecord'
confStopRecord: confApiFactory 'ConfStopRecord'
confVolumeAdjust: confApiFactory 'ConfVolumeAdjust'
confMemberPause: confApiFactory 'ConfMemberPause'
confMemberResume: confApiFactory 'ConfMemberResume'
confAlarmClock: confApiFactory 'ConfAlarmClock'
queryConfState: confApiFactory 'QueryConfState'
exports.init = init