// Google Workspace page: thin client over /google/workspace/* (all limits server side). (function () { "use strict"; var $ = function (id) { return document.getElementById(id); }; var out = $("ws-out"), err = $("ws-error"), calls = []; function show(res) { return res.text().then(function (t) { var j; try { j = JSON.parse(t); } catch (e) { j = { message: t }; } if (!res.ok) { err.textContent = j.message || ("HTTP " + res.status); out.textContent = ""; } else { out.textContent = JSON.stringify(j, null, 2); } return j; }); } function send(path, extra) { err.textContent = ""; var params, body; try { params = JSON.parse($("ws-params").value || "{}"); } catch (e) { err.textContent = "Parameters are not valid JSON"; return; } var payload = { api: $("ws-api").value, id: $("ws-call").value, params: params, confirm: $("ws-confirm").checked }; var raw = $("ws-body").value.trim(); if (raw) { try { payload.body = JSON.parse(raw); } catch (e2) { err.textContent = "Body is not valid JSON"; return; } } for (var k in extra) payload[k] = extra[k]; out.textContent = "..."; fetch(path, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) }) .then(show).catch(function (e) { err.textContent = String(e); out.textContent = ""; }); } function loadCalls() { var q = "?api=" + encodeURIComponent($("ws-api").value) + "&kind=" + encodeURIComponent($("ws-kind").value); fetch("/google/workspace/calls" + q).then(function (r) { return r.json(); }).then(function (j) { calls = j.calls || []; var sel = $("ws-call"); sel.innerHTML = ""; calls.forEach(function (c) { var o = document.createElement("option"); o.value = c.id; o.textContent = c.id + " [" + c.kind + "]"; sel.appendChild(o); }); sig(); }); } function sig() { var c = calls.filter(function (x) { return x.id === $("ws-call").value; })[0]; $("ws-sig").textContent = c ? (c.http + " " + c.path + " params: " + (c.params || "none") + (c.body ? " body: " + c.body : "")) : ""; } fetch("/google/workspace/status").then(function (r) { return r.json(); }).then(function (s) { $("ws-config").textContent = s.configured ? "Credential mode: " + s.mode + (s.subject_set ? " (impersonating a user)." : ".") : "No Google credential: calls answer 503 until one is configured."; }); fetch("/google/workspace/apis").then(function (r) { return r.json(); }).then(function (j) { var sel = $("ws-api"); j.apis.forEach(function (a) { var o = document.createElement("option"); o.value = a.name; o.textContent = a.name + " (" + a.calls + ")"; sel.appendChild(o); }); loadCalls(); }); $("ws-api").onchange = loadCalls; $("ws-kind").onchange = loadCalls; $("ws-call").onchange = sig; $("ws-run").onclick = function () { send("/google/workspace/call", {}); }; $("ws-list").onclick = function () { send("/google/workspace/list", { maxPages: 2 }); }; })();