Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Mar 24, 2024
2 parents c880b4f + cec3e3f commit 61ec89f
Show file tree
Hide file tree
Showing 21 changed files with 341 additions and 443 deletions.
81 changes: 81 additions & 0 deletions web/api/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const Events = {
abortController: new AbortController(),
}

Events.longPoll = function (subId, deadline) {
const headers = getAuthHeaders();
let optsReq = {
method: "GET",
headers: headers,
};
const timeout = deadline - Date.now();
return fetch(`/v1/events/${subId}`, optsReq)
.then(resp => {
clearTimeout(setTimeout(() => {
Events.abortController.abort();
}, timeout));
if (!resp.ok) {
throw new Error(`Request failed with status: ${resp.status}`);
}
return resp.json();
})
.catch(e => {
console.log(e);
return null;
})
.then(data => {
if (data != null && data.hasOwnProperty("msgs") && data.msgs.length > 0) {
console.log(`Read subscription ${subId}: got ${data.msgs.length} new events`);
return data.msgs;
} else {
console.log(`Read subscription ${subId}: no new events`);
return null;
}
});
};

// uuidv4
Events.newId = function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}

Events.publishInternal = function (payload, headers) {
return fetch("/v1/pub/internal", {
method: "POST",
headers: headers,
body: JSON.stringify(payload),
})
.then(resp => {
if (!resp.ok) {
resp.text().then(errMsg => console.error(errMsg));
throw new Error(`Request failed ${resp.status}`);
}
return resp.json();
})
.then(_ => {
return true;
})
.catch(err => {
alert(err);
return false;
});
}

Events.publish = function (payload, headers) {
return fetch("/v1/pub", {
method: "POST",
headers: headers,
body: JSON.stringify(payload),
})
.then(resp => {
if (!resp.ok) {
resp.text().then(errMsg => console.error(errMsg));
throw new Error(`Request failed ${resp.status}`);
}
return resp.json();
})

}
44 changes: 0 additions & 44 deletions web/api/evts.js

This file was deleted.

45 changes: 45 additions & 0 deletions web/api/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,48 @@ Sources.fetchListPage = function (type, own, order, limit, filter, headers) {
cache: "force-cache",
});
}

Sources.fetch = function (typ, addrEnc, headers) {
headers["X-Awakari-Src-Addr"] = addrEnc;
return fetch(`/v1/src/${typ}`, {
method: "GET",
headers: headers,
cache: "default",
})
.then(resp => {
if (!resp.ok) {
throw new Error(`Request failed with status: ${resp.status}`);
}
return resp.json();
});
}

Sources.delete = function (typ, addrEnc, headers) {
headers["X-Awakari-Src-Addr"] = addrEnc;
return fetch(`/v1/src/${typ}`, {
method: "DELETE",
headers: headers,
})
.then(resp => {
if (!resp.ok) {
throw new Error(`Request failed with status: ${resp.status}`);
}
return resp;
})
}

Sources.add = function (srcType, srcAddr, updFreq, headers) {
const payload = {
"limit": {
"freq": updFreq,
},
"src": {
"addr": srcAddr,
}
}
return fetch(`/v1/src/${srcType}`, {
method: "POST",
headers: headers,
body: JSON.stringify(payload),
});
}
94 changes: 92 additions & 2 deletions web/api/subscriptions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Subscriptions = {};

Subscriptions.fetchListPage = function (cursor, order, filter, headers) {
return fetch(`/v1/sub?limit=${pageLimit}&cursor=${cursor}&order=${order}&filter=${encodeURIComponent(filter)}`, {
Subscriptions.fetchListPage = function (cursor, order, limit, filter, headers) {
return fetch(`/v1/sub?limit=${limit}&cursor=${cursor}&order=${order}&filter=${encodeURIComponent(filter)}`, {
method: "GET",
headers: headers,
cache: "no-cache",
Expand All @@ -13,3 +13,93 @@ Subscriptions.fetchListPage = function (cursor, order, filter, headers) {
return resp.json();
})
}

Subscriptions.delete = function (id, headers) {
let optsReq = {
method: "DELETE",
headers: headers,
};
return fetch(`/v1/sub/${id}`, optsReq)
.then(resp => {
if (!resp.ok) {
resp.text().then(errMsg => console.error(errMsg));
throw new Error(`Failed to delete the subscription ${id}: ${resp.status}`);
}
});
}

Subscriptions.create = function (descr, enabled, expires, cond, headers) {
const payload = {
description: descr,
enabled: enabled,
cond: cond,
}
if (expires) {
payload.expires = expires.toISOString();
}
const optsReq = {
method: "POST",
headers: headers,
body: JSON.stringify(payload)
};
return fetch(`/v1/sub`, optsReq)
.then(resp => {
if (!resp.ok) {
resp.text().then(errMsg => console.error(errMsg));
if (resp.status === 429) {
throw new Error("Subscription count limit reached. Please contact awakari@awakari.com and request to increase the limit.");
} else {
throw new Error(`Failed to create a new subscription: ${resp.status}`);
}
}
return resp.json();
})
.then(data => {
if (data) {
return data.id;
} else {
throw new Error(`Empty create subscription response`);
}
})

}

Subscriptions.fetch = function (id, headers) {
const optsReq = {
method: "GET",
headers: headers,
cache: "default",
}
return fetch(`/v1/sub/${id}`, optsReq)
.then(resp => {
if (!resp.ok) {
throw new Error(`Failed to fetch the subscription ${id}, status: ${resp.status}`);
}
return resp.json();
})
}

Subscriptions.update = function (id, descr, enabled, expires, cond, headers) {
const payload = {
id: id,
description: descr,
enabled: enabled,
cond: cond,
}
if (expires) {
payload.expires = expires.toISOString();
}
const optsReq = {
method: "PUT",
headers: headers,
body: JSON.stringify(payload)
}
return fetch(`/v1/sub/${id}`, optsReq)
.then(resp => {
if (!resp.ok) {
resp.text().then(errMsg => console.error(errMsg))
throw new Error(`Failed to update the subscription ${id}, status: ${resp.status}`);
}
return resp.json();
});
}
3 changes: 2 additions & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script src="jwt-decode.js"></script>
<script src="auth.js"></script>
<script src="api/evts.js"></script>
<script src="api/events.js"></script>
<script src="intcomm.js"></script>
<script src="jquery-3.7.1.min.js"></script>
<script src="api/subscriptions.js"></script>
<script src="query.js"></script>
<script src="status.js"></script>

Expand Down
Loading

0 comments on commit 61ec89f

Please sign in to comment.