// Enterprise APIs page: thin client over /google/enterprise/*. Every Google // call is a POST; the status route says whether a credential is configured. (function () { "use strict"; var $ = function (id) { return document.getElementById(id); }; var out = $("e-out"), err = $("e-error"); function show(text, isError) { (isError ? err : out).textContent = text; if (isError) out.textContent = ""; else err.textContent = ""; } function request(method, path, body) { var opts = { method: method }; if (body !== undefined) { opts.headers = { "Content-Type": "application/json" }; opts.body = JSON.stringify(body); } return fetch(path, opts).then(function (res) { return res.text().then(function (text) { if (!res.ok) { var msg = "HTTP " + res.status; try { var j = JSON.parse(text); msg = (j.error || "") + ": " + (j.message || msg); } catch (e) {} throw new Error(msg); } return JSON.parse(text); }); }); } function json(id) { var v = $(id).value.trim(); return v ? JSON.parse(v) : undefined; } function run(promise) { promise.then(function (j) { show(JSON.stringify(j, null, 2), false); }) .catch(function (e) { show(e.message, true); }); } $("e-find").addEventListener("click", function () { var path = "/google/enterprise/calls?api=" + encodeURIComponent($("e-api").value) + "&q=" + encodeURIComponent($("e-q").value); run(request("GET", path)); }); $("e-run").addEventListener("click", function () { var body; try { body = { api: $("e-api").value, rpc: $("e-rpc").value.trim(), path: json("e-path") || {}, query: json("e-query") || {}, confirm: $("e-confirm").checked }; var b = json("e-body"); if (b !== undefined) body.body = b; } catch (e) { show("Invalid JSON: " + e.message, true); return; } run(request("POST", "/google/enterprise/call", body)); }); request("GET", "/google/enterprise/status").then(function (s) { $("e-config").textContent = s.configured && s.project_set ? "Google credential and project are set. " + s.calls + " calls available." : "Not configured: set a Google credential and GOOGLE_CLOUD_PROJECT to call Google."; }).catch(function () { $("e-config").textContent = "Status unavailable."; }); })();