This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apiManagement.js
65 lines (55 loc) · 1.77 KB
/
apiManagement.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
/**
* module to process 'version 2' or API Management version specs from JSON source
*/
var url = require('url');
var fs = require('fs');
var request = require('request');
function retrieve(source,suffix,options) {
request(source, function (error, response, body) {
if (error) {
console.log("Error requesting page: " + error);
return false;
}
else {
try {
var obj = JSON.parse(body);
if ((Object.keys(obj).length==2) && obj.status && obj.message) {
// skip
}
else {
console.log('Got a hit: %s',source);
var outfile = './json/'+options.apiName+(suffix ? '-'+suffix : '')+'.json';
console.log(outfile);
fs.writeFile(outfile,JSON.stringify(obj,null,2),'utf8');
}
}
catch (ex) {
}
}
});
}
module.exports = {
convertJSON : function($,options) {
var x = {};
x.url = options.urlOrFile;
x.examples = x.url+'/examples';
x.apiDesc = $('.apiDescription').first().text();
x.extDocs = $('.full-docs').first().text();
x.extUrl = $('.full-docs').first().attr('href');
if (x.apiDesc || x.extDocs || x.extUrl) {
console.log(x.apiDesc);
console.log(x.extDocs+' '+x.extUrl);
}
fs.writeFile('./json/'+options.apiName+'-ext.json',JSON.stringify(x,null,2),'utf8');
var u = url.parse(options.urlOrFile);
var source = options.urlOrFile+'/data/'+options.apiName+'.json';
retrieve(source,'',options);
retrieve(options.urlOrFile+'/'+options.apiName+'.json',1,options);
retrieve(options.urlOrFile+'/data/io-docs.json',2,options);
retrieve(options.urlOrFile+'/data/iodocs.json',3,options);
retrieve(options.urlOrFile+'/io-docs.json',4,options);
retrieve(options.urlOrFile+'/iodocs.json',5,options);
var root = u.protocol+'//'+u.host+'/data/'+options.apiName+'.json';
if (root != source) retrieve(root,6,options);
}
};