forked from Soroban-Smart-Block-Explorer/Soroban-Smart-Block
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry-it.html
More file actions
449 lines (432 loc) · 14.3 KB
/
Copy pathtry-it.html
File metadata and controls
449 lines (432 loc) · 14.3 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Try-it Console · Soroban Smart Block Explorer</title>
<meta
name="description"
content="Interactive request console for the Soroban Smart Block Explorer API with multi-language code snippets and a WebSocket tester."
/>
<style>
:root {
--bg: #0d1117;
--panel: #161b22;
--border: #30363d;
--text: #e6edf3;
--muted: #8b949e;
--accent: #58a6ff;
--green: #3fb950;
--mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
background: var(--bg);
color: var(--text);
font-family:
system-ui,
-apple-system,
sans-serif;
line-height: 1.5;
}
header {
display: flex;
align-items: center;
gap: 1rem;
padding: 0.85rem 1.25rem;
background: var(--panel);
border-bottom: 1px solid var(--border);
}
header a {
color: var(--accent);
text-decoration: none;
font-size: 0.9rem;
}
header .spacer {
flex: 1;
}
main {
max-width: 1100px;
margin: 0 auto;
padding: 1.5rem 1.25rem 4rem;
}
h1 {
font-size: 1.3rem;
}
h2 {
font-size: 1.05rem;
margin-top: 2rem;
border-bottom: 1px solid var(--border);
padding-bottom: 0.4rem;
}
label {
display: block;
font-size: 0.8rem;
color: var(--muted);
margin: 0.75rem 0 0.25rem;
}
input,
select,
textarea {
width: 100%;
background: #0b0f14;
color: var(--text);
border: 1px solid var(--border);
border-radius: 6px;
padding: 0.5rem 0.6rem;
font-family: var(--mono);
font-size: 0.85rem;
}
textarea {
min-height: 120px;
resize: vertical;
}
.row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
}
button {
background: var(--accent);
color: #0d1117;
border: 0;
border-radius: 6px;
padding: 0.55rem 1.1rem;
font-weight: 600;
cursor: pointer;
margin-top: 1rem;
}
button.secondary {
background: transparent;
color: var(--accent);
border: 1px solid var(--border);
}
.params {
margin-top: 0.5rem;
}
pre {
background: #0b0f14;
border: 1px solid var(--border);
border-radius: 6px;
padding: 0.85rem;
overflow: auto;
font-family: var(--mono);
font-size: 0.82rem;
}
.tabs {
display: flex;
gap: 0.4rem;
flex-wrap: wrap;
margin-top: 0.75rem;
}
.tabs button {
margin: 0;
background: var(--panel);
color: var(--muted);
border: 1px solid var(--border);
padding: 0.35rem 0.8rem;
font-weight: 500;
}
.tabs button.active {
color: var(--text);
border-color: var(--accent);
}
.status {
font-family: var(--mono);
font-size: 0.85rem;
}
.status.ok {
color: var(--green);
}
.status.err {
color: #f85149;
}
.hint {
font-size: 0.8rem;
color: var(--muted);
}
code {
font-family: var(--mono);
color: var(--accent);
}
</style>
</head>
<body>
<header>
<strong>⬡ Try-it Console</strong>
<span class="spacer"></span>
<a href="../site/index.html">← Docs home</a>
<a href="./playground.html">Swagger UI</a>
<a href="./changelog.md">API changelog</a>
</header>
<main>
<h1>Interactive API console</h1>
<p class="hint">
Pick an endpoint, fill in parameters, and send a real request against a
running indexer. The auth helper stores your API key once and applies it
to every request and snippet. Nothing leaves your browser except the
request you send.
</p>
<h2>Connection</h2>
<div class="row">
<div>
<label for="baseUrl">Base URL</label>
<input id="baseUrl" value="http://localhost:3001" />
</div>
<div>
<label for="apiKey">API key (optional, sent as X-API-Key)</label>
<input id="apiKey" placeholder="paste once, applies everywhere" />
</div>
</div>
<h2>Request</h2>
<label for="endpoint">Endpoint</label>
<select id="endpoint"></select>
<div class="params" id="params"></div>
<div id="bodyWrap" style="display: none">
<label for="body">Request body (JSON)</label>
<textarea id="body">{}</textarea>
</div>
<button id="send">Send request</button>
<span id="status" class="status"></span>
<h2>Response</h2>
<pre id="response">No request sent yet.</pre>
<h2>Code snippet</h2>
<div class="tabs" id="langTabs">
<button data-lang="curl" class="active">curl</button>
<button data-lang="js">JavaScript</button>
<button data-lang="python">Python</button>
<button data-lang="rust">Rust</button>
</div>
<pre id="snippet"></pre>
<h2>WebSocket console</h2>
<p class="hint">
The indexer pushes live messages of the form
<code>{ "type": "event" | "vault_ratio" | "contract_link", "data": ... }</code>
. Connect to <code>ws(s)://<host>/?api_key=<key></code>.
</p>
<div class="row">
<div>
<label for="wsUrl">WebSocket URL</label>
<input id="wsUrl" value="ws://localhost:3001/" />
</div>
<div style="display: flex; align-items: flex-end; gap: 0.5rem">
<button id="wsConnect" style="margin: 0">Connect</button>
<button id="wsClose" class="secondary" style="margin: 0">
Disconnect
</button>
</div>
</div>
<span id="wsStatus" class="status"></span>
<pre id="wsLog">Not connected.</pre>
</main>
<script>
const $ = (id) => document.getElementById(id);
let spec = null;
let operations = [];
let currentLang = "curl";
async function loadSpec() {
const res = await fetch("./openapi.json");
spec = await res.json();
const base = spec.servers && spec.servers[0] && spec.servers[0].url;
if (base) $("baseUrl").value = base.replace("https://", "http://");
operations = [];
for (const [path, item] of Object.entries(spec.paths || {})) {
for (const [method, op] of Object.entries(item)) {
if (!["get", "post", "put", "delete", "patch"].includes(method))
continue;
operations.push({ path, method, op });
}
}
const sel = $("endpoint");
sel.innerHTML = operations
.map(
(o, i) =>
`<option value="${i}">${o.method.toUpperCase()} ${o.path} — ${(o.op.summary || "").replace(/"/g, "")}</option>`,
)
.join("");
renderParams();
}
function pathParams(path) {
return (path.match(/{(\w+)}/g) || []).map((s) => s.slice(1, -1));
}
function currentOp() {
return operations[Number($("endpoint").value)] || operations[0];
}
function renderParams() {
const o = currentOp();
if (!o) return;
const container = $("params");
container.innerHTML = "";
const params = o.op.parameters || [];
for (const name of pathParams(o.path)) {
if (!params.find((p) => p.name === name))
params.push({ name, in: "path", required: true });
}
for (const p of params) {
const wrap = document.createElement("div");
const req = p.required ? " *" : "";
wrap.innerHTML = `<label>${p.name} <span class="hint">(${p.in}${req})</span></label>`;
const input = document.createElement("input");
input.dataset.name = p.name;
input.dataset.in = p.in;
input.placeholder = (p.schema && p.schema.type) || "";
wrap.appendChild(input);
container.appendChild(wrap);
}
$("bodyWrap").style.display = o.op.requestBody ? "block" : "none";
updateSnippet();
}
function buildUrl() {
const o = currentOp();
let path = o.path;
const query = [];
document.querySelectorAll("#params input").forEach((input) => {
const v = input.value.trim();
if (!v) return;
if (input.dataset.in === "path") {
path = path.replace(`{${input.dataset.name}}`, encodeURIComponent(v));
} else {
query.push(
`${encodeURIComponent(input.dataset.name)}=${encodeURIComponent(v)}`,
);
}
});
const base = $("baseUrl").value.replace(/\/$/, "");
return base + path + (query.length ? "?" + query.join("&") : "");
}
function requestInit() {
const o = currentOp();
const headers = {};
const key = $("apiKey").value.trim();
if (key) headers["X-API-Key"] = key;
const init = { method: o.method.toUpperCase(), headers };
if (o.op.requestBody) {
headers["Content-Type"] = "application/json";
init.body = $("body").value;
}
return init;
}
async function send() {
const url = buildUrl();
const init = requestInit();
$("status").textContent = "Sending…";
$("status").className = "status";
try {
const res = await fetch(url, init);
const text = await res.text();
let pretty = text;
try {
pretty = JSON.stringify(JSON.parse(text), null, 2);
} catch (_) {}
$("response").textContent = pretty || "(empty body)";
$("status").textContent = `${res.status} ${res.statusText}`;
$("status").className = "status " + (res.ok ? "ok" : "err");
} catch (err) {
$("response").textContent = String(err);
$("status").textContent = "Network error (is the indexer running?)";
$("status").className = "status err";
}
}
function snippet(lang) {
const url = buildUrl();
const init = requestInit();
const method = init.method;
const key = $("apiKey").value.trim();
const body = init.body;
if (lang === "curl") {
let s = `curl -X ${method} "${url}"`;
if (key) s += ` \\\n -H "X-API-Key: ${key}"`;
if (body) s += ` \\\n -H "Content-Type: application/json" \\\n -d '${body}'`;
return s;
}
if (lang === "js") {
const opts = { method, headers: init.headers };
if (body) opts.body = body;
return `const res = await fetch("${url}", ${JSON.stringify(opts, null, 2)});\nconst data = await res.json();\nconsole.log(data);`;
}
if (lang === "python") {
let s = `import requests\n\nres = requests.${method.toLowerCase()}(\n "${url}",`;
if (key) s += `\n headers={"X-API-Key": "${key}"},`;
if (body) s += `\n json=${body},`;
s += `\n)\nprint(res.json())`;
return s;
}
if (lang === "rust") {
let s = `// Cargo.toml: reqwest = { version = "0.12", features = ["json"] }, tokio = { version = "1", features = ["full"] }\nlet client = reqwest::Client::new();\nlet res = client\n .${method.toLowerCase()}("${url}")`;
if (key) s += `\n .header("X-API-Key", "${key}")`;
if (body) s += `\n .body(r#"${body}"#)`;
s += `\n .send()\n .await?;\nprintln!("{}", res.text().await?);`;
return s;
}
return "";
}
function updateSnippet() {
$("snippet").textContent = snippet(currentLang);
}
// WebSocket console
let socket = null;
function wsLog(line) {
const el = $("wsLog");
if (el.textContent === "Not connected.") el.textContent = "";
el.textContent += line + "\n";
el.scrollTop = el.scrollHeight;
}
function wsConnect() {
const base = $("wsUrl").value.trim();
const key = $("apiKey").value.trim();
const url = base + (key ? `?api_key=${encodeURIComponent(key)}` : "");
try {
socket = new WebSocket(url);
} catch (err) {
$("wsStatus").textContent = String(err);
$("wsStatus").className = "status err";
return;
}
socket.onopen = () => {
$("wsStatus").textContent = "Connected";
$("wsStatus").className = "status ok";
wsLog("[open] connected to " + base);
};
socket.onmessage = (e) => wsLog("[message] " + e.data);
socket.onerror = () => {
$("wsStatus").textContent = "Socket error";
$("wsStatus").className = "status err";
};
socket.onclose = (e) => {
$("wsStatus").textContent = `Closed (${e.code})`;
$("wsStatus").className = "status";
wsLog("[close] " + e.code);
};
}
function wsClose() {
if (socket) socket.close();
}
document.addEventListener("DOMContentLoaded", () => {
loadSpec().catch((err) => {
$("response").textContent =
"Could not load openapi.json: " + err.message;
});
$("endpoint").addEventListener("change", renderParams);
$("params").addEventListener("input", updateSnippet);
$("baseUrl").addEventListener("input", updateSnippet);
$("apiKey").addEventListener("input", updateSnippet);
$("body").addEventListener("input", updateSnippet);
$("send").addEventListener("click", send);
$("langTabs").addEventListener("click", (e) => {
if (e.target.dataset.lang) {
currentLang = e.target.dataset.lang;
document
.querySelectorAll("#langTabs button")
.forEach((b) => b.classList.toggle("active", b === e.target));
updateSnippet();
}
});
$("wsConnect").addEventListener("click", wsConnect);
$("wsClose").addEventListener("click", wsClose);
});
</script>
</body>
</html>