This repository has been archived by the owner on May 23, 2023. It is now read-only.
forked from PlayFab/API_Specs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
203 lines (179 loc) · 6.96 KB
/
fetch.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var https = require("https");
var fs = require("fs");
function WriteJsonFile(jsonBody, options) {
if (!jsonBody) {
console.log(" *** Failed to write: " + options.description);
return false;
}
if (!options.outputFilename.endsWith(".json"))
options.outputFilename = options.outputFilename + ".json";
console.log(" -> Begin writing " + jsonBody.length + " bytes: " + options.description);
fs.writeFile(options.outputFilename, jsonBody, function (err) {
if (err)
return console.log(err);
console.log(" <- Finished writing: " + options.outputFilename);
});
return true;
}
function TabifyJson(inputJson, options) {
// console.log(" Begin tabifying: " + options.description);
if (!inputJson)
return null;
var output = null;
var tabSpaces = options.jsonTabSpaces;
if (!tabSpaces) tabSpaces = 2;
try {
var tempObj = JSON.parse(inputJson);
output = JSON.stringify(tempObj, null, tabSpaces);
// console.log(" Finish tabifying: " + options.description);
} catch (e) {
console.log(" *** Failed to stringify: " + options.description);
output = null;
}
return output;
}
function UpdateVersionNumbers(rawResponse, options) {
console.log(" Begin version numbers");
var versionRe = new RegExp("([0-9]+)\.([0-9]+)\.[0-9][0-9][0-9][0-9][0-9][0-9]");
var now = new Date();
var todaysDate = (now.getUTCFullYear()%100).toString().padStart(2, "0") + (now.getMonth()+1).toString().padStart(2, "0") + (now.getUTCDate()).toString().padStart(2, "0");
var output = JSON.parse(rawResponse);
var versions = output.sdkVersion;
for (var i in versions) {
var eachTempVer = versions[i];
if (typeof(eachTempVer) !== "string") continue;
var reMatch = eachTempVer.match(versionRe);
if (!reMatch) continue;
var majorVer = parseInt(reMatch[1]);
var minorVer = parseInt(reMatch[2]);
if (majorVer !== 0 || minorVer !== 0)
minorVer++; // Increment the minor version here
versions[i] = majorVer.toString() + "." + minorVer.toString() + "." + todaysDate;
}
var processedResponse = TabifyJson(JSON.stringify(output), options);
if (processedResponse) {
return WriteJsonFile(processedResponse, options);
}
return false;
}
function GetFileFromUrl(inputUrl, options, retry = 0) {
if (retry == 10) {
var msg = " !!!!!!!!!! Aborting GetFileFromUrl, failed " + retry + " times: " + inputUrl;
console.log(msg); return; // throw Error(msg);
}
console.log("-> Begin reading: " + inputUrl);
var rawResponse = "";
var postReq = https.get(inputUrl, function (res) {
res.setEncoding("utf8");
res.on("data", function (chunk) { rawResponse += chunk; });
res.on("end", function () {
console.log("<- Finished reading " + rawResponse.length + " bytes: " + inputUrl);
var processedResponse = rawResponse;
if (options.expectJson) {
processedResponse = TabifyJson(rawResponse, options);
}
var callbackSuccess = res.statusCode == 200;
if (callbackSuccess && options.onFileDownload) {
callbackSuccess = options.onFileDownload(processedResponse, options);
}
if (!callbackSuccess) {
console.log(" !!! Failed to GetFileFromUrl (retry: " + retry + ", " + res.statusCode + "): " + inputUrl );
GetFileFromUrl(inputUrl, options, retry + 1)
}
});
});
}
function ExtractArgs(args) {
var cmdArgs = args.slice(2, args.length);
var argsByName = {};
var activeKey = null;
for (var i = 0; i < cmdArgs.length; i++) {
if (cmdArgs[i].indexOf("-") === 0) {
activeKey = cmdArgs[i].substring(1);
argsByName[activeKey] = "";
} else if (activeKey == null) {
throw "Unexpected token: " + cmdArgs[i];
} else {
var temp = argsByName[activeKey];
if (temp.length > 0)
argsByName[activeKey] = temp + " " + cmdArgs[i];
else
argsByName[activeKey] = cmdArgs[i];
}
}
// Pull from environment variables?
// process.env.hasOwnProperty("varname")
return argsByName;
}
function GetPlayFabUrl() {
var argsDict = ExtractArgs(process.argv);
var verticalUrl = "www";
var playFabUrl = "https://"+verticalUrl+".playfabapi.com/";
if (argsDict["playFabUrl"])
playFabUrl = argsDict["playFabUrl"];
if (!playFabUrl.endsWith("/"))
playFabUrl = playFabUrl + "/";
return playFabUrl;
}
function UpdateApiFilesFromToc(playFabUrl, tocObj) {
try {
for (var i = 0; i < tocObj.documents.length; ++i) {
var apiSection = tocObj.documents[i];
if (apiSection.format === "LegacyPlayFabApiSpec" || apiSection.format === "Swagger") {
var eachApiOpt = {
description: apiSection.relPath,
expectJson: true,
jsonTabSpaces: 2,
outputFilename: apiSection.relPath,
onFileDownload: WriteJsonFile
}
GetFileFromUrl(playFabUrl + apiSection.pfurl, eachApiOpt);
}
}
} catch(err) {
console.log("=== fetch.js failed to parse TOC.json as JSON :( ===");
console.log(err);
}
}
function UpdateSdkManualNotes() {
var versionOpt = {
description: "SdkManualNotes",
expectJson: true,
jsonTabSpaces: 4,
outputFilename: "SdkManualNotes.json",
onFileDownload: UpdateVersionNumbers
}
GetFileFromUrl("https://raw.githubusercontent.com/PlayFab/API_Specs/master/SdkManualNotes.json", versionOpt);
}
// Find Api Groups from the TOC or legacy Api list, which don't exist in the other
function CheckLegacyApiGroupList(playFabUrl, tocObj) {
var legacyApiListUrl = playFabUrl + "apispec/";
var options = {
description: "legacyApiList",
expectJson: true,
jsonTabSpaces: 0
// TODO: Add a function to compare tocObj with the json result
};
GetFileFromUrl(legacyApiListUrl, options);
}
// Find Api Groups from the TOC or swagger Api list, which don't exist in the other
function CheckSwaggerApiGroupList(playFabUrl, tocObj) {
var swaggerApiListUrl = playFabUrl + "swagger/";
var options = {
description: "legacyApiList",
expectJson: true,
jsonTabSpaces: 0
// TODO: Add a function to compare tocObj with the json result
};
GetFileFromUrl(swaggerApiListUrl, options);
}
function DoWork() {
var playFabUrl = GetPlayFabUrl();
var tocObj = require("./TOC.json");
UpdateApiFilesFromToc(playFabUrl, tocObj);
UpdateSdkManualNotes();
CheckLegacyApiGroupList(playFabUrl, tocObj);
CheckSwaggerApiGroupList(playFabUrl, tocObj);
// TODO: Some kind of final global report, with an errors list from all threads
}
DoWork();