-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
665 lines (628 loc) · 16.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Servant</title>
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="dist/theme/black.css" id="theme">
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/monokai.css" id="highlight-theme">
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
APIs in Servant
</section>
<section>
<h3>
Who am I?
</h3>
<p>
Owen Harvey
</p>
<p>
Software Developer
</p>
<p>
CSIRO Data61: E&D
</p>
</section>
<section>
<section>
<h4>
Assumed Knowledge
</h4>
<p>
Servant requires a fair degree of comfort with some advanced Haskell concepts.
</p>
</section>
<section>
<p>
Assumptions going into this talk
<ul>
<li>Typeclasses</li>
<li>mtl/transformers</li>
<li>handwaving extensions</li>
</ul>
</p>
</section>
<section>
<p>
Assume this throughout
</p>
<pre><code class="language-haskell" data-trim data-noescape>
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
</code></pre>
</section>
</section>
<section>
<section>
<h4>
What is Servant
</h4>
<p class="fragment" >
Type driven library for writing web clients and servers
</p>
</section>
<section>
<h4>
Why you should care
</h4>
<p>
<ul>
<li>
Composable APIs
</li>
<li>
Client generation from API type
</li>
<li>
Swagger/OpenAPI output (with a util library)
</li>
<li>
Enforced types from top to bottom
</li>
</ul>
</p>
</section>
</section>
<section>
<section>
<h4>
How Servant Works
</h4>
</section>
<section>
<p>
Built around a set of combinators for building an API
</p>
<p>
Path, Query, Body, Method, Headers, etc.
</p>
<p>
</p>
</section>
</section>
<section>
<section>
<h4>
Writing a Servant API
</h4>
</section>
<section>
<pre><code class="language-haskell" data-trim data-noescape>
import Servant ((:>), Get, JSON)
-- create an endpoint at /hello/world
-- GET /hello/world returns a JSON String
type TestAPI = "hello" :> "world" :> Get '[JSON] String
</code></pre>
<p>
:> joins segments into a route
</p>
<p>
'[JSON] defines the encodings we can use.
</p>
<p>
String says we will be returning a String to the user.
</p>
</section>
<section>
<p>
Multiple routes can be defined in a single type using
</p>
<p>
:<|>
</p>
<pre><code class="language-haskell" data-trim data-noescape>
import Servant ((:>), Get, JSON, (:<|>))
type Foo = "foo" :> Get '[JSON] ()
type Bar = "bar" :> Get '[JSON] ()
type Baz = "baz" :> Post '[JSON] ()
-- These are identical
type FooBarAPI = Foo :<|> Bar :<|> Baz
type FooBarAPI =
"foo" :> Get '[JSON] () :<|>
"bar" :> Get '[JSON] () :<|>
"baz" :> Post '[JSON] ()
</code></pre>
</section>
<section>
<p>
Routes with a common root can be combined
</p>
<pre><code class="language-haskell" data-trim data-noescape>
type CommonPrefix = "prefix":> (Foo :<|> Bar :<|> Baz)
</code></pre>
<p class="fragment">
This is especially useful for handling authentication
</p>
</section>
</section>
<section>
<section>
<h4>Anatomy of a Servant server</h4>
</section>
<section>
<p>
Servant servers are usually split into 3 broad sections
</p>
<ul class="fragment">
<li>API type</li>
<li>Server function mapping</li>
<li>Application types and code</li>
</ul>
</section>
<section>
<h4>
The API Type
</h4>
<p>
What we looked at before
</p>
<ul>
<li>Routes</li>
<li>Path and Query Names</li>
<li>Request Body</li>
<li>Encodings</li>
<li>Methods</li>
<li>Headers</li>
</ul>
</section>
<section>
<h4>Routes</h4>
<p>
Defined using the combinator :>
</p>
<p>
Joined by :<|> to form a larger API
</p>
</section>
<section>
<h4>Path and Query Names</h4>
<p>
Capture and QueryParam
</p>
<p>
Capture "name" Type : Required value
</p>
<p>
QueryParam "name" Type : Optional Value
</p>
</section>
<section>
<h4>Request Body</h4>
<p>
ReqBody '[Encodings] Type
</p>
<p>
Takes a list of acceptable encodings and a type to decode to
</p>
</section>
<section>
<h4>Encodings</h4>
<p>
Represent the Accept and Content-Type headers
</p>
<p>
Defined in lists of formats passed to other combinators
</p>
<p>
'[JSON, PlainText, OctetStream]
</p>
</section>
<section>
<h4>Encodings</h4>
<p>
Heavily dependent on typeclasses to handle encodings
</p>
<p>
Most basic types are handled for you
</p>
<p>
Some encodings can be derived by the compiler or provided by hand
</p>
</section>
<section>
<h4>Encodings</h4>
<p>
<dl>
<dt>JSON</dt>
<dd>ToJSON a / FromJSON a</dd>
<dt>PlainText</dt>
<dd>String and Text</dd>
<dt>FormUrlEncoded</dt>
<dd>FromForm a / ToForm a</dd>
<dt>OctetStream</dt>
<dd>ByteString</dd>
</dl>
</p>
</section>
<section>
<h4>Acceptable Methods</h4>
<p>
Methods for a route mirror the HTTP verb
</p>
<p>
Get, Put, Delete, GetNoContent
</p>
<p>
Takes both a set of response encodings and a type for the content
</p>
</section>
<section>
<h4>Request Headers</h4>
<p>
Header "name" Type
</p>
<p>
Captures an optional header from a request
</p>
</section>
<section>
<h4>Response Headers</h4>
<p>
Wrapper over the route response type
</p>
<p>
Headers '[Header "a" Type] ResponseType
</p>
<p>
Can defined multiple headers at once
</p>
</section>
<section>
<h4>Server Function Mapping</h4>
<p>
Telling servant what to run
</p>
<p>
Uses the same combinators as the type definition
</p>
</section>
<section>
<pre><code class="language-haskell" data-trim data-noescape>
type FooBarAPI =
"foo" :> Capture "x" Bool :> Get '[JSON] String :<|>
"bar" :> Get '[JSON] Integer :<|>
"baz" :> ReqBody '[JSON] [String] :> Post '[JSON] [String]
foo :: MonadIO m => Bool -> m String
bar :: MonadIO m => m Integer
baz :: MonadIO m => [String] -> m [String]
fooBarServer :: MonadIO m => ServerT FooBarAPI m
fooBarServer = foo :<|> bar :<|> baz
</code></pre>
</section>
<section>
<p>
Rules of Thumb
</p>
<p>
Each top level type joined by :<|> requires a function
</p>
<p>
Values are passed in the order they are defined
</p>
<p>
Must all return the same `m`
</p>
</section>
<section>
<h4>
Running a server
</h4>
<p>
servant-server is designed around the WAI specification
</p>
<p>
We'll be using the Warp server in this talk
</p>
</section>
<section>
<pre><code class="language-haskell" data-trim data-noescape>
serve :: HasServer api '[] =>
Proxy api -> ServerT api Handler -> Application
</code></pre>
<p>
turns an API and a server into a runnable Application
</p>
<p>
Handler is a monad that gives servant the functionality it needs to run requests
</p>
</section>
<section>
<p>
Sometimes Handler is undesirable
<ul>
<li>IO free application code</li>
<li>Value passing with ReaderT</li>
</ul>
</p>
<pre><code class="language-haskell" data-trim data-noescape>
hoistServer :: HasServer api '[] =>
Proxy api -> (forall x. m x -> n x) ->
ServerT api m -> ServerT api n
</code></pre>
<p>
usually converting
<pre><code class="language-haskell" data-trim data-noescape>
ServerT api m -> ServerT api Handler
</code></pre>
</p>
</section>
<section>
<pre><code class="language-haskell" data-trim data-noescape>
type FooBarAPI =
"foo" :> Capture "x" Bool :> Get '[JSON] String :<|>
"bar" :> Get '[JSON] Integer :<|>
"baz" :> ReqBody '[JSON] [String] :> Post '[JSON] [String]
fooBarAPI :: Proxy FooBarAPI
foo :: MonadIO m => Bool -> m String
bar :: MonadIO m => m Integer
baz :: MonadIO m => [String] -> m [String]
fooBarServer :: MonadIO m => ServerT FooBarAPI m
app = serve fooBarAPI fooBarServer
main = Network.Wai.Handler.Warp.run 8080 app
</code></pre>
</section>
</section>
<section>
<section>
<h4>Servant Clients</h4>
</section>
<section>
<p>
Clients are split into two parts
</p>
<ul>
<li>API type</li>
<li>ClientM functions</li>
</ul>
</section>
<section>
<p>
API clients are built around ClientM and runClientM
</p>
<p class="fragment">
ClientM represents a request and all that entails
</p>
<p class="fragment">
runClientM executes a request and returns results
</p>
</section>
<section>
<p>
Like servers, functions are extracted by patterns
</p>
<pre><code class="language-haskell" data-trim data-noescape>
type FooBarAPI =
"foo" :> Capture "x" Bool :> Get '[JSON] String :<|>
"bar" :> Get '[JSON] Integer :<|>
"baz" :> ReqBody '[JSON] [String] :> Post '[JSON] [String]
data ApiCalls = ApiCalls
{ foo :: Bool -> ClientM String
, bar :: ClientM Integer
, baz :: [String] -> ClientM [String]
}
apiCalls = ApiCalls foo bar
where
foo :<|> bar :<|> baz = client (Proxy :: Proxy FooBarAPI)
</code></pre>
</section>
<section>
<p>
runClientM needs a ClientEnv
</p>
<p>
ClientEnv contains basic request data
<ul>
<li>Host</li>
<li>Cookies</li>
<li>TLS settings</li>
<li>Timeouts</li>
</ul>
</p>
</section>
<section>
<pre><code class="language-haskell" data-trim data-noescape>
clientEnv :: IO ClientEnv
clientEnv = do
manager <- newManager defaultManagerSettings
let baseUrl = BaseUrl Http "localhost" 8080 ""
pure $ mkClientEnv manager baseUrl
</code></pre>
<p class="fragment">
Putting the ClientEnv into a ReaderT is very helpful
</p>
</section>
<section>
<pre><code class="language-haskell" data-trim data-noescape>
type AppM m a = ReaderT ClientEnv (ExceptT ClientError m) a
getFoo :: MonadIO m => AppM m String
getFoo = do
env <- ask
res <- liftIO $ runClientM (foo apiCalls True) env
either throwError pure res
getBar :: MonadIO m => AppM m Integer
postBaz :: MonadIO m => AppM m [String]
someAppFunction :: MonadIO m => AppM m (String, ...)
main = do
env <- clientEnv
res <- runExceptT $ runReaderT someAppFunction clientEnv
either throwError pure res
</code></pre>
</section>
</section>
<section>
<section>
<h4>
Error handling
</h4>
</section>
<section>
<p>
MonadError and ExceptT are your friends
</p>
<p class="fragment">
Servers use ServerError<br/>
Clients use ClientError
</p>
<p class="fragment">
Keeps clients and servers seperate
</p>
</section>
<section>
<p>
Server error handling
</p>
<p>
Application error types can be mapped using withExceptT
</p>
<pre><code class="language-haskell" data-trim data-noescape>
data AppError
= ErrorA ByteString
| ErrorB Int
type AppM a = ExceptT AppError IO a
toServantError :: AppError -> ServerError
toServantError e = case e of
ErrorA s -> err400 { errBody = s }
ErrorB i -> err500 { errBody = pack $ show i }
toHandler :: AppM a -> Handler a
toHandler = Handler . withExceptT toServantError
</code></pre>
</section>
<section>
<p>Client error handling</p>
<pre><code class="language-haskell" data-trim data-noescape>
toAppError :: ClientError -> AppError
toAppError (ConnectionError e) = AppException e
...
type AppM a = ExceptT AppError IO a
mapError :: ClientM a -> AppM a
mapError client = do
e <- runClientM client env
either
(throwError . toAppError)
pure
e
</code></pre>
</section>
</section>
<section>
<section>
<h4>Swagger</h4>
</section>
<section>
<p>
Automatic Swagger/OpenAPI generation
</p>
<p>
Completely type driven
</p>
</section>
<section>
<pre><code class="language-haskell" data-trim data-noescape>
module Main where
import Data.Aeson
import Data.ByteString.Lazy.Char8
import Data.Proxy
import Servant
import Servant.Swagger
type FooBarAPI =
"foo" :> Capture "x" Bool :> Get '[JSON] String :<|>
"bar" :> Get '[JSON] Integer :<|>
"baz" :> ReqBody '[JSON] [String] :> Post '[JSON] [String]
main = putStrLn . encode $ toSwagger @(FooBarAPI) Proxy
</code></pre>
</section>
</section>
<section>
<section>
<h4>
Other Cool Things
</h4>
</section>
<section>
<h4>JS client generation</h4>
<p>
<pre><code class="language-haskell" data-trim data-noescape>
apiJS :: Text
apiJS = jsForAPI api vanillaJS
</code></pre>
apiJS will contain the generated code
<pre><code class="language-haskell" data-trim data-noescape>
var getPoint = function(onSuccess, onError)
{
var xhr = new XMLHttpRequest();
xhr.open('GET', '/point', true);
...
</code></pre>
This works with jQuery, vanillaJS (XMLHttpRequest), Axios, and Angular
</p>
</section>
</section>
<section>
<section>
<h4>Links</h4>
<dl>
<dt>Servant</dt>
<dd>docs.servant.dev</dd>
<dt>Hackage</dt>
<dd>
hackage.haskell.org/package/servant<br/>
hackage.haskell.org/package/servant-server<br/>
hackage.haskell.org/package/servant-client<br/>
hackage.haskell.org/package/servant-swagger<br/>
hackage.haskell.org/package/servant-js
</dd>
<dt>Slides</dt>
<dd>github.com/lepsa/servant-talk</dd>
</dl>
</section>
</section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script>
// More info about initialization & config:
// - https://revealjs.com/initialization/
// - https://revealjs.com/config/
Reveal.initialize({
hash: true,
// Learn about plugins: https://revealjs.com/plugins/
plugins: [ RevealMarkdown, RevealHighlight, RevealNotes ]
});
</script>
</body>
</html>