git.iko.soy/typeable/servant-reflex
on master help sign in
Misc WIP
commitf837f06cc43f783226ee973f037afd5f5ca94968 authorDoug Beardsley <mightybyte@gmail.com> dateThu 18 Feb 2016, 05:12 (11 years ago) parentffec9a3
11 files changed+58−53
.gitmodules+3−3 build.sh+1−1 deps/reflex-dom-contrib+1−1 deps/reflex-platform+1 deps/try-reflex−1 exec/API.hs+2−1 exec/Example.hs+2−2 overrides.nix+3−4 servant-reflex.cabal+8−4 shell.sh+1−1 src/Servant/Reflex.hs+36−35
.gitmodules+3−3
@@ -1,9 +1,6 @@
11[submodule "deps/reflex-dom-contrib"]
22 path = deps/reflex-dom-contrib
33 url = git@github.com:reflex-frp/reflex-dom-contrib.git
4−[submodule "deps/try-reflex"]
5− path = deps/try-reflex
6− url = git@github.com:ryantrinkle/try-reflex.git
74[submodule "deps/servant"]
85 path = deps/servant
96 url = https://github.com/haskell-servant/servant.git
@@ -16,3 +13,6 @@
1613[submodule "deps/reflex"]
1714 path = deps/reflex
1815 url = https://github.com/ryantrinkle/reflex
16+[submodule "deps/reflex-platform"]
17+ path = deps/reflex-platform
18+ url = git@github.com:reflex-frp/reflex-platform.git
build.sh+1−1
@@ -1,3 +1,3 @@
11#!/bin/sh
22
3−deps/try-reflex/work-on ./overrides.nix ./. --run "cabal configure --ghcjs ; cabal build"
3+deps/reflex-platform/work-on ./overrides.nix ./. --run "cabal configure --ghcjs ; cabal build"
deps/reflex-dom-contrib+1−1

Submodule changed.

deps/reflex-platformnew file+1

Submodule changed.

deps/try-reflexdeleted−1

Submodule changed.

exec/API.hs+2−1
@@ -5,4 +5,5 @@ module API where
55
66import Servant.API
77
8−type API = Get '[JSON] () :<|> Get '[JSON] Int
8+--type API = Get '[JSON] () :<|> Get '[JSON] Int
9+type API = Get '[JSON] ()
exec/Example.hs+2−2
@@ -3,10 +3,10 @@
33
44module Main where
55
6−import Servant.Reflex
76import API
87import Data.Proxy
98import Reflex.Dom
9+import Servant.Reflex
1010
1111api :: Proxy API
1212api = Proxy
@@ -19,7 +19,7 @@ main = mainWidget run
1919
2020run :: forall t m. MonadWidget t m => m ()
2121run = do
22− let (getUnit :: Event t () -> m (Event t ((),()))) = client api url
22+ let getUnit = clientWithRoute api defReq url
2323 b :: Event t () <- button "Get unit"
2424 res :: Event t ((),()) <- getUnit b
2525 c <- foldDyn (\_ (n :: Int) -> succ n) 0 res
overrides.nix+3−4
@@ -1,11 +1,10 @@
1−{}:
1+{ try-reflex, reflex-platform }:
22let
33 pkgs = import <nixpkgs> {};
4− try-reflex = import deps/try-reflex {};
5−in try-reflex.ghcjs.override {
4+in reflex-platform.ghcjs.override {
65 overrides = self: super: {
76 self.lens = pkgs.haskell.lib.dontCheck (self.lens);
87 reflex-dom-contrib = pkgs.haskell.lib.dontCheck (self.callPackage deps/reflex-dom-contrib {});
9− servant = self.callPackage (try-reflex.cabal2nixResult deps/servant/servant) {};
8+ servant = self.callPackage (reflex-platform.cabal2nixResult deps/servant/servant) {};
109 };
1110}
servant-reflex.cabal+8−4
@@ -30,7 +30,7 @@ library
3030 ghcjs-dom >= 0.2 && < 0.3,
3131 http-api-data >= 0.2 && < 0.3,
3232 http-media >= 0.6 && < 0.7,
33− http-types >= 0.8 && < 0.9,
33+ http-types >= 0.8 && < 0.10,
3434 mtl,
3535 network-uri >= 2.6 && < 2.7,
3636 reflex,
@@ -47,9 +47,13 @@ library
4747 ghc-options: -threaded -Wall -fwarn-tabs -funbox-strict-fields -O2
4848
4949executable example
50− build-depends: reflex, servant-reflex, base, servant, reflex-dom
50+ hs-source-dirs: exec
51+ build-depends:
52+ base,
53+ reflex,
54+ reflex-dom,
55+ servant,
56+ servant-reflex
5157 default-language: Haskell2010
5258 main-is: Example.hs
5359 other-modules: API
54− hs-source-dirs: exec
55−
shell.sh+1−1
@@ -1,3 +1,3 @@
11#!/bin/sh
22
3−deps/try-reflex/work-on ./overrides.nix ./.
3+deps/reflex-platform/work-on ./overrides.nix ./.
src/Servant/Reflex.hs+36−35
@@ -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,