on master help sign in
add request body support, works very much like captures
c1a5a3aAlp Mestanogullari
src/Servant.hs+18
@@ -8,6 +8,7 @@
8{-# LANGUAGE ScopedTypeVariables #-}
8{-# LANGUAGE ScopedTypeVariables #-}
9module Servant where
9module Servant where
10
10
11+import Control.Applicative
11import Data.Aeson
12import Data.Aeson
12import Data.Monoid
13import Data.Monoid
13import Data.Proxy
14import Data.Proxy
@@ -18,6 +19,7 @@ import Network.Wai
18import Network.Wai.Handler.Warp
19import Network.Wai.Handler.Warp
19import Soenke
20import Soenke
20
21
22+-- * Captures
21data Capture sym a
23data Capture sym a
22
24
23class FromText a where
25class FromText a where
@@ -47,6 +49,22 @@ instance (KnownSymbol capture, FromText a, HasServer sublayout)
47 }
49 }
48 _ -> return Nothing
50 _ -> return Nothing
49
51
52+-- * Request Body support
53+data RQBody a
54+
55+instance (FromJSON a, HasServer sublayout)
56+ => HasServer (RQBody a :> sublayout) where
57+
58+ -- same caveat as for the Captures...
59+ type Server (RQBody a :> sublayout) =
60+ RQBody a :> (a -> Server sublayout)
61+
62+ route Proxy (rqbody :> subserver) request = do
63+ mrqbody <- decode' <$> lazyRequestBody request
64+ case mrqbody of
65+ Nothing -> return Nothing
66+ Just v -> route (Proxy :: Proxy sublayout) (subserver v) request
67+
50-- * Example
68-- * Example
51
69
52type TestApi = "hello" :> Capture "name" Text :> Get Greet
70type TestApi = "hello" :> Capture "name" Text :> Get Greet