-
Notifications
You must be signed in to change notification settings - Fork 0
/
calameo-node-sdk.js
189 lines (144 loc) · 5.93 KB
/
calameo-node-sdk.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
var unirest = require('unirest');
var crypto = require('crypto');
var CalameoSdk = function (config)
{
if (!config.apikey || !config.secret) return console.error('you must pass an object {} containing at least your apikey and secret');
this.config = config;
this.buildMethods();
}
CalameoSdk.prototype =
{
url: 'http://api.calameo.com/1.0',
buildParam: function (params)
{
params.apikey = this.config.apikey;
params.output = params.output || this.config.output;
// iterate over param object and concatenate key / value in a new array
var paramArray = [];
for (var param in params)
{
paramArray.push(param + params[param]);
}
// sort the paramArray alphabeticaaly
paramArray = paramArray.sort();
// prepend the secret and join paramArray
var signature = this.config.secret + paramArray.join('');
// push the signature param before sending request
params['signature'] = crypto.createHash('md5').update(signature).digest("hex");
return params;
},
serializeParam: function (params)
{
return Object.keys(params).map(function(key) {
return key + '=' + params[key];
}).join('&');
},
request: function (options)
{
// store callback if present and remove it from params
var callback = options.callback && typeof options.callback === 'function' ? options.callback : null;
if (callback) delete options.callback;
// store file and remove it from param if any
var file = options.file;
delete options.file;
var params = this.buildParam(options);
var request;
if (!file)
{
// if we don't need to upload file, serializeParam for GET request
params = this.serializeParam(params);
request = unirest.get((options.endpoint || this.url) + '?' + params);
}
else
{
request = unirest.post((options.endpoint || this.uploadUrl))
.headers({'Accept': 'application/json'})
.field(params) // Form field
.attach('file', file) // Attachment
}
request.end(function (response) {
var resp = response.body.response;
if (resp.status !== 'ok') console.error(options.action + ' => '+ resp.error.code + ' ' + resp.error.message);
if (callback) callback(response);
})
},
checkError: function (required, options)
{
if (!required) return;
var err = [];
var arrayLength = required.length;
for (var i = 0; i < arrayLength; i++) {
if ( !options[required[i]] ) err.push(required[i]);
}
if (err.length) return console.error(options.action + ' => ' + err.join(', ') + (err.length > 1 ? ' are required' : ' is required') );
},
buildMethods: function () {
var methodList = this.methods;
var self = this;
for ( var method in methodList )
{
this[method] = (function (method, methodList, self) {
return function (options) {
options = options || {};
options.action = method;
if (methodList[method]['endpoint']) options.endpoint = methodList[method]['endpoint'];
var err = self.checkError(methodList[method]['required'], options);
if (err) return;
self.request(options);
}
})(method, methodList, self);
}
},
methods: {
// accounts
getAccountInfos: {},
fetchAccountSubscriptions: {},
fetchAccountBooks: {},
fetchAccountSubscribers: {},
// subscriptions
getSubscriptionInfos: {required: ['subscription_id'] },
fetchSubscriptionBooks: {required: ['subscription_id'] },
fetchSubscriptionSubscribers: {required: ['subscription_id']},
// publications
getBookInfos: {required: ['book_id']},
activateBook: {required: ['book_id']},
deactivateBook: {required: ['book_id']},
updateBook: {required: ['book_id']},
deleteBook: {required: ['book_id']},
fetchBookTocs: {required: ['book_id']},
fetchBookComments: {required: ['book_id']},
renewBookPrivateUrl: {required: ['book_id']},
// publishing
publish: {required: ['subscription_id', 'file'], endpoint: 'http://upload.calameo.com/1.0'},
publishFromUrl: {required: ['subscription_id', 'url'], endpoint: 'http://upload.calameo.com/1.0'},
publishFromText: {required: ['subscription_id', 'text'], endpoint: 'http://upload.calameo.com/1.0'},
revise: {required: ['book_id', 'file'], endpoint: 'http://upload.calameo.com/1.0'},
reviseFromUrl: {required: ['book_id', 'url', 'subscription_id'], endpoint: 'http://upload.calameo.com/1.0'},
reviseFromText: {required: ['book_id', 'text', 'subscription_id'], endpoint: 'http://upload.calameo.com/1.0'},
// subscribers
getSubscriberInfos: {required: ['subscription_id', 'login']},
activateSubscriber: {required: ['subscription_id', 'login']},
deactivateSubscriber: {required: ['subscription_id', 'login']},
addSubscriber: {required: ['subscription_id', 'login']},
updateSubscriber: {required: ['subscription_id', 'login']},
deleteSubscriber: {required: ['subscription_id', 'login']},
fetchSubscriberBooks: {required: ['subscription_id', 'login']},
authSubscriberSession: {required: ['subscription_id', 'login']},
checkSubscriberSession: {required: ['session_id']},
deleteSubscriberSession: {required: ['session_id']},
// drm
fetchSubscriberDRMSingles: {required: ['subscription_id', 'login']},
fetchSubscriberDRMPeriods: {required: ['subscription_id', 'login']},
fetchSubscriberDRMSeries: {required: ['subscription_id', 'login']},
addSubscriberDRMSingle: {required: ['subscription_id', 'login', 'book_id']},
addSubscriberDRMPeriod: {required: ['subscription_id', 'login', 'from', 'to']},
addSubscriberDRMSerie: {required: ['subscription_id', 'login', 'from', 'books']},
updateSubscriberDRMPeriod: {required: ['subscription_id', 'login', 'period_id', 'from', 'to']},
updateSubscriberDRMSerie: {required: ['subscription_id', 'login', 'serie_id', 'from', 'books']},
deleteSubscriberDRMSingle: {required: ['subscription_id', 'login', 'book_id']},
deleteSubscriberDRMPeriod: {required: ['subscription_id', 'login', 'period_id']},
deleteSubscriberDRMSerie: {required: ['subscription_id', 'login', 'serie_id']},
clearSubscriberDRMs: {required: ['subscription_id', 'login']}
}
}
module.exports = CalameoSdk;