Friendiiverse/App/Net.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-04-25 00:55:20 +02:00
function NetApiCall(Data, Proc) {
2023-04-23 19:46:01 +02:00
// Data = {Target: "Friendica", Method: "...", Data: {}, Call: (), CallFine: (), CallFail: ()}
var Req = new XMLHttpRequest();
Req.Proc = Proc;
Req.onloadend = function(){
try {
this.responseJson = JSON.parse(this.responseText);
this.responseLog = this.responseJson;
} catch(Ex) {
this.responseLog = this.responseText;
};
if (Data.Call) {
Data.Call(this);
};
if (IsHttpCodeGood(this.status)) {
LogDebug([this.status, this.responseLog], 'l');
if (Data.CallFine) {
Data.CallFine(this);
};
} else {
LogDebug([this.status, this.responseLog], 'e');
if (Data.CallFail) {
Data.CallFail(this);
};
};
};
if (Data.Target == 'Mastodon') {
Req.open('GET', `${MastodonUrl}/api/v1/${Data.Method}`, true);
} else
if (Data.Target == 'Friendica') {
Req.open('GET', `${FriendicaUrl}/api/${Data.Method}.json`, true);
Req.setRequestHeader('Authorization', `Basic ${btoa(FriendicaCredentials)}`);
};
Req.send();
};
function IsHttpCodeGood(Code) {
Code = String(Code)[0];
if (['1', '2', '3'].includes(Code)) {
return true;
} else
if (['4', '5'].includes(Code)) {
return false;
};
};