-
Notifications
You must be signed in to change notification settings - Fork 2
/
protocolsio.js
189 lines (179 loc) · 5.74 KB
/
protocolsio.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
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var request = require('request');
var encodeUrl = require('encodeurl');
var fs = require('fs');
var ProtocolsIO = function () {
function ProtocolsIO(apikey) {
_classCallCheck(this, ProtocolsIO);
this._apikey = apikey;
}
_createClass(ProtocolsIO, [{
key: '_constructURL',
value: function _constructURL(keywords, pageid) {
var currURL = 'https://www.protocols.io/api/open/get_protocols?key=' + this._apikey;
if (keywords != null) {
currURL += '&keywords=' + keywords;
}
if (pageid != null) {
currURL += '&page_id' + pageid;
}
return encodeUrl(currURL);
}
}, {
key: 'getProtocols',
value: function getProtocols(keywords, pageid, callback) {
var muhKeywords = keywords == undefined ? null : keywords;
var muhPageID = pageid == undefined ? null : pageid;
var reqURL = this._constructURL(muhKeywords, muhPageID);
request(reqURL, function (error, response, body) {
if (error) {
callback(error, null);
return;
} else {
switch (response.statusCode) {
case 200:
callback(null, JSON.parse(body));
break;
default:
callback('UNSUPPORTED STATUS CODE: ${response.statusCode}', null);
}
}
});
}
}, {
key: 'getProtocolJSON',
value: function getProtocolJSON(protoid, callback) {
var reqURL = encodeUrl('https://www.protocols.io/api/open/get_protocol_json?key=' + this._apikey + '&protocol_id=' + protoid);
request(reqURL, function (error, response, body) {
if (error) {
callback(error, null);
return;
} else {
switch (response.statusCode) {
case 200:
callback(null, JSON.parse(body));
break;
default:
callback('UNSUPPORTED STATUS CODE: ${response.statusCode}', null);
}
}
});
}
}, {
key: 'getProtocolJSONArray',
value: function getProtocolJSONArray(protoidarr, callback) {
var retValue = {};
var promiseArray = [];
var currentObjectInstance = this;
if (!Array.isArray(protoidarr)) {
callback('ERROR: getProtocolJSONArray() only accepts arrays.', null);
} else {
var _loop = function _loop(i) {
promiseArray.push(new Promise(function (resolve, reject) {
currentObjectInstance.getProtocolJSON(protoidarr[i], function (error, result) {
if (error) {
resolve(null);
} else {
resolve({ protoidarr: protoidarr[i], result: result });
}
});
}));
};
for (var i = 0; i < protoidarr.length; ++i) {
_loop(i);
}
Promise.all(promiseArray).then(function (protocols) {
protocols.map(function (protocol) {
retValue[protocol.protoidarr] = protocol.result;
});
callback(null, retValue);
}).catch(function (err) {
callback(Error('Error: ' + err.message), null);
});
}
}
}, {
key: 'getProtocolPDF',
value: function getProtocolPDF(protoid, callback) {
var reqURL = encodeUrl('https://www.protocols.io/api/open/get_protocol_pdf?key=' + this._apikey + '&protocol_id=' + protoid);
request({ url: reqURL, encoding: null }, function (error, response, body) {
if (error) {
callback(error, null);
return;
} else {
switch (response.statusCode) {
case 200:
var didCatch = false;
try {
var x = JSON.parse(body);
} catch (e) {
didCatch = true;
callback(null, body);
}
if (!didCatch) {
callback(null, null);
}
break;
default:
callback('UNSUPPORTED STATUS CODE: ${response.statusCode}', null);
}
}
});
}
}, {
key: 'getProtocolPDFArray',
value: function getProtocolPDFArray(protoidarr, callback) {
var retValue = {};
var promiseArray = [];
var currentObjectInstance = this;
if (!Array.isArray(protoidarr)) {
callback('ERROR: getProtocolPDFArray() only accepts arrays.', null);
} else {
var _loop2 = function _loop2(i) {
promiseArray.push(new Promise(function (resolve, reject) {
currentObjectInstance.getProtocolPDF(protoidarr[i], function (error, result) {
if (error) {
resolve(null);
} else if (result == null) {
resolve({ protoidarr: protoidarr[i], result: null });
} else {
resolve({ protoidarr: protoidarr[i], result: result });
}
});
}));
};
for (var i = 0; i < protoidarr.length; ++i) {
_loop2(i);
}
Promise.all(promiseArray).then(function (protocols) {
protocols.map(function (protocol) {
retValue[protocol.protoidarr] = protocol.result;
});
callback(null, retValue);
}).catch(function (err) {
callback(Error('Error: ' + err.message), null);
});
}
}
}]);
return ProtocolsIO;
}();
var test = function test() {
var muhApiKey = process.env.PIO_API_KEY;
var protocols = new ProtocolsIO(muhApiKey);
var protopdf = protocols.getProtocolPDF('foo', function (error, result) {
if (error) {
console.log(error);
} else {
fs.writeFile('329.pdf', result, function (err) {
if (err) {
console.log(err);
}
console.log('File written successfully.');
});
}
});
};
module.exports = ProtocolsIO;