git.iko.soy/typeable/servant-reflex
on master help sign in
Response parsing
commit59bc4fe7838dcb9f1ba53e31b3b54b14825bf512 authorDoug Beardsley <mightybyte@gmail.com> dateFri 19 Feb 2016, 15:19 (11 years ago) parentf837f06
5 files changed+28−16
exec/API.hs+1−1 exec/Example.hs+5−5 servant-reflex.cabal+1 src/Servant/Reflex.hs+14−6 testserver/Main.hs+7−4
exec/API.hs+1−1
@@ -6,4 +6,4 @@ module API where
66import Servant.API
77
88--type API = Get '[JSON] () :<|> Get '[JSON] Int
9−type API = Get '[JSON] ()
9+type API = Get '[JSON] Int
exec/Example.hs+5−5
@@ -12,16 +12,16 @@ api :: Proxy API
1212api = Proxy
1313
1414url :: BaseUrl
15−url = BaseUrl Http "localhost" 8000 ""
15+url = BaseUrl Http "localhost" 8000 "api"
1616
1717main :: IO ()
1818main = mainWidget run
1919
2020run :: forall t m. MonadWidget t m => m ()
2121run = do
22− let getUnit = clientWithRoute api defReq url
23− b :: Event t () <- button "Get unit"
24− res :: Event t ((),()) <- getUnit b
25− c <- foldDyn (\_ (n :: Int) -> succ n) 0 res
22+ let getInt = clientWithRoute api defReq url
23+ b :: Event t () <- button "Get int"
24+ res :: Event t ((),Int) <- getInt b
25+ c <- foldDyn (\n accum -> accum + snd n) 0 $ traceEvent "res" res
2626 display c
2727
servant-reflex.cabal+1
@@ -33,6 +33,7 @@ library
3333 http-types >= 0.8 && < 0.10,
3434 mtl,
3535 network-uri >= 2.6 && < 2.7,
36+ readable,
3637 reflex,
3738 reflex-dom,
3839 safe,
src/Servant/Reflex.hs+14−6
@@ -5,6 +5,7 @@
55{-# LANGUAGE InstanceSigs #-}
66{-# LANGUAGE OverloadedStrings #-}
77{-# LANGUAGE ScopedTypeVariables #-}
8+{-# LANGUAGE TupleSections #-}
89{-# LANGUAGE TypeFamilies #-}
910{-# LANGUAGE TypeOperators #-}
1011{-# LANGUAGE UndecidableInstances #-}
@@ -28,6 +29,7 @@ import Control.Applicative ((<$>))
2829import Control.Monad.Trans (liftIO)
2930import Data.Default
3031import Data.Proxy
32+import Data.String.Conversions
3133import GHC.TypeLits
3234import Servant.API
3335import Servant.Common.BaseUrl
@@ -102,21 +104,27 @@ instance
102104 performAJAX requestBuilder responseParser trigEvents
103105 where
104106 requestBuilder _ = XhrRequest "GET" (showBaseUrl baseurl) def
105− responseParser xhrResp = undefined
107+ responseParser xhrResp =
108+ hush . mimeUnrender (Proxy :: Proxy ct) =<<
109+ (cs <$> _xhrResponse_responseText xhrResp)
110+
111+hush (Left _) = Nothing
112+hush (Right a) = Just a
106113
107114performAJAX
108115 :: (MonadWidget t m)
109116 => (a -> XhrRequest)
110117 -- ^ Function to build the request
111− -> (XhrResponse -> b)
118+ -> (XhrResponse -> Maybe b)
112119 -- ^ Function to parse the response
113120 -> Event t a
114121 -> m (Event t (a, b))
115−performAJAX mkRequest parseResponse req =
116− performEventAsync $ ffor req $ \a cb -> do
122+performAJAX mkRequest parseResponse req = do
123+ e <- performEventAsync $ ffor req $ \a cb -> do
117124 _ <- newXMLHttpRequest (mkRequest a) $ \response ->
118− liftIO $ cb (a, parseResponse response)
125+ liftIO $ cb $ (a,) <$> parseResponse response
119126 return ()
127+ return $ fmapMaybe id e
120128
121129
122130instance
@@ -131,7 +139,7 @@ instance
131139 performAJAX requestBuilder responseParser trigEvents
132140 where
133141 requestBuilder _ = XhrRequest "GET" (showBaseUrl baseurl) def
134− responseParser _ = ()
142+ responseParser _ = Just ()
135143
136144-- -- | Pick a 'Method' and specify where the server you want to query is. You get
137145-- -- back the full `Response`.
testserver/Main.hs+7−4
@@ -14,8 +14,8 @@ import Snap.Http.Server
1414import Snap.Core
1515import Servant.Server.Internal.SnapShims
1616
17−import Servant
18−import Servant.Server
17+import Servant hiding (route)
18+import Servant.Server hiding (route)
1919import API
2020
2121-- * Example
@@ -37,7 +37,7 @@ testApi = Proxy
3737--
3838-- Each handler runs in the 'ExceptT ServantErr IO' monad.
3939server :: Server API Snap
40−server = return () :<|> return 100
40+server = return 100
4141
4242 where helloH name Nothing = helloH name (Just False)
4343 helloH name (Just False) = return . Greet $ "Hello, " <> name
@@ -54,4 +54,7 @@ test = serve testApi server
5454
5555-- Put this all to work!
5656main :: IO ()
57−main = quickHttpServe $ applicationToSnap (serve testApi server)
57+main = quickHttpServe $ route
58+ [ ("api", applicationToSnap (serve testApi server))
59+ , ("", serveDirectory "example.jsexe")
60+ ]