@@ -16,35 +16,25 @@
1616-- querying functions for each endpoint just from the type representing your
1717-- API.
1818module Servant.Reflex
19− ( client
20− , HasReflexClient(..)
19+ ( HasReflexClient(..)
2120 , ServantError(..)
2221 , module Servant.Common.BaseUrl
22+ , module Servant.Common.Req
2323 ) where
2424
2525#if !MIN_VERSION_base(4,8,0)
2626import Control.Applicative ((<$>))
2727#endif
28−import Control.Monad
2928import Control.Monad.Trans (liftIO)
30−import Control.Monad.Trans.Except
31−import Data.ByteString.Lazy (ByteString)
3229import Data.Default
33−import Data.List
3430import Data.Proxy
35−import Data.String.Conversions
36−import Data.Text (unpack)
3731import GHC.TypeLits
38−import Network.HTTP.Media
39−import qualified Network.HTTP.Types as H
40−import qualified Network.HTTP.Types.Header as HTTP
4132import Servant.API
4233import Servant.Common.BaseUrl
4334import Servant.Common.Req
4435import Reflex
4536import Reflex.Dom
46−import Web.HttpApiData
47−import Reflex.Dom.Xhr
37+--import Reflex.Dom.Xhr
4838
4939-- * Accessing APIs as a Client
5040
@@ -60,17 +50,18 @@ import Reflex.Dom.Xhr
6050-- > postNewBook :: Book -> ExceptT String IO Book
6151-- > (getAllBooks :<|> postNewBook) = client myApi host
6252-- > where host = BaseUrl Http "localhost" 8080
63−client :: HasReflexClient layout
64− => Proxy layout
65− -> BaseUrl
66− -> Client (Input layout) (Output layout)
67−client p baseurl = clientWithRoute p defReq baseurl
53+--client :: HasReflexClient layout
54+-- => Proxy layout
55+-- -> BaseUrl
56+-- -> Client (Input layout) (Output layout)
57+--client p baseurl = clientWithRoute p defReq baseurl
6858
6959data a ::> b = a ::> b deriving (Eq,Ord,Show,Read)
7060
7161infixr 3 ::>
7262
73−type Client ins outs = MonadWidget t m => Event t ins -> m (Event t (ins,outs))
63+--type SingleClient ins outs = MonadWidget t m => Event t ins -> m (Event t (ins,outs))
64+--type SingleClient a = MonadWidget t m => Event t (Input a) -> m (Event t (Input a,Output a))
7465
7566-- | This class lets us define how each API combinator
7667-- influences the creation of an HTTP request. It's mostly
@@ -78,11 +69,21 @@ type Client ins outs = MonadWidget t m => Event t ins -> m (Event t (ins,outs))
7869class HasReflexClient layout where
7970 type Input layout :: *
8071 type Output layout :: *
81− -- clientWithRoute :: MonadWidget t m => Proxy layout -> Req -> BaseUrl
82− -- -> Event t (Input layout) -> m (Event t (Input layout, Output layout))
83− clientWithRoute :: MonadWidget t m
84− => Proxy layout -> Req -> BaseUrl
85− -> Event t (Input layout) -> m (Event t (Input layout, Output layout))
72+ clientWithRoute
73+ :: MonadWidget t m
74+ => Proxy layout
75+ -> Req
76+ -> BaseUrl
77+ -> Event t (Input layout)
78+ -> m (Event t (Input layout, Output layout))
79+
80+class HasReflexClients layout where
81+ type Clients layout :: *
82+ clientsWithRoute
83+ :: Proxy layout
84+ -> Req
85+ -> BaseUrl
86+ -> Clients layout
8687
8788
8889-- | If you have a 'Get' endpoint in your API, the client
@@ -97,10 +98,11 @@ instance
9798 type Input (Get (ct ': cts) result) = ()
9899 type Output (Get (ct ': cts) result) = result
99100 -- type Client (Get (ct ': cts) result) = MonadWidget t m => Event t (Input layout) -> m (Event t result)
100− clientWithRoute Proxy req baseurl trigEvents = undefined -- performAJAX requestBuilder responseParser
101−-- where
102−-- requestBuilder _ = XhrRequest "GET" (showBaseUrl baseurl) def
103−-- responseParser xhrResp = undefined
101+ clientWithRoute Proxy req baseurl trigEvents =
102+ performAJAX requestBuilder responseParser trigEvents
103+ where
104+ requestBuilder _ = XhrRequest "GET" (showBaseUrl baseurl) def
105+ responseParser xhrResp = undefined
104106
105107performAJAX
106108 :: (MonadWidget t m)
@@ -156,7 +158,7 @@ instance
156158-- return $ Headers { getResponse = resp
157159-- , getHeadersHList = buildHeadersTo hdrs
158160-- }
159−--
161+
160162-- | A client querying function for @a ':<|>' b@ will actually hand you
161163-- one function for querying @a@ and another one for querying @b@,
162164-- stitching them together with ':<|>', which really is just like a pair.
@@ -171,12 +173,11 @@ instance
171173-- > postNewBook :: Book -> ExceptT String IO Book
172174-- > (getAllBooks :<|> postNewBook) = client myApi host
173175-- > where host = BaseUrl Http "localhost" 8080
174−instance (HasReflexClient a, HasReflexClient b) => HasReflexClient (a :<|> b) where
175− type Input (a :<|> b) = Input a :<|> Input b
176− type Output (a :<|> b) = Output a :<|> Output b
177− clientWithRoute Proxy req baseurl (a :<|> b) = undefined
178−-- clientWithRoute (Proxy :: Proxy a) req baseurl a :<|>
179−-- clientWithRoute (Proxy :: Proxy b) req baseurl b
176+--instance (HasReflexClient a, HasReflexClient b) => HasReflexClients (a :<|> b) where
177+-- type Clients (a :<|> b) = SingleClient a :<|> SingleClient b
178+-- clientsWithRoute Proxy req baseurl (a :<|> b) =
179+-- clientWithRoute (Proxy :: Proxy a) req baseurl :<|>
180+-- clientWithRoute (Proxy :: Proxy b) req baseurl
180181
181182-- ------------------------------------------------------------------------------
182183-- -- | If you use a 'Capture' in one of your endpoints in your API,