on master help sign in
More example polish
fab4c6eGreg Hale
exec/API.hs+2−1
@@ -5,6 +5,7 @@ module API where
5
5
6import Servant.API
6import Servant.API
7
7
8+-- | API spec for server, client, and docs
8−type API = "getunit" :> Get '[JSON] ()
9+type API = "getunit" :> Get '[JSON] ()
9 :<|> "getint" :> Get '[JSON] Int
10 :<|> "getint" :> Get '[JSON] Int
10 :<|> Raw
11 :<|> Raw
exec/Example.hs+19−13
@@ -19,21 +19,27 @@ url = BaseUrl Http "localhost" 8000 ""
19main :: IO ()
19main :: IO ()
20main = mainWidget run
20main = mainWidget run
21
21
22+
22run :: forall t m. MonadWidget t m => m ()
23run :: forall t m. MonadWidget t m => m ()
23run = do
24run = do
25+
26+ -- Name the computed API client functions
27+ let (getUnit :<|> getInt :<|> doRaw) =
24− let (getUnit :<|> getInt :<|> doRaw) = client api (Proxy :: Proxy m) (constDyn url)
28+ client api (Proxy :: Proxy m) (constDyn url)
29+
25− b :: Event t () <- button "Get unit"
30+ unitBtn <- button "Get unit"
26− b' :: Event t () <- button "Get int"
31+ intBtn <- button "Get int"
27− res :: Event t (Maybe (), XhrResponse) <- getUnit b
32+
33+ unitResponse <- getUnit unitBtn
28− res' :: Event t (Maybe Int, XhrResponse) <- getInt b'
34+ intResponse :: Event t (Maybe Int, XhrResponse) <- getInt intBtn
35+
29− score <- foldDyn (+) 0 (fmapMaybe fst res')
36+ score <- foldDyn (+) 0 (fmapMaybe fst intResponse)
37+
38+ r <- holdDyn "Waiting" $
30− r <- holdDyn "Waiting" $ leftmost [fmap (showXhrResponse . snd) res
39+ leftmost [fmap (showXhrResponse . snd) unitResponse
31− ,fmap (showXhrResponse . snd) res'
40+ ,fmap (showXhrResponse . snd) intResponse
32− ]
41+ ]
33− dynText r
34− el "br" $ return ()
35− text "Total: "
36− display score
42+ dynText r >> el "br" (return ()) >> text "Total: " >> display score
37
43
38showXhrResponse :: XhrResponse -> String
44showXhrResponse :: XhrResponse -> String
39showXhrResponse (XhrResponse stat stattxt rbmay rtmay) =
45showXhrResponse (XhrResponse stat stattxt rbmay rtmay) =