This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path07.restful.apis.html
executable file
·1603 lines (1430 loc) · 56.1 KB
/
07.restful.apis.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Web & Mobile Development — 07.RESTful.APIs</title>
<meta name="description" content="Web & Mobile Development — 07.RESTful.APIs">
<meta name="author" content="Bram(us) Van Damme - ikdoeict.be">
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css" media="screen">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- <link rel="stylesheet" href="assets/07/examples.css"> -->
<link rel="stylesheet" href="lib/zenburn.css">
</head>
<body>
<div id="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- 0 : Title -->
<section>
<section>
<h3 class="inverted">Web & Mobile Development <small>JLW288</small></h3>
<h1>07.RESTful.APIs</h1>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
<script>
// Delicously hacky. Look away.
if(navigator.userAgent.match(/(iPhone|iPad|iPod|Android)/i))
document.write('<p style="color: rgba(0,0,0,0.3); text-shadow: none;">('+'Tap to navigate'+')</p>');
</script>
</section>
</section>
<!-- 1 : REST Intro -->
<section>
<section>
<h2>RESTful APIs</h2>
<p><img src="assets/07/resting.png" alt="" title="" height="375" width="500" /></p>
<q>The key to victory is discipline,<br />and that means a well made bed.</q>
</section>
<section>
<h2>RESTful API?</h2>
<ul>
<li class="fragment">
An API that is REST compliant
</li>
</ul>
</section>
<section>
<h2>SRSLY?</h2>
<p><img src="assets/07/notsureif.png" alt="Not Sure If ..." title="Not Sure If ..." width="480" height="360" /></p>
<p><em>Not sure if trolling or just “technically correct”</em></p>
</section>
<section>
<h2>API</h2>
<ul>
<li class="fragment">
Application Programming Interface
<ul>
<li class="fragment">Abstract Interface to allow communication between software components</li>
<li class="fragment">eg. Photoshop has a JS based API to remotely control Photoshop</li>
<li class="fragment">eg. Browsers have an API that allow creation of extensions</li>
<li class="fragment">eg. Browsers have a Device API that give JS devvers access to device features such as geolocation, camera, vibration, etc.</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Web API</h2>
<ul>
<li class="fragment">
Websites can also offer an API
<ul>
<li class="fragment">Allow you to push/pull data to/from a web site</li>
<li class="fragment">Scripting language independent (PHP, ASP, RoR, ..)</li>
<li class="fragment">Sometimes referred to as a “Web Service” <em>(blame Microsoft)</em></li>
<li class="fragment">Implemented via JavaScript, XML-RPC, SOAP, REST, …</li>
<li class="fragment">Return formats JS, XML, HTML, plaintext, JSON(P), …</li>
</ul>
</li>
</ul>
<footer class="fragment">
<em>You've already implemented a SOAP-based one in the course Webprogrammatie</em>
</footer>
</section>
<section>
<h2>Why provide an API? (1)</h2>
<ul>
<li class="fragment">
Technical Limitations
<ul>
<li class="fragment">You cannot connect to a MySQL server from an iOS app you made</li>
<li class="fragment">You cannot connect to a MySQL server from within JavaScript</li>
</ul>
</li>
<li class="fragment">
Security Issues
<ul>
<li class="fragment">It is not smart to connect to a MySQL server from an iOS app you made</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Why provide an API? (2)</h2>
<ul>
<li class="fragment">
See cool stuff built on top of your technology platform
<div style="text-align: center; margin-top: 30px;">
<figure class="fragment">
<img src="assets/07/BotanicallsSparkfunKit_jade1_800.jpg" alt="" title="" height="300" width="400" />
<figcaption>Let your plants tell you via Twitter that they're thirsty with <a href="http://www.botanicalls.com/kits/">botanicalls</a></figcaption>
</figure>
<figure class="fragment">
<img src="assets/07/kickbee.jpg" alt="" title="" height="250" width="400" />
<figcaption>Know when your baby kicked via Twitter with <a href="http://www.botanicalls.com/kits/">http://kickbee.net/</a></figcaption>
</figure>
</div>
</li>
</ul>
</section>
<section>
<h2>Why provide an API? (3)</h2>
<ul>
<li class="fragment">
See cool stuff built using your data
<div style="text-align: center; margin-top: 30px;">
<figure class="fragment">
<img src="assets/07/gowallaheat.jpg" alt="" title="" height="300" width="400" />
<figcaption>Visualize your Gowalla (RIP) checkins with <a href="http://gowallaheat.bram.us/">GowallaHEAT</a></figcaption>
</figure>
<figure class="fragment">
<img src="assets/07/tweetie.jpg" alt="" title="" height="300" width="410" />
<figcaption>Write your own Twitter Client (and eventually <a href="http://mashable.com/2010/04/09/breaking-twitter-acquires-tweetie-iphone-app/">get bought by Twitter</a>)</figcaption>
</figure>
</div>
</li>
</ul>
</section>
<section>
<h2>REST</h2>
<ul>
<li class="fragment">
REST = REpresentational State Transfer
<ul>
<li class="fragment">
Architectural style for distributed hypermedia systems <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm">described</a> by Roy Fielding, in 2000
</li>
</ul>
</li>
<li class="fragment">
REST constraints
<ol>
<li class="fragment">Client-server</li>
<li class="fragment">Stateless Server</li>
<li class="fragment">Cacheable</li>
<li class="fragment">
Uniform interface
<ol>
<li>Identification of resources <em>(URI)</em></li>
<li>Manipulation of resources through representations <em>(modify/delete)</em></li>
<li>Self-descriptive messages <em>(message and meta-data)</em></li>
<li>Hypermedia As The Engine Of Application State <em>(state transitions)</em></li>
</ol>
</li>
<li class="fragment">Layered System</li>
<li class="fragment">Code-On-Demand <em>(optional)</em></li>
</ol>
</li>
</ul>
</section>
<section>
<h2>REST API</h2>
<ul>
<li class="fragment">
API that follows the REST constraints
</li>
<li class="fragment">
Constraint #4 (Uniform interface) is the tricky one
</li>
</ul>
</section>
<section>
<h2>Why choose REST?</h2>
<ul>
<li class="fragment">As a consumer, it's easy to work with</li>
<li class="fragment">As a developer, it's (fairly) easy to implement</li>
<li class="fragment">It's well structured</li>
<li class="fragment">
It's popular
<div style="text-align: center; margin-top: 10px;">
<figure>
<img src="assets/07/rest_winning.png" alt="" title="" height="299" width="707" />
<figcaption>Figure by <a href="http://www.slideshare.net/jmusser/pw-glue-conmay2010">jmusser</a></figcaption>
</figure>
</div>
</li>
</ul>
</section>
</section>
<!-- 1 : REST URL Design -->
<section>
<section>
<h2>Richardson Maturity Model</h2>
<p><img src="assets/07/globetrotters.png" alt="" title="" height="375" width="500" /></p>
<q>This chronological wang-dang-doodle<br />could destroy the very matrix of reality.</q>
</section>
<section>
<h2>Richardson Maturity Model</h2>
<ul>
<li class="fragment">
Model that breaks down the principal elements of a REST approach in a few steps
</li>
<li class="fragment">
The model embraces 4 levels
<ol start="0">
<li class="fragment">Level 0: The Swamp of POX</li>
<li class="fragment">Level 1: Identify and organize resources in a hierarchy</li>
<li class="fragment">Level 2: Use HTTP verbs for operations</li>
<li class="fragment">Level 3: <acronym title="Hypertext As The Engine Of Application State">HATEOAS</acronym></li>
</ol>
</li>
</ul>
<footer class="fragment"><em>Most self-proclaimed “RESTful APIs” out there aren't even level 1 compliant</em></footer>
</section>
<section>
<h2>0. The Swamp of POX</h2>
<p><img src="assets/07/rmm-0.png" alt="Richardson Maturity Model Level 0" title="Richardson Maturity Model Level 0" /></p>
</section>
<section>
<h2>What?</h2>
<ul>
<li class="fragment">
Use HTTP as a transport mechanism
<ul>
<li class="fragment">
Mostly one URI (<code>/api</code>) and one METHOD (<code>POST</code>)
</li>
<li class="fragment">
Based on the sent-in data (in <code>$_POST</code>) you get back some data
</li>
</ul>
</li>
<li class="fragment">
POX = Plain Old XML
<ul>
<li class="fragment">
Of course you can use a different format if you like
<ul>
<li class="fragment">Plaintext is just ... plain</li>
<li class="fragment">JSON is concise and quite readable too</li>
<li class="fragment">JSONP does not allow custom headers and only works with <code>GET</code></li>
</ul>
</li>
<li class="fragment">
→ We'll go for JSON
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>API Call Example</h2>
<ul>
<li class="fragment">
Do a <code>POST</code> request to <code>/api</code> with <code>$_POST['action']</code> set to <code>getLecturers</code> and get back a list of lecturers as the actual content
<pre class="bigger"><code class="language-javascript dontrun" style="max-height: 380px; overflow: scroll;">{
"lecturers":
[
{
"id": 1,
"name": "Davy De Winne"
},
{
"id": 2,
"name": "Bramus Van Damme"
}
]
}</code></pre>
</li>
</ul>
</section>
<section>
<h2>1. Resources</h2>
<p><img src="assets/07/rmm-1.png" alt="Richardson Maturity Model Level 1" title="Richardson Maturity Model Level 1" /></p>
</section>
<section>
<h2 style="font-size: 200%">Resource Identification & Hierarchy</h2>
<ul>
<li class="fragment">
Basic naming conventions
<ul>
<li class="fragment">Verbs are bad, nouns are good</li>
<li class="fragment">Use plurals</li>
<li class="fragment">Respect the Hierarchy</li>
</ul>
</li>
<li class="fragment">
Examples
<ul>
<li class="fragment"><del><code>/product</code></del> → <ins><code>/products</code></ins></li>
<li class="fragment"><del><code>/product/1234</code></del> → <ins><code>/products/1234</code></ins></li>
<li class="fragment"><del><code>/photos/product/1234</code></del> → <ins><code>/products/1234/photos</code></ins></li>
<li class="fragment"><del><code>/photos/product/1234/5678</code></del> → <ins><code>/products/1234/photos/5678</code></ins></li>
</ul>
</li>
</ul>
<footer class="fragment"><em>Note: This doesn't apply for “normal pages” of your site</em></footer>
</section>
<section>
<h2>Identification & Hierarchy bis</h2>
<ul>
<li class="fragment">
Use concrete naming, yet don't be too specific
<ul>
<li class="fragment"><del><code>/things</code></del></li>
<li class="fragment"><del><code>/animals</code></del></li>
<li class="fragment"><code class="ok">/dogs</code></li>
<li class="fragment"><del><code>/beagles</code></del></li>
</ul>
</li>
<li class="fragment">
Some cases will require a verb instead of a noun, yet consider these exceptions
<ul>
<li class="fragment"><code>/search?course=WMD</code></li>
<li class="fragment"><code>/convert?from=EUR&to=USD&amount=100</code></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>URL Structure Example</h2>
<ul class="fragment">
<li><code>/</code></li>
<li><code>/movies</code></li>
<li><code>/movies/<em>{id}</em></code></li>
<li><code>/movies/<em>{id}</em>/photos</code></li>
<li><code>/actors</code></li>
<li><code>/actors/<em>{id}</em></code></li>
<li><code>/actors/<em>{id}</em>/movies</code></li>
<li><code>/about</code></li>
<li><code>/contact</code></li>
</ul>
</section>
<section>
<h2>API Call Example</h2>
<ul>
<li class="fragment">
Do a <code>POST</code> request to <code>/api/lecturers</code> and get back a list of lecturers as the actual content
<pre class="bigger"><code class="language-javascript dontrun" style="max-height: 380px; overflow: scroll;">{
"lecturers":
[
{
"id": 1,
"name": "Davy De Winne"
},
{
"id": 2,
"name": "Bramus Van Damme"
}
]
}</code></pre>
</li>
</ul>
</section>
<section>
<h2>URL Design Sidenotes (1)</h2>
<ul>
<li class="fragment">
API Versioning
<ul>
<li class="fragment">
Don't place it in the URL (e.g. <code>api.twitter.com/v1/…</code> is wrong)
</li>
<li class="fragment">
Solution: see further
</li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em">
Filtering & Sorting
<ul>
<li class="fragment">Use URL params <code>?filter=foo&sort=asc</code></li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em">
Limiting fields
<ul>
<li class="fragment">Use an URL param <code>?fields=firstname,lastname,age</code></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>URL Design Sidenotes (2)</h2>
<ul>
<li class="fragment">
Pagination
<ul>
<li class="fragment">Mandatory for huge resources</li>
<li class="fragment">
Use <code>limit</code> and <code>offset</code> URL params
<ul>
<li>Defaulting to <code>?limit=10&offset=<max_id></code></li>
<li>Avoid using numbers for <code>offset</code>, opt for id-constraint based on sorting method; this avoids doubles when paginating over a frequently updated result set.</li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>2. HTTP Verbs</h2>
<p><img src="assets/07/rmm-2.png" alt="Richardson Maturity Model Level 2" title="Richardson Maturity Model Level 2" /></p>
</section>
<section>
<h2>HTTP Verbs</h2>
<ul>
<li class="fragment">
HTTP Verbs = CRUD = Queries
<ol>
<li class="fragment"><code>POST</code> = Create = <code>INSERT</code></li>
<li class="fragment"><code>GET</code> = Read = <code>SELECT</code></li>
<li class="fragment"><code>PUT</code> = Update = <code>UPDATE</code></li>
<li class="fragment"><code>DELETE</code> = Delete = <code>DELETE</code></li>
</ol>
</li>
<li class="fragment">
Examples
<ol>
<li class="fragment"><code>POST /products</code> <em>(data via <code>$_POST</code>)</em></li>
<li class="fragment"><code>GET /products</code> or <code>GET /products/1234</code></li>
<li class="fragment"><code>PUT /products/1234</code> <em>(data via “<code>$_PUT</code>”)</em></li>
<li class="fragment"><code>DELETE /products/1234</code></li>
</ol>
</li>
</ul>
</section>
<section>
<h2>HTTP Status Codes (1)</h2>
<ul>
<li class="fragment">
When accessing a resource, you'll get back the data, along with <a href="http://httpstatus.es/">an HTTP status code</a>
</li>
<li class="fragment">
General Codes (applicable to all verbs/resources)
<ul>
<li class="fragment"><code>401 — Not Authorized</code> <em>(e.g. No API Key given)</em></li>
<li class="fragment"><code>405 — Method Not Allowed</code> <em>(e.g. wrong verb for resource)</em></li>
<li class="fragment"><code>500 — Internal Server Error</code> <em>(Developer screwed up ;))</em></li>
</ul>
</li>
<li class="fragment">
<code>GET</code> <small style="vertical-align: baseline;">(SQL SELECT)</small>
<ul>
<li class="fragment"><code>200 — OK</code> <em>(requested data sent via response body)</em></li>
<li class="fragment"><code>404 — Not Found</code> <em>(resource not found, not “API not found”)</em></li>
</ul>
</li>
<li class="fragment">
<code>POST</code> <small style="vertical-align: baseline;">(SQL INSERT)</small>
<ul>
<li class="fragment"><code>201 — Created</code> <em>(=OK)</em></li>
<li class="fragment"><code>400 — Bad Request</code> <em>(Missing Parameters)</em></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>HTTP Status Codes (2)</h2>
<ul>
<li class="fragment">
<code>PUT</code> <small style="vertical-align: baseline;">(SQL UPDATE)</small>
<ul>
<li class="fragment"><code>200 — OK</code> <em>(requested data sent via response body)</em></li>
<li class="fragment"><code>400 — Bad Request</code> <em>(Missing Parameters)</em></li>
<li class="fragment"><code>404 — Not Found</code></li>
</ul>
</li>
<li class="fragment">
<code>DELETE</code> <small style="vertical-align: baseline;">(SQL DELETE)</small>
<ul>
<li class="fragment"><code>204 — No Content</code> <em>(=OK — Note: <strong>no</strong> response body sent!)</em></li>
<li class="fragment"><code>404 — Not Found</code></li>
</ul>
</li>
<li class="fragment">
Warning!
<ul><li>HTTP Statuses 4XX and 5XX will invoke <code>$.ajax().error()</code> in jQuery!</li></ul>
</li>
</ul>
</section>
<section>
<h2>API Call Example (1)</h2>
<ul>
<li class="fragment">
Do a <code>GET</code> request to <code>/api/lecturers</code> and get back an HTTP <code>200</code> status code and a list of lecturers as the content
<pre class="bigger"><code class="language-javascript dontrun" style="max-height: 380px; overflow: scroll;">{
"lecturers":
[
{
"id": 1,
"name": "Davy De Winne"
},
{
"id": 2,
"name": "Bramus Van Damme"
}
]
}</code></pre>
</li>
</ul>
</section>
<section>
<h2>API Call Example (2)</h2>
<ul>
<li class="fragment">
Do a <code>POST</code> request to <code>/api/lecturers</code> with some data in <code>$_POST</code> to insert a new lecturer
<ul>
<li class="fragment">Get back <code>201 — Created</code> if all OK</li>
<li class="fragment">Get back <code>400 — Bad Request</code> if parameters are missing</li>
</ul>
</li>
<li class="fragment" style="margin-top: 0.8em;">
Do a <code>PUT</code> request to <code>/api/lecturers/2</code> with some data in <code>$_PUT</code> to update a lecturer
<ul>
<li class="fragment">Get back <code>200 — OK</code> if all OK</li>
<li class="fragment">Get back <code>400 — Bad Request</code> if parameters are missing</li>
<li class="fragment">Get back <code>404 — Not Found</code> if the lecturer does not exist</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>3. Hypermedia Controls</h2>
<p><img src="assets/07/rmm-3.png" alt="Richardson Maturity Model Level 3" title="Richardson Maturity Model Level 3" /></p>
</section>
<section>
<h2>HATEOAS (1)</h2>
<ul>
<li class="fragment">
Hypermedia as the Engine of Application State
<ul>
<li class="fragment">Application State = Where the user is in the process of completing a task <em>(e.g. a page of the application)</em></li>
<li class="fragment">Hypermedia = <q>The simultaneous presentation of information and controls such that the information becomes the <abbr title="A quality of an object, or an environment, which allows an individual to perform an action">affordance</abbr> through which the user obtains choices and selects actions</q> <small><a href="http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven#comment-718">⚑</a></small></li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em;">
Roughly translated
<ul>
<li>
The response must be hypertext and include links so that the user/automation can transition to other pages of the application
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>HATEOAS (2)</h2>
<ul>
<li class="fragment">
Essentially, an API consumer should only know
<ol>
<li>
The URI starting point
</li>
<li>
How to parse the response <em>(viz. what media type it is)</em>
</li>
</ol>
</li>
<li class="fragment">With this, the consumer has enough info to parse out the links and transition to other states when needed</li>
<li class="fragment" style="margin-top: 1em;">
When an API embraces HATEOAS, we can call it a <strong>Hypermedia API</strong>
</li>
</ul>
</section>
<section>
<h2>Response Media Types (1)</h2>
<ul>
<li class="fragment">
Responses <strong>must</strong><small><a href="http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven">⚑</a></small> be hypertext to be RESTful
<ul>
<li class="fragment">
XML and JSON are not hypermedia types; they only define a syntax
</li>
<li class="fragment">
(X)HTML, ATOM, RDF/XML, etc. are hypermedia types
<ul>
<li>These are all <a href="http://amundsen.com/hypermedia/">specified</a></li>
<li>Each has their own MIME Type, e.g. <code>application/atom+xml</code></li>
</ul>
</li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em;">
JSON based hypermedia types are in the making
<ul>
<li><a href="https://github.com/collection-json/spec">Collection+JSON</a></li>
<li><a href="https://github.com/kevinswiber/siren">Siren</a></li>
<li><a href="http://stateless.co/hal_specification.html">HAL</a></li>
<li><a href="http://json-ld.org/">JSON-LD</a></li>
</ul>
</li>
<li class="fragment">Collection+JSON looks very promising</li>
</ul>
</section>
<section>
<h2>Response Media Types (2)</h2>
<ul>
<li class="fragment">
It's perfectly possible to roll your own hypermedia type
<ul>
<li>e.g. <code>application/vnd.ikdoeict.whatever</code></li>
<li>General consensus amongst hypermedia gurus: don't</li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em">
An API can support multiple hypermedia types
<ul>
<li class="fragment">
The client indicates in an <code>accept</code> header which
<ul>
<li>e.g <code>Accept: application/collection+json</code></li>
</ul>
</li>
<li class="fragment">
Content negotiation:
<ul>
<li>e.g. <code>Accept: application/xml; q=0.8, application/json</code></li>
</ul>
</li>
<li class="fragment">
Also use this header for versioning
<ul>
<li>e.g. <code>Accept: application/vnd.ikdoeict.whatever-v2+json</code><br/>or <code>Accept: application/collection+json;v=2</code></li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>What will we do?</h2>
<ul>
<li class="fragment">
We're going to break the rules and use our own format
<ul>
<li class="fragment">
JSON based
</li>
<li class="fragment">
Served as plain <code>application/json</code> <em>(Sorry Roy, gotta <a href="http://www.w3.org/TR/html-design-principles/#pave-the-cowpaths">pave the cowpaths</a>)</em>
</li>
<li class="fragment">
The returned HTTP status code will also be part of the response<br />
<em>(but we'll also send it via the response headers as RMM Level 2 requires)</em>
<ul>
<li>The response is split out in a <code>status</code> and <code>content</code> object</li>
</ul>
</li>
<li class="fragment">
When links are needed, inject a <code>links</code> array holding the links
<ul>
<li class="fragment">
Links have a <code>rel</code> and <code>href</code> property
</li>
<li class="fragment">
<code>rel</code> values are based on <a href="http://www.iana.org/assignments/link-relations/link-relations.xml">RFC5988</a>
<ul>
<li><code>self</code>, <code>up</code>, <code>index</code>, <code>next</code>, <code>prev</code>, ...</li>
<li>When no value can be found in the RFC, pick your own (e.g. <code>photos</code>)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>API Call Example</h2>
<ul>
<li class="fragment" style="font-size: 90%">
Do a <code>GET</code> request to <code>/lecturers</code> and get back a custom JSON response with HTTP status code <code>200</code> containing a list of lecturers (with resource links) as the response content
<pre class="bigger"><code class="language-javascript dontrun" style="max-height: 260px; overflow: scroll;">{
"status":
{
"code": 200,
"text": "OK"
},
"content":
{
"size": 2,
"lecturers":
[
{
"id": 1,
"name": "Davy De Winne",
"links": [
{
"rel": "self",
"href": "/users/1"
}
]
},
{
"id": 2,
"name": "Bramus Van Damme",
"links": [
{
"rel": "self",
"href": "/users/2"
}
]
}
]
}
}</code></pre>
</li>
</ul>
<footer class="fragment"><em>Note: we've broken the rules here ... this ain't a genuine Hypermedia API!</em></footer>
</section>
<section>
<h2>Summary</h2>
<ul>
<li class="fragment">
Real RESTful APIs or Hypermedia APIs are APIs that consist of two things
<ol>
<li class="fragment">Usage of HTTP to its fullest <em>(RMM Levels 0 - 2)</em></li>
<li class="fragment">Responses are served as hypermedia that manages application state <em>(RMM Level 3)</em></li>
</ol>
</li>
<li class="fragment" style="margin-top: 1em;">
We'll be RMM level 3 <em>compliant-ish</em> and use our own (simple) hypermedia format but serve it as <code>application/json</code> — It cuts the mustard
</li>
</ul>
</section>
</section>
<!-- 4 : API Case Study -->
<section>
<section>
<h2>RESTful API Case Study</h2>
<p><img src="assets/07/ascent-of-bot.png" alt="" title="" height="371" width="684"></p>
<q>Hey, look! I found a robot fossil!</q>
</section>
<section>
<h2>Twitter API (1)</h2>
<ul>
<li class="fragment">
One of the first RESTful APIs, suffering from historically grown bloat.
<ul>
<li class="fragment">Spoiler: it's not RESTful at all!</li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em;">
Get a status
<ul>
<li class="fragment">
Endpoint
<ul>
<li><code>GET api.twitter.com/1/statuses/show/id.format</code></li>
</ul>
</li>
<li class="fragment">
Problems
<ul>
<li class="fragment">API Version in URL</li>
<li class="fragment">Operation <code>show</code> included in URL</li>
<li class="fragment"><code>id</code> not a child of <code>statuses</code></li>
</ul>
</li>
<li class="fragment">
Better
<ul>
<li class="fragment"><code>GET api.twitter.com/statuses/{id}</code></li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Twitter API (2)</h2>
<ul>
<li class="fragment">
Set (update) your status
<ul>
<li class="fragment">
Endpoint
<ul>
<li><code>POST api.twitter.com/1/statuses/update/id.format</code></li>
</ul>
</li>
<li class="fragment">
Problems
<ul>
<li class="fragment">API Version in URL</li>
<li class="fragment">Operation <code>update</code> included in URL</li>
<li class="fragment">Uses authenticated user implicitly</li>
</ul>
</li>
<li class="fragment">
Better
<ul>
<li class="fragment"><code>POST api.twitter.com/users/{id}/statuses</code></li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Twitter API (3)</h2>
<ul>
<li class="fragment">
Remove a status udpate
<ul>
<li class="fragment">
Endpoint
<ul>
<li><code>POST api.twitter.com/1/statuses/destroy/id.format</code></li>
</ul>
</li>
<li class="fragment">
Problems
<ul>
<li class="fragment">API Version in URL</li>
<li class="fragment">Operation <code>destroy</code> included in URL</li>
<li class="fragment">Uses authenticated user implicitly</li>
</ul>
</li>
<li class="fragment">
Better
<ul>
<li class="fragment"><code>DELETE api.twitter.com/users/{id}/statuses/{id}</code></li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Twitter API (4)</h2>
<ul>
<li class="fragment">
Get retweets of a status
<ul>
<li class="fragment">
Endpoint
<ul>
<li><code>GET api.twitter.com/1/statuses/retweets/id.format</code></li>
</ul>
</li>
<li class="fragment">
Problems
<ul>
<li class="fragment">API Version in URL</li>
<li class="fragment">Hierarchy is wrong</li>
</ul>
</li>
<li class="fragment">
Better
<ul>
<li class="fragment"><code>GET api.twitter.com/statuses/{id}/retweets</code></li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>MoviesDB (1)</h2>
<p style="text-align: center;" class="fragment"><a href="http://bramus.github.com/simple-rest-api-explorer/"><img src="assets/07/moviesdb.png" width="600" height="487" alt="" title="" style="background: transparent; border: 0; -webkit-box-shadow: none; -moz-box-shadow: none; -ms-box-shadow: none; -o-box-shadow: none; box-shadow: none;" /></a><p>
<footer class="fragment">(click image to launch)</footer>
</section>
<section>
<h2>MoviesDB (2)</h2>
<ul>
<li class="fragment">
Example API
</li>
<li class="fragment">
Play around with the API Explorer to check it out
<ul>
<li class="fragment">FYI: You can <a href="http://bramus.github.com/simple-rest-api-explorer/">Fork the API Explorer</a></li>
</ul>
</li>
<li class="fragment" style="margin-top: 1em;">
Recommened tool: <a href="https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en">Chrome Postman plugin</a>
<div style="width: 350px; margin: 0.4em auto 0;"><iframe width="350" height="263" src="http://www.youtube.com/embed/VPLlkhhRMvQ" frameborder="0" allowfullscreen></iframe></div>
</li>
</ul>
</section>
</section>
<!-- 3 : REST in PHP -->
<section>
<section>
<h2>RESTful APIs in PHP</h2>
<p><img src="assets/07/groin.png" alt="" title="" height="338" width="600" /></p>
<q>Would it cheer you up if I punch Fry in the groin?<br />Cause I'll do it, regardless.</q>
</section>
<section>
<h2>Prerequisite: Supporting nice URLs</h2>
<ul>
<li class="fragment">
Route all requests to non-existing files/folders to <code>index.php</code>
</li>
<li class="fragment" style="margin-top: 1em;">
Build <code>index.php</code> in such a way that it decides what to do based on the URL (found in <code>$_SERVER['REQUEST_URI']</code>)
</li>
</ul>
</section>
<section>
<h2>Nice URLs in Apache</h2>
<ul>