git.iko.soy/typeable/servant-reflex
on master help sign in
More example polish
commitfab4c6e2f7b1ddd4131e82d337483798a4c93418 authorGreg Hale <imalsogreg@gmail.com> dateThu 18 Feb 2016, 18:53 (11 years ago) parentfda2a9c
2 files changed+21−14
exec/API.hs+2−1 exec/Example.hs+19−13
exec/API.hs+2−1
@@ -5,6 +5,7 @@ module API where
55
66import Servant.API
77
8−type API = "getunit" :> Get '[JSON] ()
8+-- | API spec for server, client, and docs
9+type API = "getunit" :> Get '[JSON] ()
910 :<|> "getint" :> Get '[JSON] Int
1011 :<|> Raw
exec/Example.hs+19−13
@@ -19,21 +19,27 @@ url = BaseUrl Http "localhost" 8000 ""
1919main :: IO ()
2020main = mainWidget run
2121
22+
2223run :: forall t m. MonadWidget t m => m ()
2324run = do
24− let (getUnit :<|> getInt :<|> doRaw) = client api (Proxy :: Proxy m) (constDyn url)
25− b :: Event t () <- button "Get unit"
26− b' :: Event t () <- button "Get int"
27− res :: Event t (Maybe (), XhrResponse) <- getUnit b
28− res' :: Event t (Maybe Int, XhrResponse) <- getInt b'
29− score <- foldDyn (+) 0 (fmapMaybe fst res')
30− r <- holdDyn "Waiting" $ leftmost [fmap (showXhrResponse . snd) res
31− ,fmap (showXhrResponse . snd) res'
32− ]
33− dynText r
34− el "br" $ return ()
35− text "Total: "
36− display score
25+
26+ -- Name the computed API client functions
27+ let (getUnit :<|> getInt :<|> doRaw) =
28+ client api (Proxy :: Proxy m) (constDyn url)
29+
30+ unitBtn <- button "Get unit"
31+ intBtn <- button "Get int"
32+
33+ unitResponse <- getUnit unitBtn
34+ intResponse :: Event t (Maybe Int, XhrResponse) <- getInt intBtn
35+
36+ score <- foldDyn (+) 0 (fmapMaybe fst intResponse)
37+
38+ r <- holdDyn "Waiting" $
39+ leftmost [fmap (showXhrResponse . snd) unitResponse
40+ ,fmap (showXhrResponse . snd) intResponse
41+ ]
42+ dynText r >> el "br" (return ()) >> text "Total: " >> display score
3743
3844showXhrResponse :: XhrResponse -> String
3945showXhrResponse (XhrResponse stat stattxt rbmay rtmay) =