-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_tools.js
186 lines (181 loc) · 6.18 KB
/
api_tools.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
/**
* @fileoverview Collection of functions that deal with API calls, mostly REST related
* @author Donald Elrod
* @version 1.0.0
*/
var http = require('http');
var https = require('https');
//var file_tools = require('./file_tools.js');
var Stream = require('stream').Transform;
/**
* Collection of functions that deal with API calls, mostly REST related
* @exports api_tools
*/
module.exports = {
/**
* Returns a Promise for the response from an HTTPS REST API call
* @param {string} url the url that the REST API call is at
* @return {Promise<string>} the response from the URI
*/
getRestHttps: async function(url) {
var dat = '';
return new Promise(function(resolve, reject) {
https.get(url, function(res) {
res.setEncoding('utf8');
res.on('data', function (dat_chunk) {
dat += dat_chunk;
});
res.on('end', function() {
resolve(dat);
});
}).on('error', function(err) {
console.log('https error');
console.error(err);
reject(err)
});
}).catch(function(err) {
return null
});
},
/**
* Returns a Promise for the response from an HTTP REST API call
* @param {string} url the url that the REST API call is at
* @return {Promise<string>} the response from the URI
*/
getRestHttp: function(url) {
var dat = '';
return new Promise( (resolve, reject) => {
http.get(url, function(res) {
res.setEncoding('utf8');
res.on('data', function (dat_chunk) {
dat += dat_chunk;
});
res.on('end', function() {
var parsed = JSON.parse(dat);
resolve(parsed);
});
}).on('error', function(err) {
//console.log('http error');
//console.error(err);
reject(err);
});
}).catch(function(err) {
return null;
});
},
/**
* Sends a PUT request to the given host and returns a promise to its response
* @param {string} host the hostname of the address
* @param {string} path the path to the resource
* @param {string} body the request body
* @param {Object} headers header objects, empty by default
* @param {number} port port number, 80 by default
*/
putRestHttp: async function(host, path, body, headers = {}, port = 80) {
var dat = '';
var options = {
host: host,
port: port,
path: path,
headers: headers,
method: 'PUT'//,
//protocol: 'http'
};
return new Promise(function(resolve, reject) {
var req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', function(chunk) {
dat += chunk;
});
res.on('error', function(err) {
console.log('Failed PUT request...');
reject('Failed PUT request');
});
res.on('end', function() {
var parsed = JSON.parse(dat);
//console.log(parsed);
resolve(parsed);
});
});
req.write(body);
req.end();
});
},
/**
* Returns a Promise for the stream to the file from an HTTP GET call
* @param {string} url the url that the REST API call is at
* @return {Promise<string>} the response from the URI
*/
getFileHttp: function(url) {
var dat = new Stream();
return new Promise(function(resolve, reject) {
http.get(url, function(res) {
res.on('data', function (dat_chunk) {
dat.push(dat_chunk);
});
res.on('end', function() {
resolve(dat);
});
res.on('error', function(err) {
console.log('http error');
console.error(err);
reject(undefined)
})
}).end();
});
},
/**
* Gets the JSON response from Plex's authentication service
* @param {JSON} plexSettings the Plex portion of the settings variable in the main server.js file
* @return {JSON} the response from the Plex authentication service
*/
getPlexAuthTokenResponse: function(plexSettings) {
var options = {
protocol: 'https:',
hostname: encodeURI('plex.tv'),
path: '/users/sign_in.json?user%5Blogin%5D=' + plexSettings.username +
'&user%5Bpassword%5D=' + plexSettings.password,
port: 443,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-Plex-Client-Identifier': 10800,
'X-Plex-Product': 'PDFlex',
'X-Plex-Version': 0.1
}
};
var dat = '';
return new Promise(function(resolve, reject) {
var req = https.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', function(chunk) {
dat+=chunk;
});
res.on('error', function(err) {
console.log('error in getting plex auth token');
reject('error in getting token');
});
res.on('end', function() {
var parsed = JSON.parse(dat);
//console.log(parsed);
resolve(parsed);
});
});
req.end();
});
}//,
// parseURL: function (url) {
// url
// }
}
/**
* API Notes
*
*
* WorldCat Documentation:
* http://classify.oclc.org/classify2/api_docs/classify.html
* http://classify.oclc.org/classify2/api_docs/index.html
* http://classify.oclc.org/classify2/
* http://classify.oclc.org/classify2/Classify?author=Chris&title=python
*
*/