forked from oddnetworks/oddworks-ooyala-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
188 lines (152 loc) · 5.28 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
'use strict';
const Promise = require('bluebird');
const Client = require('./lib/client');
const defaultAssetTransform = require('./lib/default-asset-transform');
const defaultCollectionTransform = require('./lib/default-collection-transform');
const createChannelCache = require('./lib/create-channel-cache');
const fetchLabelCollection = require('./lib/fetch-label-collection');
const fetchBacklotAsset = require('./lib/fetch-backlot-asset');
const fetchPopularCollection = require('./lib/fetch-popular-collection');
const fetchSimilarCollection = require('./lib/fetch-similar-collection');
const fetchTrendingCollection = require('./lib/fetch-trending-collection');
const DEFAULTS = {
baseUrl: 'http://api.ooyala.com',
collectionTransform: defaultCollectionTransform,
assetTransform: defaultAssetTransform
};
// options.bus
// options.baseUrl
// options.secretKey
// options.apiKey
// options.collectionTransform
// options.assetTransform
exports.initialize = function (options) {
options = Object.assign({}, DEFAULTS, options || {});
const bus = options.bus;
const baseUrl = options.baseUrl;
const apiKey = options.apiKey;
const secretKey = options.secretKey;
const role = 'provider';
const cmd = 'get';
if (!bus || typeof bus !== 'object') {
throw new Error('oddworks-ooyala-provider requires an Oddcast Bus');
}
const collectionTransform = options.collectionTransform;
const assetTransform = options.assetTransform;
const client = new Client({bus, baseUrl, secretKey, apiKey});
const getChannel = createChannelCache(bus);
bus.queryHandler(
{role, cmd, source: 'ooyala-label-provider'},
exports.createLabelHandler(bus, getChannel, client, collectionTransform)
);
bus.queryHandler(
{role, cmd, source: 'ooyala-asset-provider'},
exports.createAssetHandler(bus, getChannel, client, assetTransform)
);
return Promise.resolve({
name: 'ooyala-provider',
client
});
};
exports.createLabelHandler = function (bus, getChannel, client, transform) {
const getCollection = fetchLabelCollection(bus, client, transform);
// Called from Oddworks core via bus.query
// Expects:
// args.spec.label.id
const ooyalaLabelProvider = args => {
const spec = args.spec;
const labelId = (spec.label || {}).id;
const channelId = spec.channel;
if (!labelId || typeof labelId !== 'string') {
throw new Error(
'ooyala-label-provider spec.label.id String is required'
);
}
return getChannel(channelId).then(channel => {
return getCollection({spec, channel, labelId});
});
};
return ooyalaLabelProvider;
};
exports.createAssetHandler = function (bus, getChannel, client, transform) {
const getAsset = fetchBacklotAsset(bus, client, transform);
// Called from Oddworks core via bus.query
// Expects:
// args.spec.asset
const ooyalaAssetProvider = args => {
const spec = args.spec;
const channelId = spec.channel;
const asset = spec.asset || {};
const assetId = asset.external_id || asset.embed_code;
if (!assetId || typeof assetId !== 'string') {
throw new Error(
'ooyala-asset-provider spec.asset.id String is not available'
);
}
return getChannel(channelId).then(channel => {
return getAsset({spec, channel, assetId});
});
};
return ooyalaAssetProvider;
};
exports.createPopularHandler = function (bus, getChannel, client, transform) {
const getPopular = fetchPopularCollection(bus, client, transform);
const ooyalaPopularProvider = args => {
const spec = args.spec;
const channelId = spec.channel;
return getChannel(channelId).then(channel => {
return getPopular({spec, channel});
});
};
return ooyalaPopularProvider;
};
exports.createSimilarHandler = function (bus, getChannel, client, transform) {
const getSimilar = fetchSimilarCollection(bus, client, transform);
// Called from Oddworks core via bus.query
// Expects:
// args.spec.asset
const ooyalaSimilarProvider = args => {
const spec = args.spec;
const asset = spec.asset || {};
const assetId = asset.external_id || asset.embed_code;
const channelId = spec.channel;
if (!assetId || typeof assetId !== 'string') {
throw new Error(
'ooyala-discovery-similar-provider spec.assetId String is not available'
);
}
return getChannel(channelId).then(channel => {
return getSimilar({spec, channel});
});
};
return ooyalaSimilarProvider;
};
exports.createTrendingHandler = function (bus, getChannel, client, transform) {
const getTrending = fetchTrendingCollection(bus, client, transform);
const ooyalaTrendingProvider = args => {
const spec = args.spec;
const channelId = spec.channel;
return getChannel(channelId).then(channel => {
return getTrending({spec, channel});
});
};
return ooyalaTrendingProvider;
};
// options.secretKey *required
// options.apiKey *required
// options.bus *optional
// options.baseUrl *optional
exports.createClient = function (options) {
options = Object.assign({}, DEFAULTS, options || {});
const bus = options.bus;
const baseUrl = options.baseUrl;
const secretKey = options.secretKey;
const apiKey = options.apiKey;
if (!apiKey || typeof apiKey !== 'string') {
throw new Error('oddworks-ooyala-provider requires an Ooyala apiKey key');
}
if (!secretKey || typeof secretKey !== 'string') {
throw new Error('oddworks-ooyala-provider requires an Ooyala secretKey key');
}
return new Client({bus, baseUrl, secretKey, apiKey});
};