-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1296 lines (1142 loc) · 50.6 KB
/
index.html
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2021-06-22 Tue 10:54 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Literate API Development</title>
<meta name="author" content="Kyle S Passarelli" />
<meta name="generator" content="Org Mode" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: auto;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline; margin-top: 14px;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/readtheorg_theme/css/htmlize.css"/>
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/readtheorg_theme/css/readtheorg.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/lib/js/jquery.stickytableheaders.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/readtheorg_theme/js/readtheorg.js"></script>
<script type="text/javascript">
// @license magnet:?xt=urn:btih:e95b018ef3580986a04669f1b5879592219e2a7a&dn=public-domain.txt Public Domain
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.classList.add("code-highlighted");
target.classList.add("code-highlighted");
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.classList.remove("code-highlighted");
target.classList.remove("code-highlighted");
}
}
/*]]>*///-->
// @license-end
</script>
</head>
<body>
<div id="content">
<h1 class="title">Literate API Development</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org55a80f9">1. cURL: the c is for crying</a></li>
<li><a href="#org973c861">2. Org-mode</a></li>
<li><a href="#org53d73b5">3. Restclient</a></li>
<li><a href="#org07588c9">4. Caching   <span class="tag"><span class="ATTACH">ATTACH</span></span></a>
<ul>
<li><a href="#org27c72ce">4.1. Org mode hackery</a></li>
</ul>
</li>
<li><a href="#orgf98d644">5. Multi-step forms</a></li>
<li><a href="#org92c87dd">6. Export</a></li>
<li><a href="#org3d637d6">7. Next steps</a></li>
<li><a href="#org455f1f5">8. Conclusions</a></li>
<li><a href="#org43ffb18">9. References</a></li>
<li><a href="#org2dd7083">10. Notes</a>
<ul>
<li>
<ul>
<li><a href="#org022973b">10.0.1. Chomp function</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<p>
There are a lot of fascinating REST APIs there.
</p>
<ul class="org-ul">
<li>TODO: Name a few very weird and interesting APIs.)</li>
</ul>
<p>
Combining APIs is a great way to build a product, and maybe even start a
business. Want to connect your window blinds to the weather.com API? Sure. Want
to do something else with some other thing? Go for it.
</p>
<p>
Thanks to technologies like OAuth and OpenID Connect, it’s easier than ever to
turn that toy into a robust and secure product than ever and also to share the
same client code.
</p>
<p>
In a previous life, my team and I used to be really into Postman for the initial
steps of writing applications. It was good for firing off a few requests and
getting a feel for how an API worked. Over time, Postman collections found their
way into VC repositories, zip files in shared drives, Slack… kind of
everywhere. We were happy customers.
</p>
<p>
However, like many applications over the last few years, Postman went through a
period of excitement over Electron and ballooned into a whopping 800+ MB
mountain of Javascript. It would hang in mysterious ways like when selecting an
environment, for up to 30 seconds at a time. (This probably had something to do
with my team abusing it with a common account and dozens of environments, but
still.) It felt like a flow destroying laptop warmer.
</p>
<p>
So we went back to cURL. It’s amazingly portable. It’s usually a one-liner, so
it’s possible to copy & paste a command and have it work. Postman exports cURL
commands directly from the UI, we stored those in a file and shared the file.
</p>
<p>
Things start to break in particular ways with cURL, though. Consider these
contrived but common scenarios:
</p>
<ol class="org-ol">
<li>Multi-step flows, where the response from one API goes into a second request.</li>
<li>When you are POSTing large payloads to JSON APIs.</li>
<li>When you, erm, have to remember what you’ve done before.</li>
</ol>
<p>
I’ll share my current worklow based around Emacs Org mode and a package called
Restclient. I’m assuming you have little or no Emacs knowledge, and probably
never heard of Org mode.
</p>
<p>
It’s happily replaced both Postman and cURL in the majority of scenarios. I hope
to leave you with an idea about how it fits together as a simple effetive system
for hacking away at APIs right in a text editor with minimal division between
code and prose. You could call it “literate API development”.
</p>
<div id="outline-container-org55a80f9" class="outline-2">
<h2 id="org55a80f9"><span class="section-number-2">1</span> cURL: the c is for crying</h2>
<div class="outline-text-2" id="text-1">
<p>
We mentioned OAuth earlier. To do anything useful with an OAuth API, you need to
log on and then send a request:
</p>
<p>
Step 1: Authenticate and get a bearer token.
Step 2: Send that bearer token in a header and get a resource.
</p>
<p>
This is how it might work in the terminal. First, you send a cURL command with
a username and password,
</p>
<div class="org-src-container">
<pre class="src src-shell"><span style="color: #ECBE7B;">curl</span> -u <span style="color: #98be65;">"someapplication:password"</span> <span style="color: #98be65;">\</span>
-X GET <span style="color: #98be65;">"http://httpbin.org/basic-auth/someapplication/password"</span> <span style="color: #98be65;">\</span>
-H <span style="color: #98be65;">"accept: application/json"</span>
</pre>
</div>
<p>
which returns a token
</p>
<div class="org-src-container">
<pre class="src src-json">{
<span style="color: #51afef;">"token"</span> : "<span style="color: #da8548; font-weight: bold;">123</span>"
}
</pre>
</div>
<p>
In the terminal, you copy this token and put it in a variable:
</p>
<div class="org-src-container">
<pre class="src src-shell"><span style="color: #dcaeea;">TOKEN</span>=<span style="color: #da8548; font-weight: bold;">123</span>
</pre>
</div>
<p>
Then you use the token in each authenticated call:
</p>
<div class="org-src-container">
<pre class="src src-shell"><span style="color: #ECBE7B;">curl</span> -i <span style="color: #98be65;">\</span>
-H <span style="color: #98be65;">"Authorization: Bearer </span><span style="color: #a9a1e1;">$</span><span style="color: #dcaeea;">TOKEN</span><span style="color: #98be65;">"</span> <span style="color: #98be65;">\</span>
-H <span style="color: #98be65;">"accept: application/json"</span> <span style="color: #98be65;">\</span>
-XGET https://httpbin.org/bearer
</pre>
</div>
<p>
<b><b>Does it work? yes</b></b>. It is still fairly easy to share, since everybody has a
shell. You just have to send a few cURL commands. Except they’re run in sequence
and there’s that env var in the middle, so maybe you share a shell script. (You
should start getting a sinking feeling right about now, wondering how you get to
be a professional programmer but are exchanging shell scripts.)
</p>
<p>
Oh and how about this one: the most interesting requests are POST or PUT. You
will likely end up with two little steps for every one of these calls:
</p>
<p>
For the request body, you will want to create a small file called
<code>request.json</code>. Then put your POST body in it:
</p>
<div class="org-src-container">
<pre class="src src-json">{
<span style="color: #51afef;">"jql"</span>: <span style="color: #98be65;">"project = HSP"</span>,
<span style="color: #51afef;">"startAt"</span>: <span style="color: #da8548; font-weight: bold;">0</span>,
<span style="color: #51afef;">"maxResults"</span>: <span style="color: #da8548; font-weight: bold;">15</span>,
<span style="color: #51afef;">"fields"</span>: [
<span style="color: #98be65;">"summary"</span>,
<span style="color: #98be65;">"status"</span>,
<span style="color: #98be65;">"assignee"</span>
]
}
</pre>
</div>
<p>
Second, youw will craft a cURL call to send that off as a request body with <code>@</code>:
</p>
<div class="org-src-container">
<pre class="src src-shell"><span style="color: #ECBE7B;">curl</span> -i -H <span style="color: #98be65;">"Content-Type: application/json"</span> -XPOST http://httpbin.org/post <span style="color: #98be65;">\</span>
-d <span style="color: #98be65;">"@request.json"</span>
</pre>
</div>
<p>
Now you may have three things to keep track of: a token, a small JSON
snippet, and a command. Not so convenient now, is it?
</p>
<p>
Both of these can be solved with Postman, which has scripting capabilities and
sharing. Pretty soon you’re gonna want to version your code, though; also your
code is tied to a proprietary client.
</p>
<p>
Now it’s obvious we need to go even older than cURL. To Emacs and emacs lisp. An
elegant weapon from a more civilized time.
</p>
<p>
In this article, we will see how using Emacs + Jq as an embedded REST client
lets you
</p>
<ul class="org-ul">
<li>Play around with REST APIs</li>
<li>Use Jq to explore the shape of data online and offline</li>
<li>Export client code to use for further exploration.</li>
</ul>
<p>
A couple of modifications allow you to log into APIs which give you a token, by
storing this information directly in Emacs Org mode. In this way, your programming
interface becomes your scripting. The scripting itself kind of dissolves into
the document.
</p>
<p>
Other alternatives exist. For VS Code, a similar alternative is X. I still use
VS Code from time to time as a debugger. For simpler text processing, though, I
prefer Emacs.
</p>
</div>
</div>
<div id="outline-container-org973c861" class="outline-2">
<h2 id="org973c861"><span class="section-number-2">2</span> Org-mode</h2>
<div class="outline-text-2" id="text-2">
<p>
Next to Donald Knuth, Dominic Carsten should go down in history as one of the
greatest yak-shavers of all time. Starting as a note taker, Org mode is used for
agendas and GTD systems, writing Ph.D theses, and publishing blogs like this
one.
</p>
<p>
Superficially, Org mode is an outline system alla Markdown:
</p>
<pre class="example" id="orgbb2a927">
* Heading 1
Blah blah
** Heading 2
Bleh bleh
</pre>
<p>
You have markup for font styles like bold and italic, formatted code snippets,
and can export to HTML. So far a lot like Markdown.
</p>
<p>
The differences start showing up editing code blocks. In Markdown, syntax for
code blocks is fairly basic. Some Markdown additions exist which take it a lot
futher, such as [a] and [b], but they have more limited use. A source code block
looks like this:
</p>
<pre class="example" id="org78b147d">
#+begin_src {{language}}
{{code}}
#+end_src
</pre>
<p>
It is one of the more viable solutions for doing literate programming, where
code exists inside prose, rather than prose being inside the code as comments.
</p>
<p>
Here’s a short snippet that shows how it works:
</p>
<pre class="example" id="org79bcd4f">
#+NAME: hi
#+BEGIN_SRC sh
echo "hi"
#+END_SRC
#+BEGIN_SRC sh :stdin hi
cowsay
#+END_SRC
</pre>
<p>
We have two source code blocks, and one is getting the otuput from the other.
They are both running shell scripts. When we run them, we see this:
</p>
<div class="org-src-container">
<pre class="src src-sh" id="org20d669c"><span style="color: #ECBE7B;">echo</span> <span style="color: #98be65;">"hi"</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-sh">cowsay
</pre>
</div>
<pre class="example">
____
< hi >
----
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
</pre>
<p>
So it’s a little like Markdown, but it’s also like Jupyter. I execute a “cell”
by executing a source block with a keystroke, and that outputs a results section
which is not meant to be edited by hand, since it’s regenerated every time.
</p>
<p>
There’s a lot more to org-mode, but we have enough for the curious to start I
hope.
</p>
</div>
</div>
<div id="outline-container-org53d73b5" class="outline-2">
<h2 id="org53d73b5"><span class="section-number-2">3</span> Restclient</h2>
<div class="outline-text-2" id="text-3">
<p>
The same cURL call that took 2 steps, in restclient [link] for Emacs, looks like this:
</p>
<div class="org-src-container">
<pre class="src src-restclient" id="org98040db"><span style="color: #51afef;">POST</span> <span style="color: #c678dd;">http://httpbin.org/post</span>
<span style="color: #dcaeea;">Content-Type</span>: <span style="color: #98be65;">application/json</span>
{
<span style="color: #98be65;">"jql"</span>: <span style="color: #98be65;">"project = HSP"</span>,
<span style="color: #98be65;">"startAt"</span>: 0,
<span style="color: #98be65;">"maxResults"</span>: 15,
<span style="color: #98be65;">"fields"</span>: [
<span style="color: #98be65;">"summary"</span>,
<span style="color: #98be65;">"status"</span>,
<span style="color: #98be65;">"assignee"</span>
]
}
</pre>
</div>
<p>
That’s the first source code block. REST client uses a shortened description of
the call that turns out to be an actual RFC [link]. In this printed version
(which was just generated from the Emacs source) you won’t see the header line
demarcating the source block. It identifies the block as being of “restclient”
type:
</p>
<pre class="example" id="org204ceb6">
#+begin_src restclient :results value
...
</pre>
<p>
Following the org example above, we will introduce a second code block, which
reads the output of the first, and manipulates it: (Similar to where we had
<code>cowsay</code> in the previous example.)
</p>
<div class="org-src-container">
<pre class="src src-sh">jq <span style="color: #98be65;">'.'</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-json">{
<span style="color: #51afef;">"args"</span>: {},
<span style="color: #51afef;">"data"</span>: "{\n \"jql\": \"project = HSP\",\n \"startAt\": <span style="color: #da8548; font-weight: bold;">0</span>,\n \"maxResults\": <span style="color: #da8548; font-weight: bold;">15</span>,\n \"fields\": [\n \"summary\",\n \"status\",\n \"assignee\"\n ]\n}",
<span style="color: #51afef;">"files"</span>: {},
<span style="color: #51afef;">"form"</span>: {},
<span style="color: #51afef;">"headers"</span>: {
<span style="color: #51afef;">"Accept"</span>: <span style="color: #98be65;">"*/*"</span>,
<span style="color: #51afef;">"Accept-Encoding"</span>: <span style="color: #98be65;">"gzip"</span>,
<span style="color: #51afef;">"Content-Length"</span>: "<span style="color: #da8548; font-weight: bold;">149</span>",
<span style="color: #51afef;">"Content-Type"</span>: <span style="color: #98be65;">"application/json"</span>,
<span style="color: #51afef;">"Extension"</span>: <span style="color: #98be65;">"Security/Digest Security/SSL"</span>,
<span style="color: #51afef;">"Host"</span>: <span style="color: #98be65;">"httpbin.org"</span>,
<span style="color: #51afef;">"Mime-Version"</span>: "<span style="color: #da8548; font-weight: bold;">1.0</span>",
<span style="color: #51afef;">"X-Amzn-Trace-Id"</span>: "Root=<span style="color: #da8548; font-weight: bold;">1</span>-<span style="color: #a9a1e1;">60</span>d<span style="color: #a9a1e1;">207</span>a<span style="color: #a9a1e1;">8</span>-<span style="color: #a9a1e1;">49255</span>b<span style="color: #a9a1e1;">73754</span>efc<span style="color: #a9a1e1;">51271</span>d<span style="color: #a9a1e1;">8537</span>"
},
<span style="color: #51afef;">"json"</span>: {
<span style="color: #51afef;">"fields"</span>: [
<span style="color: #98be65;">"summary"</span>,
<span style="color: #98be65;">"status"</span>,
<span style="color: #98be65;">"assignee"</span>
],
<span style="color: #51afef;">"jql"</span>: <span style="color: #98be65;">"project = HSP"</span>,
<span style="color: #51afef;">"maxResults"</span>: <span style="color: #da8548; font-weight: bold;">15</span>,
<span style="color: #51afef;">"startAt"</span>: <span style="color: #da8548; font-weight: bold;">0</span>
},
<span style="color: #51afef;">"origin"</span>: "<span style="color: #da8548; font-weight: bold;">190.140</span>.<span style="color: #da8548; font-weight: bold;">132.62</span>",
<span style="color: #51afef;">"url"</span>: <span style="color: #98be65;">"http://httpbin.org/post"</span>
}
</pre>
</div>
<p>
This is <code>jq</code>, in this example just printing the output. Now let’s write another
<code>jq</code> block, this time extracting the contents of the <code>data</code> entry, which
containins the original payload:
</p>
<div class="org-src-container">
<pre class="src src-sh" id="orgfa39493">jq -r .data
</pre>
</div>
<div class="org-src-container">
<pre class="src src-json">{
<span style="color: #51afef;">"jql"</span>: <span style="color: #98be65;">"project = HSP"</span>,
<span style="color: #51afef;">"startAt"</span>: <span style="color: #da8548; font-weight: bold;">0</span>,
<span style="color: #51afef;">"maxResults"</span>: <span style="color: #da8548; font-weight: bold;">15</span>,
<span style="color: #51afef;">"fields"</span>: [
<span style="color: #98be65;">"summary"</span>,
<span style="color: #98be65;">"status"</span>,
<span style="color: #98be65;">"assignee"</span>
]
}
</pre>
</div>
<p>
The payload has been unescaped by JQ. Pretty neat. Something that JQ doesn’t do?
No problem, let’s pipe that through a short Python function.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #51afef;">import</span> json
<span style="color: #dcaeea;">data</span> = json.loads(jsondoc)
<span style="color: #51afef;">print</span>(json.dumps(data, indent=<span style="color: #da8548; font-weight: bold;">4</span>))
</pre>
</div>
<div class="org-src-container">
<pre class="src src-json">{
<span style="color: #51afef;">"jql"</span>: <span style="color: #98be65;">"project = HSP"</span>,
<span style="color: #51afef;">"startAt"</span>: <span style="color: #da8548; font-weight: bold;">0</span>,
<span style="color: #51afef;">"maxResults"</span>: <span style="color: #da8548; font-weight: bold;">15</span>,
<span style="color: #51afef;">"fields"</span>: [
<span style="color: #98be65;">"summary"</span>,
<span style="color: #98be65;">"status"</span>,
<span style="color: #98be65;">"assignee"</span>
]
}
</pre>
</div>
<p>
It may not be obvious reading this how automated this actually is. The code
snippets and . None of the output blocks have been manually edited, they can all
be regenerated from the input, like in a Jupyter notebook. One difference
between this format and the iPython format is that there isn’t really a content
vs. container format. It’s all in a big text file with some markup that still
looks OK when you read it in plain text.
</p>
<p>
If you were to load it in Org mode, you would see nice syntax highting for the
code in the blocks, and be able to jump into each snippet in a separate window
to edit with indentation, linting, and documentation.
</p>
<p>
The whole thing is completely tied into Emacs: the way it looks, the way it
evaluates, etc. There are some Org mode implementations for other editors like
VI and Visual Studio Code, but they typically handle a fraction of the API,
which is quite extensive. There’s no context switch between the editor and the
document, or between the document the embeddd code snippets. If you invest time
to get it to work, its rewards are many.
</p>
<p>
If you are interested in looking at the soure of this article to see how the
entire thing was laid out, click here. Turns out github understands Org mode
just fine.
</p>
</div>
</div>
<div id="outline-container-org07588c9" class="outline-2">
<h2 id="org07588c9"><span class="section-number-2">4</span> Caching   <span class="tag"><span class="ATTACH">ATTACH</span></span></h2>
<div class="outline-text-2" id="text-4">
<p>
Let’s keep going with something that would have made the cURL or Postman
workflow really strain. We will make requests against an API that returns a
large response. (Large as in lines of text, not as in MB.)
</p>
<p>
With such a large payload, we want to see if we can capture it once, and then
slice and dice it several ways. This is done by enabling caching. The resulting
header will be like this:
</p>
<pre class="example" id="org09f5b40">
#+begin_src restclient :results value drawer :cache yes
</pre>
<p>
We’re going to want to use this cache in later steps. A footnote here: first,
the drawer gets in the way of the results parsing. This can be overcome through
scripting, but the workaround is just to shift the output by 1 line. (More in
notes section, along with the workaround.)
</p>
<div class="org-src-container">
<pre class="src src-restclient" id="org84c1ab4"><span style="color: #51afef;">GET</span> <span style="color: #c678dd;">https://pokeapi.co/api/v2/pokemon-species/25/</span>
<span style="color: #dcaeea;">Accept</span>: <span style="color: #98be65;">application/json</span>
</pre>
</div>
<p>
The top of the results drawer looks like this:
</p>
<pre class="example" id="orgcab6adb">
:results:
#+RESULTS[af90d0f88686fd6432b6b25e0f7764fad2413481]: pokemon-cached
{
"base_happiness": 70,
"capture_rate": 190,
"color": {
"name": "yellow",
"url": "https://pokeapi.co/api/v2/pokemon-color/10/"
},
... ~ 4,000 more lines ...
</pre>
<p>
But when I collapse it with Emacs, it’s unintrusive:
</p>
<div id="orgf1a8d85" class="figure">
<p><img src="file:///Users/kylepasarelli/org/.attach/05/daf75a-9dfb-4fff-a87c-46e79330c646/_20210621_195727screenshot.png" alt="_20210621_195727screenshot.png" />
</p>
</div>
<p>
It’s gone from line 192 to line 4246. That’s a pretty big JSON payload.
How big?
</p>
<div class="org-src-container">
<pre class="src src-sh">wc
</pre>
</div>
<pre class="example">
4051 10049 133247
</pre>
<p>
Pretty hard to grok the overall structure and contents.
</p>
<p>
Now let’s have a look at the keys:
</p>
<div class="org-src-container">
<pre class="src src-sh">jq <span style="color: #98be65;">'keys'</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-json">[
<span style="color: #98be65;">"base_happiness"</span>,
<span style="color: #98be65;">"capture_rate"</span>,
<span style="color: #98be65;">"color"</span>,
<span style="color: #98be65;">"egg_groups"</span>,
<span style="color: #98be65;">"evolution_chain"</span>,
<span style="color: #98be65;">"evolves_from_species"</span>,
<span style="color: #98be65;">"flavor_text_entries"</span>,
<span style="color: #98be65;">"form_descriptions"</span>,
<span style="color: #98be65;">"forms_switchable"</span>,
<span style="color: #98be65;">"gender_rate"</span>,
<span style="color: #98be65;">"genera"</span>,
<span style="color: #98be65;">"generation"</span>,
<span style="color: #98be65;">"growth_rate"</span>,
<span style="color: #98be65;">"habitat"</span>,
<span style="color: #98be65;">"has_gender_differences"</span>,
<span style="color: #98be65;">"hatch_counter"</span>,
<span style="color: #98be65;">"id"</span>,
<span style="color: #98be65;">"is_baby"</span>,
<span style="color: #98be65;">"is_legendary"</span>,
<span style="color: #98be65;">"is_mythical"</span>,
<span style="color: #98be65;">"name"</span>,
<span style="color: #98be65;">"names"</span>,
<span style="color: #98be65;">"order"</span>,
<span style="color: #98be65;">"pal_park_encounters"</span>,
<span style="color: #98be65;">"pokedex_numbers"</span>,
<span style="color: #98be65;">"shape"</span>,
<span style="color: #98be65;">"varieties"</span>
]
</pre>
</div>
<p>
This API has some scalars like <code>name</code>, and some lists like <code>names</code>:
</p>
<div class="org-src-container">
<pre class="src src-sh">jq <span style="color: #98be65;">'.name'</span>
</pre>
</div>
<pre class="example">
"pikachu"
</pre>
<div class="org-src-container">
<pre class="src src-sh">jq <span style="color: #98be65;">'.names | length'</span>
</pre>
</div>
<pre class="example">
11
</pre>
<div class="org-src-container">
<pre class="src src-sh">jq <span style="color: #98be65;">'.names[0]'</span>
</pre>
</div>
<pre class="example">
{
"language": {
"name": "ja-Hrkt",
"url": "https://pokeapi.co/api/v2/language/1/"
},
"name": "ピカチュウ"
}
</pre>
<p>
Now let’s try and output all the 11 translations as a table.
</p>
<div class="org-src-container">
<pre class="src src-sh">jq -r <span style="color: #98be65;">'.names[] | [.name, (.language | .name, .url)] | @csv'</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-csv">"ピカチュウ","ja-Hrkt","https://pokeapi.co/api/v2/language/1/"
"Pikachu","roomaji","https://pokeapi.co/api/v2/language/2/"
"피카츄","ko","https://pokeapi.co/api/v2/language/3/"
"皮卡丘","zh-Hant","https://pokeapi.co/api/v2/language/4/"
"Pikachu","fr","https://pokeapi.co/api/v2/language/5/"
"Pikachu","de","https://pokeapi.co/api/v2/language/6/"
"Pikachu","es","https://pokeapi.co/api/v2/language/7/"
"Pikachu","it","https://pokeapi.co/api/v2/language/8/"
"Pikachu","en","https://pokeapi.co/api/v2/language/9/"
"ピカチュウ","ja","https://pokeapi.co/api/v2/language/11/"
"皮卡丘","zh-Hans","https://pokeapi.co/api/v2/language/12/"
</pre>
</div>
<p>
We can also put in headings. Again we’re using the cached copy.
</p>
<div class="org-src-container">
<pre class="src src-sh">jq -r <span style="color: #98be65;">'["Name", "Language name", "Language URL"], (.names[] | [.name, (.language | .name, .url)]) | @csv'</span>
</pre>
</div>
<div class="org" id="orgb8e9609">
<p>
“Name”,“Language name”,“Language URL”
“ピカチュウ”,“ja-Hrkt”,“<a href="https://pokeapi.co/api/v2/language/1/">https://pokeapi.co/api/v2/language/1/</a>”
“Pikachu”,“roomaji”,“<a href="https://pokeapi.co/api/v2/language/2/">https://pokeapi.co/api/v2/language/2/</a>”
“피카츄”,“ko”,“<a href="https://pokeapi.co/api/v2/language/3/">https://pokeapi.co/api/v2/language/3/</a>”
“皮卡丘”,“zh-Hant”,“<a href="https://pokeapi.co/api/v2/language/4/">https://pokeapi.co/api/v2/language/4/</a>”
“Pikachu”,“fr”,“<a href="https://pokeapi.co/api/v2/language/5/">https://pokeapi.co/api/v2/language/5/</a>”
“Pikachu”,“de”,“<a href="https://pokeapi.co/api/v2/language/6/">https://pokeapi.co/api/v2/language/6/</a>”
“Pikachu”,“es”,“<a href="https://pokeapi.co/api/v2/language/7/">https://pokeapi.co/api/v2/language/7/</a>”
“Pikachu”,“it”,“<a href="https://pokeapi.co/api/v2/language/8/">https://pokeapi.co/api/v2/language/8/</a>”
“Pikachu”,“en”,“<a href="https://pokeapi.co/api/v2/language/9/">https://pokeapi.co/api/v2/language/9/</a>”
“ピカチュウ”,“ja”,“<a href="https://pokeapi.co/api/v2/language/11/">https://pokeapi.co/api/v2/language/11/</a>”
“皮卡丘”,“zh-Hans”,“<a href="https://pokeapi.co/api/v2/language/12/">https://pokeapi.co/api/v2/language/12/</a>”
</p>
</div>
<p>
This table has been created directly from the API response. I never had to leave
the editor, copy & paste data or anything. Similarly, we can have a look at the
other two collections in the API, for Pokemon varieties and Pokedex numers
(whatever that is.)
</p>
<div class="org-src-container">
<pre class="src src-sh">jq -r <span style="color: #98be65;">'.varieties[] | [ .pokemon | .name, .url ] | @csv'</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-csv">"pikachu","https://pokeapi.co/api/v2/pokemon/25/"
"pikachu-rock-star","https://pokeapi.co/api/v2/pokemon/10080/"
"pikachu-belle","https://pokeapi.co/api/v2/pokemon/10081/"
"pikachu-pop-star","https://pokeapi.co/api/v2/pokemon/10082/"
"pikachu-phd","https://pokeapi.co/api/v2/pokemon/10083/"
"pikachu-libre","https://pokeapi.co/api/v2/pokemon/10084/"
"pikachu-cosplay","https://pokeapi.co/api/v2/pokemon/10085/"
"pikachu-original-cap","https://pokeapi.co/api/v2/pokemon/10094/"
"pikachu-hoenn-cap","https://pokeapi.co/api/v2/pokemon/10095/"
"pikachu-sinnoh-cap","https://pokeapi.co/api/v2/pokemon/10096/"
"pikachu-unova-cap","https://pokeapi.co/api/v2/pokemon/10097/"
"pikachu-kalos-cap","https://pokeapi.co/api/v2/pokemon/10098/"
"pikachu-alola-cap","https://pokeapi.co/api/v2/pokemon/10099/"
"pikachu-partner-cap","https://pokeapi.co/api/v2/pokemon/10148/"
"pikachu-gmax","https://pokeapi.co/api/v2/pokemon/10190/"
</pre>
</div>
<div class="org-src-container">
<pre class="src src-sh">jq -r <span style="color: #98be65;">'.pokedex_numbers[] | [.entry_number, (.pokedex | .name, .url)] | @csv'</span>
</pre>
</div>
</div>
<div id="outline-container-org27c72ce" class="outline-3">
<h3 id="org27c72ce"><span class="section-number-3">4.1</span> Org mode hackery</h3>
<div class="outline-text-3" id="text-4-1">
<p>
This is all pretty much out-of-the-box Emacs. Things get a bit more interesting
when we teach Emacs a few new tricks. Since Babel has support for Emacs Lisp as
a language, we can define functions in in the document, which when evaluated
will add to or modify the document. Let’s see for example a code block which
prints the current heading.
</p>
<div class="org-src-container">
<pre class="src src-elisp"><span style="color: #51afef;">(</span><span style="color: #c678dd;">org-entry-get</span> nil <span style="color: #98be65;">"ITEM"</span><span style="color: #51afef;">)</span>
</pre>
</div>
<pre class="example">
Org mode hackery
</pre>
<p>
(As a disclaimer, my elisp-fu is pretty weak, so for sure all of these things can
be done in a much better way.)
</p>