resource-engine-fastapi 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- resource_engine_fastapi/__init__.py +39 -0
- resource_engine_fastapi/access.py +110 -0
- resource_engine_fastapi/asyncapi.py +47 -0
- resource_engine_fastapi/auth.py +158 -0
- resource_engine_fastapi/authz.py +100 -0
- resource_engine_fastapi/caching.py +111 -0
- resource_engine_fastapi/docs.py +77 -0
- resource_engine_fastapi/documents.py +219 -0
- resource_engine_fastapi/email_events.py +85 -0
- resource_engine_fastapi/errors.py +178 -0
- resource_engine_fastapi/events.py +109 -0
- resource_engine_fastapi/explorer/__init__.py +441 -0
- resource_engine_fastapi/explorer/assets/auth.js +171 -0
- resource_engine_fastapi/explorer/assets/console.js +340 -0
- resource_engine_fastapi/explorer/assets/events.js +189 -0
- resource_engine_fastapi/explorer/assets/explorer.css +156 -0
- resource_engine_fastapi/explorer/assets/explorer.js +275 -0
- resource_engine_fastapi/explorer/assets/jobs.js +143 -0
- resource_engine_fastapi/explorer/assets/messages.js +85 -0
- resource_engine_fastapi/explorer/assets/publish.js +117 -0
- resource_engine_fastapi/explorer/data.py +84 -0
- resource_engine_fastapi/explorer/events.py +182 -0
- resource_engine_fastapi/explorer/jobs.py +167 -0
- resource_engine_fastapi/explorer/labels.py +198 -0
- resource_engine_fastapi/explorer/locale/ca/LC_MESSAGES/explorer.mo +0 -0
- resource_engine_fastapi/explorer/locale/ca/LC_MESSAGES/explorer.po +417 -0
- resource_engine_fastapi/explorer/locale/es/LC_MESSAGES/explorer.mo +0 -0
- resource_engine_fastapi/explorer/locale/es/LC_MESSAGES/explorer.po +417 -0
- resource_engine_fastapi/explorer/messages.py +73 -0
- resource_engine_fastapi/explorer/publish.py +85 -0
- resource_engine_fastapi/exports.py +186 -0
- resource_engine_fastapi/i18n.py +76 -0
- resource_engine_fastapi/py.typed +0 -0
- resource_engine_fastapi/router.py +1031 -0
- resource_engine_fastapi/versioning.py +49 -0
- resource_engine_fastapi-0.1.0.dist-info/METADATA +32 -0
- resource_engine_fastapi-0.1.0.dist-info/RECORD +39 -0
- resource_engine_fastapi-0.1.0.dist-info/WHEEL +4 -0
- resource_engine_fastapi-0.1.0.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/* Try-it console: the explorer's Operations tab (mount_explorer).
|
|
2
|
+
Calls the running API with the visitor's own token; reads by default, changes
|
|
3
|
+
only after "Allow changes". Everything from the API is shown as text.
|
|
4
|
+
Other panels (events.js, explorer.js) use window.ResourceExplorer. */
|
|
5
|
+
(function () {
|
|
6
|
+
var C = window.CONSOLE;
|
|
7
|
+
if (!C) return;
|
|
8
|
+
var L = C.labels;
|
|
9
|
+
var api = (C.api || "").replace(/\/$/, "");
|
|
10
|
+
var lang = document.documentElement.lang || "";
|
|
11
|
+
|
|
12
|
+
function load(key) { try { return sessionStorage.getItem(key) || ""; } catch (e) { return ""; } }
|
|
13
|
+
function save(key, value) { try { sessionStorage.setItem(key, value); } catch (e) { /* private mode */ } }
|
|
14
|
+
|
|
15
|
+
function el(tag, props, children) {
|
|
16
|
+
var node = document.createElement(tag);
|
|
17
|
+
Object.keys(props || {}).forEach(function (k) {
|
|
18
|
+
if (k === "text") node.textContent = props[k];
|
|
19
|
+
else if (k === "className") node.className = props[k];
|
|
20
|
+
else node.setAttribute(k, props[k]);
|
|
21
|
+
});
|
|
22
|
+
(children || []).forEach(function (child) { if (child) node.appendChild(child); });
|
|
23
|
+
return node;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function settings() {
|
|
27
|
+
var token = el("input", { type: "password", autocomplete: "off", spellcheck: "false",
|
|
28
|
+
placeholder: L.token_hint, "aria-label": L.token });
|
|
29
|
+
token.value = load("re-console-token");
|
|
30
|
+
token.addEventListener("change", function () { save("re-console-token", token.value.trim()); });
|
|
31
|
+
var allow = el("input", { type: "checkbox", "aria-label": L.allow_changes });
|
|
32
|
+
allow.checked = load("re-console-allow") === "1";
|
|
33
|
+
allow.addEventListener("change", function () { save("re-console-allow", allow.checked ? "1" : ""); });
|
|
34
|
+
return el("div", { className: "console-settings" }, [
|
|
35
|
+
el("label", {}, [el("span", { text: L.token }), token]),
|
|
36
|
+
el("label", { className: "inline" }, [allow, el("span", { text: L.allow_changes })]),
|
|
37
|
+
]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function allowed() { return load("re-console-allow") === "1"; }
|
|
41
|
+
|
|
42
|
+
function call(method, path, body, headers) {
|
|
43
|
+
var init = { method: method, headers: Object.assign({ "Accept": "application/json" }, headers || {}) };
|
|
44
|
+
var token = load("re-console-token");
|
|
45
|
+
if (token) init.headers["Authorization"] = "Bearer " + token; // also API keys (rek_...)
|
|
46
|
+
if (lang) init.headers["Accept-Language"] = lang;
|
|
47
|
+
if (body !== undefined) {
|
|
48
|
+
init.headers["Content-Type"] = "application/json";
|
|
49
|
+
init.body = typeof body === "string" ? body : JSON.stringify(body);
|
|
50
|
+
}
|
|
51
|
+
var started = performance.now();
|
|
52
|
+
return fetch(api + path, init).then(function (response) {
|
|
53
|
+
// a file, whatever the OpenAPI document says: offered for download
|
|
54
|
+
if (response.ok && isFile(response)) return fileResult(response, started);
|
|
55
|
+
return response.text().then(function (text) {
|
|
56
|
+
var data = null;
|
|
57
|
+
try { data = JSON.parse(text); } catch (e) { /* not JSON */ }
|
|
58
|
+
return { status: response.status, statusText: response.statusText, ok: response.ok,
|
|
59
|
+
headers: response.headers, text: text, data: data,
|
|
60
|
+
ms: Math.round(performance.now() - started) };
|
|
61
|
+
});
|
|
62
|
+
}, function (error) {
|
|
63
|
+
return { status: 0, statusText: L.unreachable + " (" + error.message + ")", ok: false,
|
|
64
|
+
headers: new Headers(), text: "", data: null, ms: 0 };
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// An answer is a file when it says so (an attachment) or isn't JSON or text.
|
|
69
|
+
function isFile(response) {
|
|
70
|
+
var disposition = (response.headers.get("content-disposition") || "").toLowerCase();
|
|
71
|
+
if (disposition.indexOf("attachment") === 0) return true;
|
|
72
|
+
var type = (response.headers.get("content-type") || "").toLowerCase();
|
|
73
|
+
if (!type || response.status === 204) return false;
|
|
74
|
+
return !/json|^text\/(plain|html)|event-stream/.test(type);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function fileResult(response, started) {
|
|
78
|
+
return response.blob().then(function (blob) {
|
|
79
|
+
var disposition = response.headers.get("content-disposition") || "";
|
|
80
|
+
var match = /filename\*=UTF-8''([^;]+)|filename="?([^";]+)"?/i.exec(disposition);
|
|
81
|
+
var name = match ? decodeURIComponent(match[1] || match[2]) : "download";
|
|
82
|
+
return { status: response.status, statusText: response.statusText, ok: true,
|
|
83
|
+
headers: response.headers, blob: blob, filename: name,
|
|
84
|
+
ms: Math.round(performance.now() - started) };
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// A file answer (an export, a printout, a document): fetched with the token
|
|
89
|
+
// and offered as a link, never shown as text.
|
|
90
|
+
function fetchFile(method, path, headers) {
|
|
91
|
+
var init = { method: method, headers: Object.assign({}, headers || {}) };
|
|
92
|
+
var token = load("re-console-token");
|
|
93
|
+
if (token) init.headers["Authorization"] = "Bearer " + token;
|
|
94
|
+
if (lang) init.headers["Accept-Language"] = lang;
|
|
95
|
+
var started = performance.now();
|
|
96
|
+
return fetch(api + path, init).then(function (response) {
|
|
97
|
+
var ms = Math.round(performance.now() - started);
|
|
98
|
+
if (!response.ok) {
|
|
99
|
+
return response.text().then(function (text) {
|
|
100
|
+
var data = null;
|
|
101
|
+
try { data = JSON.parse(text); } catch (e) { /* not JSON */ }
|
|
102
|
+
return { status: response.status, statusText: response.statusText, ok: false,
|
|
103
|
+
headers: response.headers, text: text, data: data, ms: ms };
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return fileResult(response, started);
|
|
107
|
+
}, function (error) {
|
|
108
|
+
return { status: 0, statusText: L.unreachable + " (" + error.message + ")", ok: false,
|
|
109
|
+
headers: new Headers(), text: "", data: null, ms: 0 };
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function showFile(target, result) {
|
|
114
|
+
if (!result.blob) { show(target, result); return; }
|
|
115
|
+
target.textContent = "";
|
|
116
|
+
target.appendChild(el("p", { className: "console-status ok",
|
|
117
|
+
text: result.status + " " + result.statusText + " · " + result.ms + " ms" }));
|
|
118
|
+
var link = el("a", { href: URL.createObjectURL(result.blob), download: result.filename,
|
|
119
|
+
className: "console-download", text: L.download + ": " + result.filename +
|
|
120
|
+
" (" + Math.max(1, Math.round(result.blob.size / 1024)) + " KB)" });
|
|
121
|
+
target.appendChild(el("p", {}, [link]));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// A background export: check it until it is done, then offer the file.
|
|
125
|
+
function followExport(target, location) {
|
|
126
|
+
var path = location.indexOf(api) === 0 ? location.slice(api.length) : location;
|
|
127
|
+
var button = el("button", { type: "button", text: L.export_check });
|
|
128
|
+
var box = el("div", { className: "console-output", "aria-live": "polite" });
|
|
129
|
+
button.addEventListener("click", function () {
|
|
130
|
+
box.textContent = L.sending;
|
|
131
|
+
call("GET", path).then(function (result) {
|
|
132
|
+
if (result.ok && result.data && result.data.status === "done") {
|
|
133
|
+
fetchFile("GET", path + "/content").then(function (file) { showFile(box, file); });
|
|
134
|
+
} else {
|
|
135
|
+
show(box, result);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
target.appendChild(el("p", { className: "console-next" }, [button]));
|
|
140
|
+
target.appendChild(box);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function show(target, result) {
|
|
144
|
+
if (result.blob) { showFile(target, result); return; }
|
|
145
|
+
target.textContent = "";
|
|
146
|
+
var line = result.status ? result.status + " " + result.statusText + " · " + result.ms + " ms"
|
|
147
|
+
: result.statusText;
|
|
148
|
+
target.appendChild(el("p", { className: "console-status " + (result.ok ? "ok" : "fail"), text: line }));
|
|
149
|
+
var shown = ["content-language", "etag", "x-request-id", "retry-after", "location"]
|
|
150
|
+
.filter(function (h) { return result.headers.get(h); })
|
|
151
|
+
.map(function (h) { return h + ": " + result.headers.get(h); });
|
|
152
|
+
if (shown.length) target.appendChild(el("pre", { className: "console-headers", text: shown.join("\n") }));
|
|
153
|
+
var body = result.data !== null ? JSON.stringify(result.data, null, 2) : result.text;
|
|
154
|
+
if (body) target.appendChild(el("pre", { text: body.slice(0, 200000) }));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ------------------------------------------------------------ one operation
|
|
158
|
+
// The form of one HTTP operation: parameters, body, Execute, the answer.
|
|
159
|
+
// jump(op): open another operation's row (the allowed transitions).
|
|
160
|
+
function operationForm(resource, op, ops, jump) {
|
|
161
|
+
var form = el("div", { className: "console-form" });
|
|
162
|
+
var output = el("div", { className: "console-output", "aria-live": "polite" });
|
|
163
|
+
var extra = el("div", { className: "console-extra" });
|
|
164
|
+
var inputs = {};
|
|
165
|
+
var bodyField = null;
|
|
166
|
+
|
|
167
|
+
op.params.forEach(function (p) {
|
|
168
|
+
var input = el("input", { type: "text", "aria-label": p.name, placeholder: p.description || "" });
|
|
169
|
+
if (p.in === "path" && /id$/.test(p.name)) input.value = load("re-console-id-" + resource);
|
|
170
|
+
inputs[p.name] = { param: p, input: input };
|
|
171
|
+
var label = el("label", { className: p.in === "path" ? "required" : "" }, [
|
|
172
|
+
el("span", { text: p.name + " (" + (p.in === "header" ? L.header : p.in) + ")" }), input]);
|
|
173
|
+
form.appendChild(label);
|
|
174
|
+
});
|
|
175
|
+
if (op.body !== null && op.body !== undefined) {
|
|
176
|
+
bodyField = el("textarea", { rows: "8", spellcheck: "false", "aria-label": L.body });
|
|
177
|
+
bodyField.value = JSON.stringify(op.body, null, 2);
|
|
178
|
+
form.appendChild(el("label", { className: "wide" }, [el("span", { text: L.body }), bodyField]));
|
|
179
|
+
}
|
|
180
|
+
if (op.upload) form.appendChild(el("p", { className: "note wide", text: L.upload_note }));
|
|
181
|
+
var reads = op.method === "GET" || op.safe;
|
|
182
|
+
var send = el("button", { type: "button", className: "execute", text: reads ? L.send : L.send_change });
|
|
183
|
+
send.addEventListener("click", function () { submit(); });
|
|
184
|
+
var curl = el("button", { type: "button", className: "secondary", text: L.copy_curl });
|
|
185
|
+
curl.addEventListener("click", function () { copyCurl(); });
|
|
186
|
+
var actions = el("div", { className: "wide" }, [send, curl]);
|
|
187
|
+
if (!reads && !allowed()) actions.appendChild(el("span", { className: "note", text: " " + L.changes_off }));
|
|
188
|
+
form.appendChild(actions);
|
|
189
|
+
|
|
190
|
+
// The request the form describes: {target, headers, body}, or null after
|
|
191
|
+
// telling what's missing.
|
|
192
|
+
function request() {
|
|
193
|
+
var path = op.path, query = [], headers = {}, missing = [];
|
|
194
|
+
Object.keys(inputs).forEach(function (name) {
|
|
195
|
+
var item = inputs[name], value = item.input.value.trim();
|
|
196
|
+
if (item.param.in === "path") {
|
|
197
|
+
if (!value) missing.push(name);
|
|
198
|
+
path = path.replace("{" + name + "}", encodeURIComponent(value));
|
|
199
|
+
if (/id$/.test(name) && value) save("re-console-id-" + resource, value);
|
|
200
|
+
} else if (value && item.param.in === "query") {
|
|
201
|
+
query.push(encodeURIComponent(name) + "=" + encodeURIComponent(value));
|
|
202
|
+
} else if (value) headers[name] = value;
|
|
203
|
+
});
|
|
204
|
+
if (missing.length) { output.textContent = L.missing + ": " + missing.join(", "); return null; }
|
|
205
|
+
var body;
|
|
206
|
+
if (bodyField) {
|
|
207
|
+
try { body = JSON.parse(bodyField.value || "null"); } catch (e) {
|
|
208
|
+
output.textContent = L.bad_json + ": " + e.message; return null;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return { target: path + (query.length ? "?" + query.join("&") : ""), headers: headers, body: body };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function quote(text) { return "'" + String(text).replace(/'/g, "'\\''") + "'"; }
|
|
215
|
+
|
|
216
|
+
// The same request as a curl command; the token stays a variable.
|
|
217
|
+
function copyCurl() {
|
|
218
|
+
var req = request();
|
|
219
|
+
if (!req) return;
|
|
220
|
+
var url = (api || location.origin) + req.target;
|
|
221
|
+
var lines = ["curl -X " + op.method + " " + quote(url)];
|
|
222
|
+
// double quotes: the shell puts the token in ($TOKEN), never the page
|
|
223
|
+
if (load("re-console-token")) lines.push(' -H "Authorization: Bearer $TOKEN"');
|
|
224
|
+
if (lang) lines.push(" -H " + quote("Accept-Language: " + lang));
|
|
225
|
+
Object.keys(req.headers).forEach(function (name) {
|
|
226
|
+
lines.push(" -H " + quote(name + ": " + req.headers[name]));
|
|
227
|
+
});
|
|
228
|
+
if (req.body !== undefined) {
|
|
229
|
+
lines.push(" -H " + quote("Content-Type: application/json"));
|
|
230
|
+
lines.push(" -d " + quote(JSON.stringify(req.body)));
|
|
231
|
+
}
|
|
232
|
+
if (op.download) lines.push(" -OJ");
|
|
233
|
+
var text = lines.join(" \\\n");
|
|
234
|
+
output.textContent = "";
|
|
235
|
+
output.appendChild(el("pre", { className: "curl", text: text }));
|
|
236
|
+
if (navigator.clipboard) {
|
|
237
|
+
navigator.clipboard.writeText(text).then(function () {
|
|
238
|
+
output.appendChild(el("p", { className: "note", text: L.copied }));
|
|
239
|
+
}, function () { /* the text is there to copy by hand */ });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function submit() {
|
|
244
|
+
if (op.method !== "GET" && !op.safe && !allowed()) { output.textContent = L.changes_off; return; }
|
|
245
|
+
var req = request();
|
|
246
|
+
if (!req) return;
|
|
247
|
+
var target = req.target, headers = req.headers, body = req.body, path = target.split("?")[0];
|
|
248
|
+
output.textContent = L.sending;
|
|
249
|
+
extra.textContent = "";
|
|
250
|
+
if (op.download) {
|
|
251
|
+
fetchFile(op.method, target, headers).then(function (result) {
|
|
252
|
+
showFile(output, result);
|
|
253
|
+
if (result.status === 403 && op.action) explainButton();
|
|
254
|
+
});
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
call(op.method, target, body, headers).then(function (result) {
|
|
258
|
+
show(output, result);
|
|
259
|
+
// a record just created or read: its id fills the next forms
|
|
260
|
+
if (result.ok && result.data && result.data.id !== undefined && result.data.id !== null) {
|
|
261
|
+
save("re-console-id-" + resource, String(result.data.id));
|
|
262
|
+
}
|
|
263
|
+
if (result.status === 403 && op.action) explainButton();
|
|
264
|
+
if (result.status === 202 && result.headers.get("location")) {
|
|
265
|
+
followExport(extra, result.headers.get("location"));
|
|
266
|
+
}
|
|
267
|
+
if (result.ok && op.method === "GET" && /\{[a-z_]*id\}$/.test(op.path)) nextSteps(path);
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function explainButton() {
|
|
272
|
+
var button = el("button", { type: "button", className: "secondary", text: L.why });
|
|
273
|
+
button.addEventListener("click", function () {
|
|
274
|
+
var id = null;
|
|
275
|
+
Object.keys(inputs).forEach(function (n) {
|
|
276
|
+
if (inputs[n].param.in === "path" && inputs[n].input.value) id = inputs[n].input.value;
|
|
277
|
+
});
|
|
278
|
+
var request = { action: op.action };
|
|
279
|
+
if (id !== null) request.entity_id = /^\d+$/.test(id) ? Number(id) : id;
|
|
280
|
+
call("POST", "/authz/explain", request).then(function (result) {
|
|
281
|
+
var box = el("div", { className: "console-output" });
|
|
282
|
+
extra.textContent = ""; extra.appendChild(box);
|
|
283
|
+
if (result.status !== 403) { show(box, result); return; }
|
|
284
|
+
// explaining is for policy editors: show the caller's own view instead
|
|
285
|
+
call("GET", "/authz/me").then(function (me) {
|
|
286
|
+
if (!me.ok || !me.data) { show(box, result); return; }
|
|
287
|
+
var state = (me.data.actions || {})[op.action] || L.not_granted;
|
|
288
|
+
box.appendChild(el("p", { text: L.explain_editors }));
|
|
289
|
+
box.appendChild(el("pre", { text: L.your_roles + ": " + (me.data.roles || []).join(", ") +
|
|
290
|
+
"\n" + op.action + ": " + state }));
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
extra.textContent = "";
|
|
295
|
+
extra.appendChild(button);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function nextSteps(recordPath) {
|
|
299
|
+
var transitions = ops.filter(function (o) { return /\/transitions\/[^/]+$/.test(o.path); });
|
|
300
|
+
if (!transitions.length) return;
|
|
301
|
+
call("GET", recordPath + "/transitions").then(function (result) {
|
|
302
|
+
if (!result.ok || !Array.isArray(result.data)) return;
|
|
303
|
+
var row = el("p", { className: "console-next" }, [el("span", { text: L.next + ": " })]);
|
|
304
|
+
if (!result.data.length) row.appendChild(el("span", { text: L.none }));
|
|
305
|
+
result.data.forEach(function (event) {
|
|
306
|
+
var target = transitions.filter(function (o) {
|
|
307
|
+
return o.path.slice(o.path.lastIndexOf("/") + 1) === event;
|
|
308
|
+
})[0];
|
|
309
|
+
var button = el("button", { type: "button", className: "secondary", text: event });
|
|
310
|
+
button.addEventListener("click", function () { if (target && jump) jump(target); });
|
|
311
|
+
row.appendChild(button);
|
|
312
|
+
});
|
|
313
|
+
extra.appendChild(row);
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return el("div", { className: "try-it" }, [form, output, extra]);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// ---------------------------------------------------------------- the home page
|
|
321
|
+
function whoAmI(embedded) {
|
|
322
|
+
var output = el("div", { className: "console-output", "aria-live": "polite" });
|
|
323
|
+
var button = el("button", { type: "button", text: L.who_am_i });
|
|
324
|
+
button.addEventListener("click", function () {
|
|
325
|
+
output.textContent = L.sending;
|
|
326
|
+
call("GET", "/authz/me").then(function (result) { show(output, result); });
|
|
327
|
+
});
|
|
328
|
+
return el("details", { className: "console", id: "try-it" }, [
|
|
329
|
+
el("summary", { text: L.who_am_i }),
|
|
330
|
+
el("p", { className: "note", text: L.who_intro }),
|
|
331
|
+
embedded ? null : settings(), button, output,
|
|
332
|
+
]);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
window.ResourceExplorer = {
|
|
336
|
+
config: C, labels: L, api: api, el: el, load: load, save: save, allowed: allowed,
|
|
337
|
+
settings: settings, call: call, show: show, fetchFile: fetchFile, showFile: showFile,
|
|
338
|
+
operationForm: operationForm, whoAmI: whoAmI, panels: {},
|
|
339
|
+
};
|
|
340
|
+
})();
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/* SUB: committed events over server-sent events, read with fetch so the
|
|
2
|
+
visitor's token goes in the Authorization header (EventSource cannot send
|
|
3
|
+
it). R.subscribe({resource, type}) is the body of a SUB row: the stream of
|
|
4
|
+
that event type of that resource; without a preset, every event. */
|
|
5
|
+
(function () {
|
|
6
|
+
var R = window.ResourceExplorer;
|
|
7
|
+
if (!R || !R.config.events) return;
|
|
8
|
+
var C = R.config, L = R.labels, el = R.el, E = C.events;
|
|
9
|
+
var MAX = 500;
|
|
10
|
+
|
|
11
|
+
function headers() {
|
|
12
|
+
var h = { "Accept": "text/event-stream" };
|
|
13
|
+
var token = R.load("re-console-token");
|
|
14
|
+
if (token) h["Authorization"] = "Bearer " + token;
|
|
15
|
+
var lang = document.documentElement.lang;
|
|
16
|
+
if (lang) h["Accept-Language"] = lang;
|
|
17
|
+
return h;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function select(label, values, name) {
|
|
21
|
+
var input = el("select", { "aria-label": label, name: name });
|
|
22
|
+
input.appendChild(el("option", { value: "", text: L.any }));
|
|
23
|
+
values.forEach(function (v) { input.appendChild(el("option", { value: v, text: v })); });
|
|
24
|
+
return { input: input, label: el("label", {}, [el("span", { text: label }), input]) };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function text(label, name) {
|
|
28
|
+
var input = el("input", { type: "text", "aria-label": label, name: name, spellcheck: "false" });
|
|
29
|
+
return { input: input, label: el("label", {}, [el("span", { text: label }), input]) };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Parses "event: x\nid: y\ndata: {...}\n\n" frames out of a growing buffer.
|
|
33
|
+
function frames(buffer, onFrame) {
|
|
34
|
+
var parts = buffer.split("\n\n");
|
|
35
|
+
var rest = parts.pop();
|
|
36
|
+
parts.forEach(function (frame) {
|
|
37
|
+
var data = [], type = null;
|
|
38
|
+
frame.split("\n").forEach(function (line) {
|
|
39
|
+
if (line.indexOf("data:") === 0) data.push(line.slice(5).replace(/^ /, ""));
|
|
40
|
+
else if (line.indexOf("event:") === 0) type = line.slice(6).trim();
|
|
41
|
+
});
|
|
42
|
+
if (data.length) onFrame(type, data.join("\n"));
|
|
43
|
+
});
|
|
44
|
+
return rest;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function panel(preset) {
|
|
48
|
+
preset = preset || {};
|
|
49
|
+
var resource = select(L.filter_resource, E.resources || [], "resource");
|
|
50
|
+
var type = select(L.filter_type, (E.types || []).map(function (t) { return t.type; }), "type");
|
|
51
|
+
if (preset.resource) resource.input.value = preset.resource;
|
|
52
|
+
if (preset.type) type.input.value = preset.type;
|
|
53
|
+
var version = text(L.filter_version, "version");
|
|
54
|
+
var id = text(L.filter_id, "id");
|
|
55
|
+
var connect = el("button", { type: "button", className: "execute", text: L.connect });
|
|
56
|
+
var pause = el("button", { type: "button", text: L.pause, disabled: "disabled" });
|
|
57
|
+
var clear = el("button", { type: "button", text: L.clear });
|
|
58
|
+
var status = el("div", { className: "note", "aria-live": "polite", text: L.disconnected });
|
|
59
|
+
var list = el("ol", { className: "events-list", reversed: "reversed" });
|
|
60
|
+
var detail = el("div", { className: "console-output events-detail" });
|
|
61
|
+
var controller = null, paused = false, held = [];
|
|
62
|
+
|
|
63
|
+
function query() {
|
|
64
|
+
var q = [];
|
|
65
|
+
[["resource", resource], ["type", type], ["version", version], ["id", id]].forEach(
|
|
66
|
+
function (pair) {
|
|
67
|
+
var value = pair[1].input.value.trim();
|
|
68
|
+
if (value) q.push(pair[0] + "=" + encodeURIComponent(value));
|
|
69
|
+
});
|
|
70
|
+
return q.length ? "?" + q.join("&") : "";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function add(event) {
|
|
74
|
+
var env = event.envelope;
|
|
75
|
+
var when = (env.occurred_at || "").replace("T", " ").slice(0, 19);
|
|
76
|
+
var about = event.resource ? event.resource + " #" + event.entity_id : "—";
|
|
77
|
+
var item = el("li", {}, [el("button", { type: "button", className: "event-row",
|
|
78
|
+
text: when + " " + env.type + " v" + (env.schema_version || "1") + " " + about })]);
|
|
79
|
+
item.firstChild.addEventListener("click", function () { open(event); });
|
|
80
|
+
list.insertBefore(item, list.firstChild);
|
|
81
|
+
while (list.children.length > MAX) list.removeChild(list.lastChild);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function open(event) {
|
|
85
|
+
detail.textContent = "";
|
|
86
|
+
var env = Object.assign({}, event.envelope);
|
|
87
|
+
var payload = env.payload;
|
|
88
|
+
if (!event.payload_visible) delete env.payload;
|
|
89
|
+
detail.appendChild(el("h3", { text: L.envelope }));
|
|
90
|
+
detail.appendChild(el("pre", { text: JSON.stringify(env, null, 2) }));
|
|
91
|
+
if (event.payload_visible) {
|
|
92
|
+
detail.appendChild(el("h3", { text: L.payload }));
|
|
93
|
+
detail.appendChild(el("pre", { text: JSON.stringify(payload, null, 2) }));
|
|
94
|
+
} else {
|
|
95
|
+
detail.appendChild(el("p", { className: "note", text: L.payload_hidden }));
|
|
96
|
+
}
|
|
97
|
+
if (event.record) {
|
|
98
|
+
detail.appendChild(el("h3", { text: L.record }));
|
|
99
|
+
detail.appendChild(el("pre", { text: JSON.stringify(event.record, null, 2) }));
|
|
100
|
+
}
|
|
101
|
+
detail.appendChild(el("h3", { text: L.deliveries }));
|
|
102
|
+
if (!E.deliveries) {
|
|
103
|
+
detail.appendChild(el("p", { className: "note", text: L.deliveries_off }));
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
var box = el("div", {});
|
|
107
|
+
detail.appendChild(box);
|
|
108
|
+
R.call("GET", E.deliveries.replace("{message_id}", encodeURIComponent(env.id)))
|
|
109
|
+
.then(function (result) {
|
|
110
|
+
if (!result.ok || !Array.isArray(result.data)) { R.show(box, result); return; }
|
|
111
|
+
if (!result.data.length) {
|
|
112
|
+
box.appendChild(el("p", { className: "note", text: L.no_deliveries }));
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
box.appendChild(el("pre", { text: result.data.map(function (d) {
|
|
116
|
+
return d.consumer + ": " + d.outcome + " · " + Math.round(d.seconds * 1000) + " ms" +
|
|
117
|
+
(d.error ? " · " + d.error : "");
|
|
118
|
+
}).join("\n") }));
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function stop(message) {
|
|
123
|
+
if (controller) controller.abort();
|
|
124
|
+
controller = null;
|
|
125
|
+
connect.textContent = L.connect;
|
|
126
|
+
pause.setAttribute("disabled", "disabled");
|
|
127
|
+
status.textContent = message || L.disconnected;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function start() {
|
|
131
|
+
controller = new AbortController();
|
|
132
|
+
connect.textContent = L.disconnect;
|
|
133
|
+
pause.removeAttribute("disabled");
|
|
134
|
+
status.textContent = L.sending;
|
|
135
|
+
var mine = controller;
|
|
136
|
+
fetch(R.api + E.stream + query(), { headers: headers(), signal: mine.signal })
|
|
137
|
+
.then(function (response) {
|
|
138
|
+
if (!response.ok) {
|
|
139
|
+
return response.text().then(function (body) {
|
|
140
|
+
var data = null;
|
|
141
|
+
try { data = JSON.parse(body); } catch (e) { /* not JSON */ }
|
|
142
|
+
stop();
|
|
143
|
+
R.show(status, { status: response.status, statusText: response.statusText,
|
|
144
|
+
ok: false, headers: response.headers, text: body, data: data, ms: 0 });
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
status.textContent = L.connected;
|
|
148
|
+
var reader = response.body.getReader(), decoder = new TextDecoder(), buffer = "";
|
|
149
|
+
function pump() {
|
|
150
|
+
return reader.read().then(function (chunk) {
|
|
151
|
+
if (chunk.done) { if (controller === mine) stop(); return; }
|
|
152
|
+
buffer = frames(buffer + decoder.decode(chunk.value, { stream: true }),
|
|
153
|
+
function (kind, data) {
|
|
154
|
+
if (kind !== "event") return;
|
|
155
|
+
var event;
|
|
156
|
+
try { event = JSON.parse(data); } catch (e) { return; }
|
|
157
|
+
if (paused) held.push(event); else add(event);
|
|
158
|
+
});
|
|
159
|
+
return pump();
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return pump();
|
|
163
|
+
})
|
|
164
|
+
.catch(function (error) {
|
|
165
|
+
if (controller === mine) stop(L.unreachable + " (" + error.message + ")");
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
connect.addEventListener("click", function () { if (controller) stop(); else start(); });
|
|
170
|
+
pause.addEventListener("click", function () {
|
|
171
|
+
paused = !paused;
|
|
172
|
+
pause.textContent = paused ? L.resume : L.pause;
|
|
173
|
+
if (!paused) { held.forEach(add); held = []; }
|
|
174
|
+
});
|
|
175
|
+
clear.addEventListener("click", function () { list.textContent = ""; detail.textContent = ""; });
|
|
176
|
+
|
|
177
|
+
return el("div", { className: "events-panel" }, [
|
|
178
|
+
preset.resource ? null : el("p", { className: "note", text: L.events_intro }),
|
|
179
|
+
el("div", { className: "console-form" }, [
|
|
180
|
+
preset.resource ? null : resource.label, preset.type ? null : type.label,
|
|
181
|
+
version.label, id.label,
|
|
182
|
+
el("div", { className: "wide" }, [connect, pause, clear])]),
|
|
183
|
+
status,
|
|
184
|
+
el("div", { className: "events-columns" }, [list, detail]),
|
|
185
|
+
]);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
R.subscribe = panel;
|
|
189
|
+
})();
|