on master help sign in
http-api guide: fix codeblock formatting yet again
commitefc854bbd47c2e3895d41ed0c0a3eb177df081b8 authorTinnus Napbus <admin@tinnus-napbus.xyz> dateFri 4 Mar 2022, 23:21 (5 years ago) parentc819993
1 file changed+188−84
content/docs/userspace/http-api-guide.md+188−84
content/docs/userspace/http-api-guide.md+188−84
@@ -189,7 +189,7 @@ window.ship = "zod";
189189`"zod"` will be replaced with the actual name of the ship in question. You can
190190include this file like:
191191
192−```html
192+```
193193&lt;script src="/session.js">&lt;/script>
194194```
195195
@@ -208,8 +208,8 @@ through its channel system.
208208
209209The `Urbit` object will generate a random channel ID that looks like
210210`1646295453-e1bdfd`, and use a path of `/~/channel/1646295453-e1bdfd` for
211−channel communications. Pokes and subscription requests will be sent to that
212−channel. Responses and subscription updates will be sent out to you on that
211+channel communications. Pokes will and subscription requests will be sent to
212+that channel. Responses and subscription updates will be sent out to you on that
213213channel too.
214214
215215Eyre sends out updates and responses on an [SSE] (Server Sent Event) stream for
@@ -274,14 +274,26 @@ question):
274274Here's a simple example that will run `authenticate` for a fakezod when the
275275_Connect_ button is pressed:
276276
277−```html
278−&lt;html> &lt;head> &lt;script
279−src="https://unpkg.com/@urbit/http-api">&lt;/script> &lt;/head> &lt;body>
280−&lt;button id="start" type="button" onClick="connect()" >Connect&lt;/button>
281−&lt;/body> &lt;script> async function connect() { window.api = await
282−UrbitHttpApi.Urbit.authenticate({ ship: "zod", url: "localhost:8080", code:
283−"lidlut-tabwed-pillex-ridrup", verbose: true }); document.body.innerHTML =
284−"Connected!"; }; &lt;/script> &lt;/html>
277+```
278+&lt;html>
279+ &lt;head>
280+ &lt;script src="https://unpkg.com/@urbit/http-api">&lt;/script>
281+ &lt;/head>
282+ &lt;body>
283+ &lt;button id="start" type="button" onClick="connect()" >Connect&lt;/button>
284+ &lt;/body>
285+ &lt;script>
286+ async function connect() {
287+ window.api = await UrbitHttpApi.Urbit.authenticate({
288+ ship: "zod",
289+ url: "localhost:8080",
290+ code: "lidlut-tabwed-pillex-ridrup",
291+ verbose: true
292+ });
293+ document.body.innerHTML = "Connected!";
294+ };
295+ &lt;/script>
296+&lt;/html>
285297```
286298
287299`window.api` can then be used for further communications.
@@ -312,22 +324,45 @@ mark. The `json` argument just contains the message in a string. If for some
312324reason the `hood` agent rejects the poke, "Poke failed!" will be displayed on
313325the page.
314326
315−```html
316−&lt;html> &lt;head> &lt;script
317−src="https://unpkg.com/@urbit/http-api">&lt;/script> &lt;script
318−src="/session.js">&lt;/script> &lt;/head> &lt;body> &lt;input id="msg"
319−type="text" placeholder="Message for ship" /> &lt;button id="submit"
320−type="button" onClick="doPoke()" >Submit&lt;/button> &lt;p id="err">&lt;/p>
321−&lt;/body> &lt;script> document.getElementById("msg") .addEventListener("keyup",
322−function(event) { if (event.keyCode === 13) {
323−document.getElementById("submit").click(); } }) const api = new
324−UrbitHttpApi.Urbit(""); api.ship = window.ship; function doPoke() { const msg =
325−document.getElementById("msg").value; api.poke({ app: "hood", mark: "helm-hi",
326−json: msg, onSuccess: success, onError: error }); } function success() {
327−document.getElementById("msg").value = "";
328−document.getElementById("err").innerHTML = ""; } function error() {
329−document.getElementById("msg").value = "";
330−document.getElementById("err").innerHTML = "Poke failed!"; } &lt;/script>
327+```
328+&lt;html>
329+ &lt;head>
330+ &lt;script src="https://unpkg.com/@urbit/http-api">&lt;/script>
331+ &lt;script src="/session.js">&lt;/script>
332+ &lt;/head>
333+ &lt;body>
334+ &lt;input id="msg" type="text" placeholder="Message for ship" />
335+ &lt;button id="submit" type="button" onClick="doPoke()" >Submit&lt;/button>
336+ &lt;p id="err">&lt;/p>
337+ &lt;/body>
338+ &lt;script>
339+ document.getElementById("msg")
340+ .addEventListener("keyup", function(event) {
341+ if (event.keyCode === 13) {
342+ document.getElementById("submit").click();
343+ }
344+ })
345+ const api = new UrbitHttpApi.Urbit("");
346+ api.ship = window.ship;
347+ function doPoke() {
348+ const msg = document.getElementById("msg").value;
349+ api.poke({
350+ app: "hood",
351+ mark: "helm-hi",
352+ json: msg,
353+ onSuccess: success,
354+ onError: error
355+ });
356+ }
357+ function success() {
358+ document.getElementById("msg").value = "";
359+ document.getElementById("err").innerHTML = "";
360+ }
361+ function error() {
362+ document.getElementById("msg").value = "";
363+ document.getElementById("err").innerHTML = "Poke failed!";
364+ }
365+ &lt;/script>
331366&lt;/html>
332367```
333368
@@ -365,22 +400,47 @@ This example will, upon clicking the "Subscribe" button, subscribe to the
365400update it receives. You can test it by doing something like posting a message in
366401a chat. You can also unsubscribe by clicking the "Unsubscribe" button.
367402
368−```html
369−&lt;html> &lt;head> &lt;script
370−src="https://unpkg.com/@urbit/http-api">&lt;/script> &lt;script
371−src="/session.js">&lt;/script> &lt;/head> &lt;body> &lt;button id="toggle"
372−type="button" onClick="doSub()" >Subscribe&lt;/button> &lt;pre
373−id="event">&lt;/pre> &lt;/body> &lt;script> const api = new
374−UrbitHttpApi.Urbit(""); api.ship = window.ship; const toggle =
375−document.getElementById("toggle"); const event =
376−document.getElementById("event"); function doSub() { window.id = api.subscribe({
377−app: "graph-store", path: "/updates", event: printEvent, quit: doSub, err:
378−subFail }); toggle.innerHTML = "Unsubscribe"; toggle.onclick = doUnsub;
379−event.innerHTML = "Awaiting event"; }; function doUnsub() {
380−api.unsubscribe(window.id); toggle.innerHTML = "Subscribe"; toggle.onclick =
381−doSub; event.innerHTML = "Subscription closed"; }; function printEvent(update) {
382−event.innerHTML = JSON.stringify(update, null, 2); }; function subFail() {
383−event.innerHTML = "Subscription failed!"; }; &lt;/script> &lt;/html>
403+```
404+&lt;html>
405+ &lt;head>
406+ &lt;script src="https://unpkg.com/@urbit/http-api">&lt;/script>
407+ &lt;script src="/session.js">&lt;/script>
408+ &lt;/head>
409+ &lt;body>
410+ &lt;button id="toggle" type="button" onClick="doSub()" >Subscribe&lt;/button>
411+ &lt;pre id="event">&lt;/pre>
412+ &lt;/body>
413+ &lt;script>
414+ const api = new UrbitHttpApi.Urbit("");
415+ api.ship = window.ship;
416+ const toggle = document.getElementById("toggle");
417+ const event = document.getElementById("event");
418+ function doSub() {
419+ window.id = api.subscribe({
420+ app: "graph-store",
421+ path: "/updates",
422+ event: printEvent,
423+ quit: doSub,
424+ err: subFail
425+ });
426+ toggle.innerHTML = "Unsubscribe";
427+ toggle.onclick = doUnsub;
428+ event.innerHTML = "Awaiting event";
429+ };
430+ function doUnsub() {
431+ api.unsubscribe(window.id);
432+ toggle.innerHTML = "Subscribe";
433+ toggle.onclick = doSub;
434+ event.innerHTML = "Subscription closed";
435+ };
436+ function printEvent(update) {
437+ event.innerHTML = JSON.stringify(update, null, 2);
438+ };
439+ function subFail() {
440+ event.innerHTML = "Subscription failed!";
441+ };
442+ &lt;/script>
443+&lt;/html>
384444```
385445
386446### Subscribe Once
@@ -417,17 +477,33 @@ printing every `graph-store` update it receives, it will instead just print the
417477first one it receives and close the subscription. Additionally, it sets a five
418478second timeout, and prints an error message if it times out.
419479
420−```html
421−&lt;html> &lt;head> &lt;script
422−src="https://unpkg.com/@urbit/http-api">&lt;/script> &lt;script
423−src="/session.js">&lt;/script> &lt;/head> &lt;body> &lt;button type="button"
424−onClick="doSub()" >Subscribe Once&lt;/button> &lt;pre id="event">&lt;/pre>
425−&lt;/body> &lt;script> const api = new UrbitHttpApi.Urbit(""); api.ship =
426−window.ship; const event = document.getElementById("event"); function doSub() {
427−event.innerHTML = ""; api.subscribeOnce("graph-store","/updates",5000)
428−.then(printEvent, noEvent); }; function printEvent(update) { event.innerHTML =
429−JSON.stringify(update, null, 2); }; function noEvent(error) { event.innerHTML =
430−error; }; &lt;/script> &lt;/html>
480+```
481+&lt;html>
482+ &lt;head>
483+ &lt;script src="https://unpkg.com/@urbit/http-api">&lt;/script>
484+ &lt;script src="/session.js">&lt;/script>
485+ &lt;/head>
486+ &lt;body>
487+ &lt;button type="button" onClick="doSub()" >Subscribe Once&lt;/button>
488+ &lt;pre id="event">&lt;/pre>
489+ &lt;/body>
490+ &lt;script>
491+ const api = new UrbitHttpApi.Urbit("");
492+ api.ship = window.ship;
493+ const event = document.getElementById("event");
494+ function doSub() {
495+ event.innerHTML = "";
496+ api.subscribeOnce("graph-store","/updates",5000)
497+ .then(printEvent, noEvent);
498+ };
499+ function printEvent(update) {
500+ event.innerHTML = JSON.stringify(update, null, 2);
501+ };
502+ function noEvent(error) {
503+ event.innerHTML = error;
504+ };
505+ &lt;/script>
506+&lt;/html>
431507```
432508
433509### Scries
@@ -449,16 +525,26 @@ Upon pressing the "Scry Graphs" button, this example will scry the `graph-store`
449525agent's `/keys` endpoint for the list of graphs, and print the resulting JSON
450526data.
451527
452−```html
453−&lt;html> &lt;head> &lt;script
454−src="https://unpkg.com/@urbit/http-api">&lt;/script> &lt;script
455−src="/session.js">&lt;/script> &lt;/head> &lt;body> &lt;button id="scry"
456−type="button" onClick="doScry()" >Scry Graphs&lt;/button> &lt;pre
457−id="result">&lt;/pre> &lt;/body> &lt;script> const api = new
458−UrbitHttpApi.Urbit(""); api.ship = window.ship; async function doScry() { var
459−groups = await api.scry({app: "graph-store", path: "/keys"});
460−document.getElementById("result") .innerHTML = JSON.stringify(groups, null, 2);
461−} &lt;/script> &lt;/html>
528+```
529+&lt;html>
530+ &lt;head>
531+ &lt;script src="https://unpkg.com/@urbit/http-api">&lt;/script>
532+ &lt;script src="/session.js">&lt;/script>
533+ &lt;/head>
534+ &lt;body>
535+ &lt;button id="scry" type="button" onClick="doScry()" >Scry Graphs&lt;/button>
536+ &lt;pre id="result">&lt;/pre>
537+ &lt;/body>
538+ &lt;script>
539+ const api = new UrbitHttpApi.Urbit("");
540+ api.ship = window.ship;
541+ async function doScry() {
542+ var groups = await api.scry({app: "graph-store", path: "/keys"});
543+ document.getElementById("result")
544+ .innerHTML = JSON.stringify(groups, null, 2);
545+ }
546+ &lt;/script>
547+&lt;/html>
462548```
463549
464550### Thread
@@ -482,23 +568,44 @@ JSON result of the thread.
482568This example takes a hoon expression (such as `(add 1 1)`), evalutes it with the
483569`graph-eval` thread in the `landscape` desk, and prints the result.
484570
485−```html
486−&lt;html> &lt;head> &lt;script
487−src="https://unpkg.com/@urbit/http-api">&lt;/script> &lt;script
488−src="/session.js">&lt;/script> &lt;/head> &lt;body> &lt;input id="hoon"
489−type="text" placeholder="Hoon to evaluate" /> &lt;button id="submit"
490−type="button" onClick="runThread()" >Submit&lt;/button> &lt;pre
491−id="expr">&lt;/pre> &lt;pre id="result">&lt;/pre> &lt;/body> &lt;script>
492−document.getElementById("hoon") .addEventListener("keyup", function(event) { if
493−(event.keyCode === 13) { document.getElementById("submit").click(); } }); const
494−hoon = document.getElementById("hoon"); const expr =
495−document.getElementById("expr"); const result =
496−document.getElementById("result"); const desk = "landscape"; const api = new
497−UrbitHttpApi.Urbit("", "", desk); api.ship = window.ship; async function
498−runThread() { const threadResult = await api.thread({ inputMark:
499−"graph-view-action", outputMark: "tang", threadName: "graph-eval", body: {eval:
500−hoon.value} }); result.innerHTML = threadResult[0].join("\n"); expr.innerHTML =
501−hoon.value; hoon.value = ""; }; &lt;/script> &lt;/html>
571+```
572+&lt;html>
573+ &lt;head>
574+ &lt;script src="https://unpkg.com/@urbit/http-api">&lt;/script>
575+ &lt;script src="/session.js">&lt;/script>
576+ &lt;/head>
577+ &lt;body>
578+ &lt;input id="hoon" type="text" placeholder="Hoon to evaluate" />
579+ &lt;button id="submit" type="button" onClick="runThread()" >Submit&lt;/button>
580+ &lt;pre id="expr">&lt;/pre>
581+ &lt;pre id="result">&lt;/pre>
582+ &lt;/body>
583+ &lt;script>
584+ document.getElementById("hoon")
585+ .addEventListener("keyup", function(event) {
586+ if (event.keyCode === 13) {
587+ document.getElementById("submit").click();
588+ }
589+ });
590+ const hoon = document.getElementById("hoon");
591+ const expr = document.getElementById("expr");
592+ const result = document.getElementById("result");
593+ const desk = "landscape";
594+ const api = new UrbitHttpApi.Urbit("", "", desk);
595+ api.ship = window.ship;
596+ async function runThread() {
597+ const threadResult = await api.thread({
598+ inputMark: "graph-view-action",
599+ outputMark: "tang",
600+ threadName: "graph-eval",
601+ body: {eval: hoon.value}
602+ });
603+ result.innerHTML = threadResult[0].join("\n");
604+ expr.innerHTML = hoon.value;
605+ hoon.value = "";
606+ };
607+ &lt;/script>
608+&lt;/html>
502609```
503610
504611### Delete Channel
@@ -526,13 +633,10 @@ discarded and a fresh channel ID will be generated.
526633- [Eyre Guide][eyre-guide] - Lower-level examples of using Eyre's external API
527634 with `curl`.
528635- [`http-api` on Github][http-api-src] - The source code for `@urbit/http-api`.
529−- [Software Distribution][dist] - This section of documentation explains how to
530− distribute apps in Urbit, including how to package and serve front-end files.
531636
532637[eyre-ext-ref]: /docs/arvo/eyre/external-api-ref
533638[eyre-guide]: /docs/arvo/eyre/guide
534639[http-api-src]: https://github.com/urbit/urbit/tree/master/pkg/npm/http-api
535−[dist]: /docs/userspace/dist/dist
536640[eyre]: /docs/glossary/eyre
537641[vane]: /docs/glossary/vane
538642[arvo]: /docs/glossary/arvo