-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
145 lines (120 loc) · 4.09 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
'use strict';
const Promise = require('bluebird');
const debug = require('debug')('oddworks:provider:brightcove');
const Client = require('./lib/client');
const defaultVideoTransform = require('./lib/default-video-transform');
const defaultCollectionTransform = require('./lib/default-collection-transform');
const createChannelCache = require('./lib/create-channel-cache');
const fetchBrightcoveVideo = require('./lib/fetch-brightcove-video');
const fetchBrightcovePlaylist = require('./lib/fetch-brightcove-playlist');
const DEFAULTS = {
collectionTransform: defaultCollectionTransform,
videoTransform: defaultVideoTransform
};
// options.bus
// options.clientId
// options.clientSecret
// options.accountId
// options.collectionTransform
// options.videoTransform
exports.initialize = options => {
debug('initialize');
options = Object.assign({}, DEFAULTS, options || {});
const bus = options.bus;
const clientId = options.clientId;
const clientSecret = options.clientSecret;
const accountId = options.accountId;
const concurrentRequestLimit = options.concurrentRequestLimit;
const role = 'provider';
const cmd = 'get';
if (!bus || typeof bus !== 'object') {
throw new Error('oddworks-brightcove-provider requires an Oddcast Bus');
}
const collectionTransform = options.collectionTransform;
const videoTransform = options.videoTransform;
const client = new Client({bus, clientId, clientSecret, accountId, concurrentRequestLimit});
const getChannel = createChannelCache(bus);
bus.queryHandler(
{role, cmd, source: 'brightcove-playlist'},
exports.createPlaylistHandler(bus, getChannel, client, collectionTransform)
);
bus.queryHandler(
{role, cmd, source: 'brightcove-video'},
exports.createVideoHandler(bus, getChannel, client, videoTransform)
);
return Promise.resolve({
name: 'brightcove-provider',
client
});
};
exports.createPlaylistHandler = (bus, getChannel, client, transform) => {
debug('createPlaylistHandler');
const getCollection = fetchBrightcovePlaylist(bus, client, transform);
// Called from Oddworks core via bus.query
// Expects:
// args.spec.playlist.id
return args => {
const spec = args.spec;
const collection = spec.playlist || {};
const playlistId = collection.id;
const channelId = spec.channel;
if (!playlistId || typeof playlistId !== 'string') {
throw new Error(
'brightcove-playlist-provider spec.playlist.id String is required'
);
}
return getChannel(channelId).then(channel => {
return getCollection({spec, channel, collection, playlistId});
});
};
};
exports.createVideoHandler = (bus, getChannel, client, transform) => {
debug('createVideoHandler');
const getVideo = fetchBrightcoveVideo(bus, client, transform);
// Called from Oddworks core via bus.query
// Expects:
// args.spec.video
return args => {
const spec = args.spec;
const channelId = spec.channel;
const video = spec.video || {};
const videoId = video.id;
if (!videoId || typeof videoId !== 'string') {
throw new Error(
'brightcove-video-provider spec.video.id String is required'
);
}
return getChannel(channelId).then(channel => {
return getVideo({spec, channel, videoId});
});
};
};
// options.clientId *required
// options.clientSecret *required
// options.accountId *required
// options.bus *optional
exports.createClient = options => {
debug('createClient');
options = Object.assign({}, DEFAULTS, options || {});
const bus = options.bus;
const clientId = options.clientId;
const clientSecret = options.clientSecret;
const accountId = options.accountId;
const concurrentRequestLimit = options.concurrentRequestLimit;
if (!clientId || typeof clientId !== 'string') {
throw new Error(
'oddworks-brightcove-provider requires a Brightcove clientId'
);
}
if (!clientSecret || typeof clientSecret !== 'string') {
throw new Error(
'oddworks-brightcove-provider requires a Brightcove clientSecret'
);
}
if (!accountId || typeof accountId !== 'string') {
throw new Error(
'oddworks-brightcove-provider requires a Brightcove accountId'
);
}
return new Client({bus, clientId, clientSecret, accountId, concurrentRequestLimit});
};