-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·562 lines (531 loc) · 19.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>API - State of the art | Miha Hribar</title>
<meta name="description" content="A brief overview of the current state of APIs, where we are and where we are going.">
<meta name="author" content="Miha Hribar">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/default.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<style type="text/css">
.reveal img.noborder { border: 0; background: none; box-shadow: none }
.twocolumn { -moz-column-count:2; -webkit-column-count:2; column-count:2; }
.twocolumn, .onecolumn { list-style: none !important; }
.twocolumn p, .onecolumn p { font-size: 0.8em; margin-bottom: 1em }
.twocolumn h4, .onecolumn h4 { margin-bottom: 0; margin-top: 0 }
</style>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>API</h1>
<h3>State of the art</h3>
</section>
<section>
<h2>@mihahribar</h2>
<p><img class="noborder" src="images/toshl.png" alt=""></p>
<h3>@toshl</h3>
</section>
<section>
<h2>How we build APIs today?</h2>
<ul>
<li class="fragment">SOAP</li>
<li class="fragment">RPC</li>
<li class="fragment">REST</li>
</ul>
</section>
<!-- REST section -->
<section>
<section>
<h2>Define: REST</h2>
<blockquote cite="http://en.wikipedia.org/wiki/Representational_state_transfer">
REpresentational State Transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web.
</blockquote>
</section>
<section>
<h2>Define: RESTful API</h2>
<blockquote cite="http://en.wikipedia.org/wiki/Representational_state_transfer">
A RESTful API is a web service implemented using HTTP and the principles of REST.
</blockquote>
<p>Roy Fielding <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm">doctoral dissertation</a>, 2000.</p>
</section>
<section>
<h2>Constraints</h2>
<ul class="twocolumn">
<li class="fragment">
<h4>Client-server</h4>
<p>Separate clients and servers.</p>
</li>
<li class="fragment">
<h4>Stateless server</h4>
<p>Each request from the client contains all the information necessary to service the request.</p>
</li>
<li class="fragment">
<h4>Cacheable</h4>
<p>Clients can cache responses, if permitted by the server.</p>
</li>
<li class="fragment">
<h4>Layered system</h4>
<p>Load balancers, proxies, firewalls etc.</p>
</li>
<li class="fragment">
<h4>Code on demand (optional)</h4>
<p>Client can request code from server and execute it.</p>
</li>
<li class="fragment">
<h4>Uniform interface</h4>
<p>Between the client and the server.</p>
</li>
</ul>
</section>
<section>
<h2>Uniform Interface</h2>
<ul class="onecolumn">
<li>
<h4>Identification of resources</h4>
<p>Through URI. <code>api.toshl.com/users/42</code></p>
</li>
<li class="fragment">
<h4>Manipulation of resources</h4>
<p>POST, GET, PUT/PATCH, DELETE</p>
</li>
<li class="fragment">
<h4>Self-descriptive messages</h4>
<p>Through meta data.</p>
</li>
<li class="fragment">
<h4>Hypermedia as the engine of application state</h4>
<p>HATEOAS, huh?</p>
</li>
</ul>
</section>
<section>
<h2>Define: HATEOAS</h2>
<blockquote cite="http://en.wikipedia.org/wiki/HATEOAS">
A client interacts with a network application entirely through hypermedia provided dynamically by application servers. A REST client needs no prior knowledge about how to interact with any particular application or server beyond a generic understanding of hypermedia.
</blockquote>
<p class="fragment">In HTML <code><a></code>, <code><form></code>, <code><img></code> etc.</p>
</section>
</section>
<!-- REST design -->
<section>
<h2>Let's jump in</h2>
<img src="images/frozen-pond.gif">
<p class="fragment">Think things through</p>
</section>
<section>
<h2>When in doubt</h2>
<p>Check the spec <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.html">RFC 2616</a>.</p>
</section>
<section>
<h2>URI Structure</h2>
<ul>
<li>Collections</li>
<li>Resources</li>
<li>Controllers</li>
</ul>
<pre class="fragment"><code>https://api.toshl.com/expenses
https://api.toshl.com/expenses/42
https://api.toshl.com/budgets
https://api.toshl.com/export
</code></pre>
</section>
<section>
<h2>Help a client out</h2>
<pre><code>$ curl -v https://api.toshl.com
> GET / HTTP/1.1
>
< HTTP/1.1 204 No Content
< Allow: GET
< Link: </expenses>; rel="expenses",</expenses/tags>; rel="expense tags",</incomes>; rel="incomes",...
< Content-Length: 0
< Content-Type: application/json</code></pre>
<p>Include a handy <code>Link</code> header.</p>
</section>
<section>
<h2>CRUD</h2>
<ul>
<li>POST = <strong>C</strong>reate</li>
<li>GET = <strong>R</strong>ead</li>
<li>PUT/PATCH = <strong>U</strong>pdate</li>
<li>DELETE = <strong>D</strong>elete</li>
</ul>
<pre class="fragment"><code>$ curl -v https://api.toshl.com/expenses
> GET / HTTP/1.1
>
< HTTP/1.1 200 OK
< Allow: GET, POST, HEAD</code></pre>
</section>
<section>
<h2>Use correct response status codes</h2>
<ul>
<li>200 (OK)</li>
<li>201 (Created)</li>
<li>202 (Accepted)</li>
<li>204 (No Content)</li>
</ul>
</section>
<section>
<h2>Create</h2>
<pre><code>$ curl -v -X POST -d "..." https://api.toshl.com/expenses
> POST /expenses HTTP/1.1
>
< HTTP/1.1 201 Created
< Allow: GET,POST
< Location: /expenses/1031
< Content-Length: 176
< Content-Type: application/json
<
{"id":1031,"amount":100,"currency":"EUR","rate":1,"date":"2012-10-23","desc":null,"tags":["food"],"loc":["0.000000","0.000000"],"modified":"2012-10-23T19:20:46+00:00Z","repeat":null,"extra":null}</code></pre>
<p class="fragment">Spot the error?</p>
</section>
<section>
<h2>Create</h2>
<pre><code>$ curl -v -X POST -d "..." https://api.toshl.com/expenses
> POST /expenses HTTP/1.1
>
< HTTP/1.1 201 Created
< Allow: GET,POST
< Location: /expenses/1031
< Content-Length: 200
< Content-Type: application/json
<
{"id":1031,"href":"/expenses/1031","amount":100,"currency":"EUR","rate":1,"date":"2012-10-23","desc":null,"tags":["food"],"loc":["0.000000","0.000000"],"modified":"2012-10-23T19:20:46+00:00Z","repeat":null,"extra":null}</code></pre>
<p>HATEOAS - include a link to the resource URI.</p>
</section>
<section>
<h2>Read</h2>
<pre><code>$ curl -v https://api.toshl.com/expenses/1031
> GET /expenses/1031 HTTP/1.1
>
< HTTP/1.1 200 OK
< Allow: GET,PUT,PATCH,DELETE
< Content-Length: 219
< Content-Type: application/json
<
{"id":1031,"href":"/expenses/1031","amount":100,"currency":"EUR","rate":1,"date":"2012-10-23","desc":null,"tags":["food"],"loc":["0.000000","0.000000"],"modified":"2012-10-23T19:20:46+00:00Z","repeat":null,"extra":null}</code></pre>
</section>
<section>
<h2>Update</h2>
<pre><code>$ curl -v -X PUT -d "..." https://api.toshl.com/expenses/1031
> PUT /expenses/1031 HTTP/1.1
>
< HTTP/1.1 200 OK
< Allow: GET,PUT,PATCH,DELETE
< Content-Length: 219
< Content-Type: application/json
<
{"id":1031,"href":"/expenses/1031","amount":200,"currency":"EUR","rate":1,"date":"2012-10-23","desc":null,"tags":["food"],"loc":["0.000000","0.000000"],"modified":"2012-10-23T19:20:46+00:00Z","repeat":null,"extra":null}</code></pre>
<p class="fragment">GET and PUT are idempotent methods.</p>
</section>
<section>
<h2>Delete</h2>
<pre><code>$ curl -v -X DELETE https://api.toshl.com/expenses/1031
> DELETE /expenses/1031 HTTP/1.1
>
< HTTP/1.1 204 No Content
< Allow: GET,PUT,PATCH,DELETE
< Content-Length: 0
< Content-Type: application/json
<</code></pre>
</section>
<section>
<h2>Programmers are people too</h2>
<img src="images/data.jpeg">
</section>
<section>
<img src="images/fry.png">
</section>
<section>
<h2>Errors</h2>
<ul>
<li>400 (Bad Request)</li>
<li>401 (Unauthorized)</li>
<li>403 (Forbidden)</li>
<li>404 (Not Found)</li>
<li>405 (Method Not Allowed)</li>
<li>409 (Conflict)</li>
<li>500 (Internal Server Error)</li>
<li>503 (Service Unavailable)</li>
<li>418 (?) <a href="http://tools.ietf.org/html/rfc2324" class="fragment">I'm a teapot</a></li>
</ul>
<pre class="fragment"><code>{
"id": "income_limit"
"description": "Income limit reached."
}</code></pre>
</section>
<section>
<h2>Media Types</h2>
<ul>
<li class="fragment">Format specification and parsing rules</li>
<li class="fragment"><code>application/json</code></li>
<li class="fragment"><code>application/vnd.toshl+json</code></li>
<li class="fragment"><code>application/vnd.toshl.expense+json</code></li>
</ul>
</section>
<section>
<h2>Versioning</h2>
<ul>
<li class="fragment">
<p>URI</p>
<pre><code>https://api.toshl.com/v1/expenses/</code></pre>
</li>
<li class="fragment">
<p>Media Types</p>
<pre><code>application/vnd.toshl.v1+json
application/vnd.toshl.expense.v1+json</code></pre>
</li>
</ul>
</section>
<section>
<h2>Pagination</h2>
<pre><code>https://api.toshl.com/expenses?page=1&per_page=20</code></pre>
<p class="fragment">What about previous/next/last links?</p>
<pre class="fragment"><code>$ curl -v https://api.toshl.com/expenses
> GET / HTTP/1.1
>
< HTTP/1.1 200 OK
< Allow: GET, POST
< Link: <expenses?page=2>; rel="next",<expenses?page=9>; rel="last"</code></pre>
</section>
<section>
<h2>Date & time</h2>
<p>Timestamp <code>2012-10-23T22:05:37+00:00Z</code>. <a href="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a>.</p>
</section>
<section>
<h2>Sync</h2>
<ul>
<li class="fragment"><p>Add <code>?since=1351033818</code></p></li>
<li class="fragment"><p>Add <code>?since_id=37821</code></p></li>
<li class="fragment">
<p>Using <code>Last-Modified</code> and <code>If-Modified-Since</code></p>
<p><a href="http://hribar.info/2012/09/restful-api-with-sync/">RESTful API with sync</a></p>
</li>
<li class="fragment">
<p>Using <code>Last-Modified</code> and <code>?since=2012-10-23T22:05:37+00:00Z</code></p>
</li>
</ul>
</section>
<section>
<h2>Caching</h2>
<ul>
<li class="fragment">Server responds with <code>ETag: "3e12ec4d-994fefe4-24c8868"</code></li>
<li class="fragment">Client can then send <code>If-None-Match: "3e12ec4d-994fefe4-24c8868"</code></li>
<li class="fragment">If not modified, server responds with 304 (Not Modified)</li>
</ul>
</section>
<section>
<h2>Security</h2>
<ul>
<li>Basic auth</li>
<li class="fragment">OAuth 1.0</li>
<li class="fragment">
<p>OAuth 2.0</p>
<pre><code>GET /expenses HTTP/1.1
Host: api.toshl.com
Authorization: Bearer vF9dft4qmT</code></pre>
</li>
<li class="fragment">
<p>OAuth 2.0 + MAC</p>
<pre><code>GET /expenses HTTP/1.1
Host: api.toshl.com
Authorization: MAC id="h480djs93hd8",
nonce="264095:dj83hs9s",
mac="SLDJd4mg43cjQfElUs3Qub4L6xE="</code></pre>
</li>
</ul>
</section>
<!-- Hypermedia apis -->
<section>
<h2>Are we there yet?</h2>
<ul>
<li class="fragment">We expose our objects by type-marshaling/serialization</li>
<li class="fragment">Create special media types for each object</li>
<li class="fragment">Smack a CRUD interface on top of it</li>
<li class="fragment">And call it an API</li>
</ul>
</section>
<section>
<h2>But then</h2>
<ul>
<li class="fragment">Update the API, clients break</li>
<li class="fragment">Update process, rewrite clients</li>
<li class="fragment">Versioning doesn't help</li>
<li class="fragment">Simple data, complex client</li>
<li class="fragment">Shouldn't that be the other way round?</li>
</ul>
</section>
<section>
<h2>Define: HATEOAS (again)</h2>
<blockquote cite="http://en.wikipedia.org/wiki/HATEOAS">
A client interacts with a network application entirely through hypermedia provided dynamically by application servers. A REST client needs no prior knowledge about how to interact with any particular application or server beyond a generic understanding of hypermedia.
</blockquote>
</section>
<section>
<h2>Close but no cigar</h2>
<img src="images/cigar.jpeg">
</section>
<section>
<h2>Standing on the shoulders of giants</h2>
<blockquote>
The WWW is fundamentally a distributed hypermedia application. —Richard Taylor
</blockquote>
<p class="fragment">APIs and clients, they are hiding in plain sight.</p>
<p class="fragment">Websites, HTML and the browser.</p>
</section>
<section>
<img src="images/witch.jpeg">
</section>
<section>
<h2>Explain</h2>
<ul>
<li>Point the browser to a web page</li>
<li class="fragment">Browser executes a <code>GET page</code>.</li>
<li class="fragment">If the user clicks a link, construct a <code>GET uri</code> request.</li>
<li class="fragment">If the user submits a form, construct a <code>POST uri "data"</code> request.</li>
</ul>
</section>
<section>
<h2>Complex data, simple client</h2>
<ul>
<li>Server can update independent from the client</li>
<li class="fragment">Clients don't break</li>
<li class="fragment">Clients don't have to know anything about your data</li>
<li class="fragment">Just how to parse it and present it</li>
<li class="fragment">Execute custom code on the client</li>
</ul>
</section>
<section>
<h2>Why don't we build APIs this way?</h2>
<img src="images/picard.jpeg">
</section>
<section>
<a href="http://shop.oreilly.com/product/0636920021575.do"><img class="noborder" src="images/api-rulebook.jpeg"></a>
</section>
<section>
<h2>Define: WRML</h2>
<p>Web Resource Modeling Language</p>
<blockquote cite="http://www.wrml.org">
WRML, pronounced like "Wormle", is an open source project focused on providing standards, frameworks, and tools that support the development of web-oriented, client-server applications.
</blockquote>
<pre class="fragment"><code>> GET /common/Collection HTTP/1.1 Host: api.schemas.wrml.org
< HTTP/1.1 200 OK
< Content-Type: application/wrml;
< format="http://api.formats.wrml.org/application/json"; schema="http://api.schemas.wrml.org/common/Collection"
{"name" : "Collection", ...}</code></pre>
</section>
<section>
<a href="http://shop.oreilly.com/product/0636920020530.do"><img class="noborder" src="images/hypermedia-apis.jpeg"></a>
</section>
<section>
<h2>Hypermedia APIs</h2>
<ul>
<li class="fragment">Use HTTP correctly</li>
<li class="fragment">Serve hypermedia responses</li>
<li class="fragment">Expose processes not objects</li>
</ul>
</section>
<section class="twocolumn">
<h2>Hypermedia factors</h2>
<img src="images/hypermedia-factors.png">
<ul>
<li>LO Outbound Links</li>
<li>LT Templated Links</li>
<li>LI Idempotent Links</li>
<li>LN Non-Idempotent Links</li>
<li>Embed Links</li>
<li>CU Update Controls</li>
<li>CM Method Controls</li>
<li>CL Link Annotation Controls</li>
</ul>
</section>
<section>
<h2>Mind blown</h2>
<img class="noborder" src="images/mind-blown.jpeg">
</section>
<section>
<h2>Collection+JSON</h2>
<pre><code>// sample collection object
{
"collection" : {
"version" : "1.0",
"href" : URI,
"links" : [ARRAY],
"items : [ARRAY],
"queries" : [ARRAY],
"template" : {OBJECT},
"error" : {OBJECT}
}
}</code></pre>
</section>
<section>
<h2>Conclusion</h2>
<ul>
<li class="fragment">Complex data, simple client</li>
<li class="fragment">Expose your processes not your objects</li>
<li class="fragment">Let your API evolve</li>
<li class="fragment">Build Hypermedia APIs</li>
</ul>
</section>
<!-- End -->
<section>
<img class="noborder" width="500px" src="images/hammertime.jpeg" alt="">
</section>
<section>
<h2>Links</h2>
<ul>
<li>@mihahribar</li>
<li><a href="http://hribar.info/talks/api-state-of-the-art">hribar.info/talks/api-state-of-the-art</a></li>
<li><a href="http://shop.oreilly.com/product/0636920021575.do">REST API Design Rulebook</a>, Mark Masse</li>
<li><a href="http://shop.oreilly.com/product/0636920020530.do">Building Hypermedia APIs with HTML5 and Node</a>, Mike Amundsen</li>
<li><a href="http://norestforjson.blogspot.co.uk/2012/08/json-is-not-restful.html">JSON != RESTful</a></li>
<li><a href="http://blog.steveklabnik.com/posts/2012-02-23-rest-is-over">REST is over</a></li>
<li><a href="http://designinghypermediaapis.com">Designing Hypermedia APIs</a></li>
<li><a href="http://stateless.co/hal_specification.html">HAL – Hypertext Application Language</a></li>
</ul>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/highlight.js', async: true, callback: function() { window.hljs.initHighlightingOnLoad(); } },
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
<script type="text/javascript">
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-1970196-1']); _gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>