git.iko.soy/typeable/servant-reflex
on master help sign in
Modify types to allow accumulating Behaviors during capture
commit34c01329655f3643d2f4c973148311f967492cfb authorGreg Hale <imalsogreg@gmail.com> dateMon 8 Feb 2016, 08:50 (11 years ago) parent587c70c
2 files changed+23−23
src/Servant/Common/Req.hs+5−5 src/Servant/Reflex.hs+18−18
src/Servant/Common/Req.hs+5−5
@@ -57,8 +57,8 @@ data ServantError
5757
5858instance Exception ServantError
5959
60−data Req = Req
61− { reqPath :: String
60+data Req t = Req
61+ { reqPathParts :: [Behavior t String]
6262 , qs :: QueryText
6363 , reqBody :: Maybe (ByteString, MediaType)
6464 , reqAccept :: [MediaType]
@@ -68,9 +68,9 @@ data Req = Req
6868defReq :: Req
6969defReq = Req "" [] Nothing [] []
7070
71−appendToPath :: String -> Req -> Req
72−appendToPath p req =
73− req { reqPath = reqPath req ++ "/" ++ p }
71+prependToPathParts :: Reflex t => Behavior t String -> Req -> Req
72+prependToPathParts p req =
73+ req { reqPathParts = p : reqPathParts req }
7474
7575appendToQueryString :: Text -- ^ param name
7676 -> Maybe Text -- ^ param value
src/Servant/Reflex.hs+18−18
@@ -9,6 +9,7 @@
99{-# LANGUAGE TypeFamilies #-}
1010{-# LANGUAGE TypeOperators #-}
1111{-# LANGUAGE UndecidableInstances #-}
12+{-# LANGUAGE MultiParamTypeClasses #-}
1213
1314#include "overlapping-compat.h"
1415-- | This module provides 'client' which can automatically generate
@@ -50,7 +51,7 @@ import Reflex.Dom.Xhr
5051-- > myApi = Proxy
5152-- >
5253-- > getAllBooks :: Event t () -> m (Event t (Either XhrError ((),[Book])))
53−-- > postNewBook :: Behavior t (Maybe Book) -> Event t ()
54+-- > postNewBook :: Behavior t (Maybe Book) -> Event t ()
5455-- -> m (Event t (Either XhrError (Book,Book)))
5556-- > (getAllBooks :<|> postNewBook) = client myApi host
5657-- > where host = constDyn $ BaseUrl Http "localhost" 8080
@@ -60,41 +61,40 @@ client p baseurl = clientWithRoute p defReq baseurl
6061-- | This class lets us define how each API combinator
6162-- influences the creation of an HTTP request. It's mostly
6263-- an internal class, you can just use 'client'.
63−class HasClient layout where
64− type Client layout :: *
65− clientWithRoute :: Proxy layout -> Req -> BaseUrl -> Client layout
64+class HasClient t layout where
65+ type Client t layout :: *
66+ clientWithRoute :: Proxy layout -> Req t -> BaseUrl -> Client t layout
6667
6768
68−instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
69− type Client (a :<|> b) = Client a :<|> Client b
69+instance (HasClient t a, HasClient t b) => HasClient t (a :<|> b) where
70+ type Client t (a :<|> b) = Client t a :<|> Client t b
7071 clientWithRoute Proxy req baseurl manager =
71− clientWithRoute (Proxy :: Proxy a) req baseurl manager :<|>
72− clientWithRoute (Proxy :: Proxy b) req baseurl manager
72+ clientWithRoute (Proxy :: Proxy a) req baseurl :<|>
73+ clientWithRoute (Proxy :: Proxy b) req baseurl
7374
7475-- Capture. Example:
7576-- > type MyApi = "books" :> Capture "isbn" Text :> Get '[JSON] Book
7677-- >
7778-- > myApi :: Proxy MyApi = Proxy
7879-- >
79−-- > getBook :: MonadWidget t m
80+-- > getBook :: MonadWidget t m
8081-- => Dynamic t BaseUrl
81−-- -> Behavior t (Maybe Text)
82+-- -> Behavior t (Maybe Text)
8283-- -> Event t ()
8384-- -> m (Event t (Either XhrError (Text, Book)))
8485-- > getBook = client myApi (constDyn host)
85−instance (KnownSymbol capture, ToHttpApiData a, HasClient sublayout)
86− => HasClient (Capture capture a :> sublayout) where
86+instance (Reflex t, KnownSymbol capture, ToHttpApiData a, HasClient t sublayout)
87+ => HasClient t (Capture capture a :> sublayout) where
8788
88− type Client (Capture capture a :> sublayout) =
89− a -> Client sublayout
89+ type Client t (Capture capture a :> sublayout) =
90+ Behavior t (Maybe a) -> Client t sublayout
9091
91− clientWithRoute Proxy req baseurl manager val =
92+ clientWithRoute Proxy req baseurl val =
9293 clientWithRoute (Proxy :: Proxy sublayout)
93− (appendToPath p req)
94+ (appendToPathParts p req)
9495 baseurl
95− manager
9696
97− where p = unpack (toUrlPiece val)
97+ where p = fmap (unpack . toUrlPiece) val
9898
9999-- VERB (Returning content) --
100100instance OVERLAPPABLE_