(o)
| 180 | } |
| 181 | |
| 182 | function QueryRequest(o) { |
| 183 | const option = ChangeHeaders(o); |
| 184 | const needCache = $.EnableCache && (option.url.endsWith('/apps') || /\/apps\/\d+\/builds\/\d+$/.test(req.url)); |
| 185 | return $.http[req.method.toLowerCase()](option) |
| 186 | .then(r => { |
| 187 | $.log(`URL "${option.url}" response: status=${r.status}, body=${Boolean(r.body)}`); |
| 188 | if (r.status == 401 && o) { |
| 189 | if (list[o].InvalidKey >= 2) { //prevent misjudgment |
| 190 | delete list[o]; |
| 191 | } else { |
| 192 | list[o].InvalidKey = (list[o].InvalidKey || 0) + 1; |
| 193 | } |
| 194 | $.write(list, 'AccountList'); |
| 195 | $.notify('TestFlight Account', '', `Account ID "${o}" key expired ⚠️`); |
| 196 | throw 'key expired ⚠️'; |
| 197 | } |
| 198 | if (needCache && r.status == 200 && r.body && r.body.startsWith('{')) { |
| 199 | const cacheKey = (cacheInfo[option.url] && cacheInfo[option.url].key) || `TESTFLIGHT-ACCOUNT-${letterEncode(option.url.split(/\/\/.+?\/(.+)/)[1])}`; |
| 200 | $.log(`Write to cache, URL "${option.url}", READ KEY "${cacheKey}"`); |
| 201 | cacheInfo[option.url] = { key: cacheKey, lastUsed: Date.now() }; |
| 202 | Object.keys(cacheInfo).forEach((i) => (Date.now() - (cacheInfo[i].lastUsed || 0) > 864e5 * 3) && $.delete(`#${cacheInfo[i].key}`) && delete cacheInfo[i]); //clear unused cache for 3 days |
| 203 | $.write(cacheInfo, 'CachedInfo'); |
| 204 | $.write(JSON.stringify(r), `#${cacheKey}`); |
| 205 | } |
| 206 | return { ...r, account: o } |
| 207 | }) |
| 208 | .catch(e => { |
| 209 | if (needCache && cacheInfo[option.url] && !(e).includes('key expired')) { |
| 210 | $.log(`URL "${option.url}" Try using cached data`); |
| 211 | const cachedData = $.read(`#${cacheInfo[option.url].key}`); |
| 212 | cacheInfo[option.url].lastUsed = Date.now(); |
| 213 | !cachedData ? delete cacheInfo[option.url] : null; |
| 214 | $.write(cacheInfo, 'CachedInfo'); |
| 215 | return { ...JSON.parse(cachedData || '{}'), account: o } |
| 216 | } |
| 217 | $.error(`URL "${option.url}" response failed: ${e}`); |
| 218 | return { account: o } |
| 219 | }) |
| 220 | } |
| 221 | |
| 222 | function ExternalAccount(key) { |
| 223 | try { |
no test coverage detected