-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend-layer.js
46 lines (37 loc) · 1.22 KB
/
backend-layer.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
/* globals Cookies THEUSHER_TOKEN_COOKIE_NAME BACKEND_RESOURCE_BASEURL IMG_WAITING IMG_BLANK_PIXEL */
async function callMathServiceResource(operationName) {
document.getElementById("waitingIconImage").src = IMG_WAITING;
const HEADERS = {
"Content-Type": "application/json",
Authorization: "Bearer " + Cookies.get(THEUSHER_TOKEN_COOKIE_NAME),
};
const REQUEST_BODY = {
left: parseFloat(document.getElementById("left").value),
right: parseFloat(document.getElementById("right").value)
}
const RESPONSE = await fetch(
BACKEND_RESOURCE_BASEURL + operationName,
{
method: "POST",
headers: HEADERS,
body: JSON.stringify(REQUEST_BODY)
}
);
const TEXT = await RESPONSE.text();
try {
const RESULT = JSON.parse(TEXT)["result"];
document.getElementById("result").value = RESULT;
} catch (err) {
alert('The backend resource responded with: "' + TEXT + '"');
}
document.getElementById("waitingIconImage").src = IMG_BLANK_PIXEL;
}
async function runAddBackendResource() {
callMathServiceResource("add");
}
async function runSubtractBackendResource() {
callMathServiceResource("sub");
}
async function runDivideBackendResource() {
callMathServiceResource("div");
}