forked from fritzy/node-azure-media
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
258 lines (230 loc) · 7.92 KB
/
index.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var models = require('./models');
var endpoints = require('./endpoints');
var request = require('request');
var AzureMedia = require('./media');
var resourceMap = {
accesspolicy: 'AccessPolicies',
assetfile: 'Files',
asset: 'Assets',
contentkey: 'ContentKeys',
ingestmanifest: 'IngestManifests',
ingestmanifestasset: 'IngestManifestAssets',
ingestmanifestfile: 'IngestManifestFiles',
job: 'Jobs',
jobtemplate: 'JobTemplates',
locator: 'Locators',
mediaprocessor: 'MediaProcessors',
notificationendpoint: 'NotificationEndpoints',
task: 'Tasks',
tasktemplate: 'TaskTemplates',
};
function AzureAPI(config) {
this.config = config || {};
if (!this.config.hasOwnProperty('base_url')) {
this.config.base_url = "https://media.windows.net/API/";
}
if (!this.config.hasOwnProperty('oauth_url')) {
this.config.oauth_url = "https://wamsprodglobal001acs.accesscontrol.windows.net/v2/OAuth2-13";
}
this.auth_token = config.auth_token || '';
this.rest = {};
this.media = new AzureMedia(this);
}
(function () {
this.init = function (cb) {
Object.keys(endpoints).forEach(function (endpoint) {
this.rest[endpoint] = {};
Object.keys(endpoints[endpoint]).forEach(function (call) {
this.rest[endpoint][call] = function () {
endpoints[endpoint][call].apply(this, arguments);
}.bind(this);
}.bind(this));
}.bind(this));
Object.keys(models).forEach(function (model) {
if (model !== 'common') {
models[model].addModelVar('api', this);
}
}.bind(this));
this.getAuthToken(function (err, result) {
if (err) return cb(err);
//get the first redirect
request.get({
uri: this.modelURI('asset'),
headers: this.defaultHeaders(),
followRedirect: false,
strictSSL: true
}, function (err, res) {
if (err) return cb(err);
if (!err && res.statusCode === 301) {
this.config.base_url = res.headers.location;
console.log("changing base url to", this.config.base_url);
}
cb(err, result);
}.bind(this));
}.bind(this));
};
this.defaultHeaders = function (opts) {
var headers = {
Accept: 'application/json;odata=verbose',
DataServiceVersion: '3.0',
MaxDataServiceVersion: '3.0',
'x-ms-version': '2.11',
'Content-Type': 'application/json;odata=verbose',
Authorization: 'Bearer ' + this.oauth.access_token
};
return headers;
};
this.modelURI = function (modelName, id, filters) {
var filter;
var fidx;
var url = this.config.base_url + resourceMap[modelName];
var filterurl = [];
if (id) {
url = url + "('" + id + "')";
}
if (typeof filters !== 'undefined') {
for (fidx in filters) {
filter = filters[fidx];
filterurl.push("$filter=" + filter.op + "('" + filter.source + ", " + filter.compare + "')");
}
}
return url;
};
this.getAuthToken = function (cb) {
cb = cb || function () {};
request.post({
uri: this.config.oauth_url,
form: {
grant_type: 'client_credentials',
client_id: this.config.client_id,
client_secret: this.config.client_secret,
scope: 'urn:WindowsAzureMediaServices'
},
strictSSL: true
}, function (err, res) {
if (err) {
return cb(err);
}
var result = JSON.parse(res.body);
if (result.error) {
return cb(result);
}
this.oauth = result;
this.oauth.time_started = Date.now();
cb(err, result.access_token);
}.bind(this));
};
this.getRequest = function (model, id, cb) {
cb = cb || function () {};
request.get({
uri: this.modelURI(model, id),
headers: this.defaultHeaders(),
followRedirect: false,
strictSSL: true
}, function (err, res) {
if (res.statusCode == 200) {
var data = JSON.parse(res.body).d;
var dobj = models[model].create(data);
cb(err, dobj);
} else {
cb(err || 'Expected 200 status, received: ' + res.statusCode + ' ' + res.body);
}
});
};
this.listRequest = function (model, cb, query) {
cb = cb || function () {};
request.get({
uri: this.modelURI(model),
headers: this.defaultHeaders(),
followRedirect: false,
strictSSL: true,
qs: query
}, function (err, res) {
if (err) return cb(err);
var objs = [];
if (res.statusCode == 200) {
var data = JSON.parse(res.body).d.results;
if(data.length>0) {
data.forEach(function (rawd) {
var dobj = models[model].create(rawd);
objs.push(dobj);
});
}
cb(err, objs);
} else {
cb(err || 'Expected 200 status, received: ' + res.statusCode + '\n' + res.body);
}
});
};
this.createRequest = function (model, data, cb) {
cb = cb || function () {};
var pl = models[model].create(data);
var validationErrors = pl.doValidate();
if (validationErrors.length) {
return cb(validationErrors);
}
var r = request.post({
uri: this.modelURI(model),
headers: this.defaultHeaders(),
body: JSON.stringify(data),
followRedirect: false,
strictSSL: true
}, function (err, res) {
if (err) return cb(err);
if (res.statusCode == 201) {
var data = JSON.parse(res.body).d;
var dobj = models[model].create(data);
cb(err, dobj);
} else {
cb(err || 'Create ' + model + ': Expected 201 status, received: ' + res.statusCode + '\n' + res.body);
}
});
};
this.deleteRequest = function (model, id, cb) {
cb = cb || function () {};
request({
method: 'DELETE',
uri: this.modelURI(model, id),
headers: this.defaultHeaders(),
followRedirect: false,
strictSSL: true
}, function (err, res) {
if (res.statusCode == 204) {
cb(err);
} else {
cb(err || 'Delete ' + model + ': Expected 204 status, received: ' + res.statusCode + '\n' + res.body);
}
});
};
this.updateRequest = function (model, id, data, cb) {
cb = cb || function () {};
var pl = models[model].create(data);
var validationErrors = pl.doValidate();
if (validationErrors.length) {
return cb(validationErrors);
}
request({
method: 'MERGE',
uri: this.modelURI(model, id),
headers: this.defaultHeaders(),
followRedirect: false,
strictSSL: true,
body: JSON.stringify(data)
}, function (err, res) {
if (err) return cb(err);
if (res.statusCode == 204 || res.statusCode == 200) {
if(res.statusCode == 204){
console.log(res.body);
cb(err, 'No content');
} else {
var data = JSON.parse(res.body).d;
var dobj = models[model].create(data);
cb(err, dobj);
}
} else {
cb(err || 'Update ' + model + ': Expected 204 or 200 status, received: ' + res.statusCode + '\n' + res.body);
}
});
};
}).call(AzureAPI.prototype);
module.exports = AzureAPI;