Files

69 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

// Google Discovery page: thin client over /google/discovery/* (all limits server side).
(function () {
"use strict";
var $ = function (id) { return document.getElementById(id); };
var out = $("dc-out"), err = $("dc-error");
function post(path, body) {
err.textContent = "";
out.textContent = "...";
return fetch(path, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
}).then(function (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);
}
});
}).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
}
function json(id) {
var raw = $(id).value.trim();
if (!raw) return undefined;
try { return JSON.parse(raw); } catch (e) { err.textContent = id + " is not valid JSON"; return null; }
}
function callBody() {
var params = json("dc-params"), body = json("dc-body");
if (params === null || body === null) return null;
var b = {
api: $("dc-api").value.trim(),
method: $("dc-method").value.trim(),
location: $("dc-location").value.trim(),
auth: $("dc-auth").value,
maxPages: parseInt($("dc-pages").value, 10) || 1,
wait: $("dc-wait").checked,
confirm: $("dc-confirm").checked
};
if (params !== undefined) b.params = params;
if (body !== undefined) b.body = body;
return b;
}
fetch("/google/discovery/status").then(function (r) { return r.json(); }).then(function (s) {
$("dc-config").textContent = s.configured
? "Google credential found. Documents cached: " + s.cached_apis + "."
: "No Google credential: every action answers 503 until one is configured.";
});
$("dc-apis").onclick = function () {
post("/google/discovery/apis", { filter: $("dc-filter").value.trim(), limit: 50 });
};
$("dc-methods").onclick = function () {
post("/google/discovery/methods", { api: $("dc-api").value.trim(), filter: $("dc-method").value.trim(), limit: 100 });
};
$("dc-describe").onclick = function () {
post("/google/discovery/describe", { api: $("dc-api").value.trim(), method: $("dc-method").value.trim() });
};
$("dc-plan").onclick = function () { var b = callBody(); if (b) post("/google/discovery/plan", b); };
$("dc-call").onclick = function () { var b = callBody(); if (b) post("/google/discovery/call", b); };
})();