]> Git — Sourcephile - haskell/symantic-http.git/blob - symantic-http-demo/API.hs
stack: bump to lts-14.13
[haskell/symantic-http.git] / symantic-http-demo / API.hs
1 {-# LANGUAGE DataKinds #-}
2 {-# LANGUAGE MultiParamTypeClasses #-}
3 {-# LANGUAGE NoMonomorphismRestriction #-}
4 {-# LANGUAGE OverloadedStrings #-}
5 {-# LANGUAGE TypeApplications #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# OPTIONS_GHC -fno-warn-orphans #-}
8 {-# OPTIONS_GHC -Wno-missing-signatures #-}
9 module API where
10 import Text.Read (readMaybe)
11 import qualified Pipes as P
12 import qualified Data.Text.Lazy as TL
13 import qualified Data.Text.Lazy.Encoding as TL
14
15 import Symantic.HTTP
16
17 -- | Define the API, common to the client and server.
18 -- Either use @NoMonomorphismRestriction@ like here,
19 -- or add an argument to 'api',
20 -- to let the compiler infer its (complex) type.
21 -- Read the executables of the client and of the server
22 -- to see how to derive code from this 'api'.
23 api =
24 "succ" </> capture @Integer "n"
25 <.> get @Integer @'[PlainText]
26
27 <!>
28 "countdown" </> capture @Integer "n"
29 <.> getStream @(P.Producer Integer IO ())
30 @'[PlainText]
31 @NewlineFraming
32
33 instance MimeEncodable Integer PlainText where
34 mimeEncode _ = TL.encodeUtf8 . TL.pack . show
35 instance MimeDecodable Integer PlainText where
36 mimeDecode _mt bs =
37 let s = TL.unpack $ TL.decodeUtf8 bs in
38 case readMaybe s of
39 Nothing -> Left $ "cannot parse as Integer: "<>s
40 Just n -> Right n