on 286b1db help sign in
servant/src/Servant/API/ReqBody.hs
52 lines 1.4 kB history raw
1{-# LANGUAGE PolyKinds #-}
2{-# LANGUAGE TypeFamilies #-}
3{-# LANGUAGE TypeOperators #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE ScopedTypeVariables #-}
6module Servant.API.ReqBody where
8import Control.Applicative
9import Data.Aeson
10import Data.Proxy
11import Network.Wai
12import Servant.API.Sub
13import Servant.Client
14import Servant.Common.Req
15import Servant.Docs
16import Servant.Server
18-- * Request Body support
19data ReqBody a
21instance (FromJSON a, HasServer sublayout)
22 => HasServer (ReqBody a :> sublayout) where
24 type Server (ReqBody a :> sublayout) =
25 a -> Server sublayout
27 route Proxy subserver request respond = do
28 mrqbody <- decode' <$> lazyRequestBody request
29 case mrqbody of
30 Nothing -> respond $ failWith InvalidBody
31 Just v -> route (Proxy :: Proxy sublayout) (subserver v) request respond
33instance (ToJSON a, HasClient sublayout)
34 => HasClient (ReqBody a :> sublayout) where
36 type Client (ReqBody a :> sublayout) =
37 a -> Client sublayout
39 clientWithRoute Proxy req body =
40 clientWithRoute (Proxy :: Proxy sublayout) $
41 setRQBody (encode body) req
43instance (ToSample a, HasDocs sublayout)
44 => HasDocs (ReqBody a :> sublayout) where
46 docsFor Proxy (endpoint, action) =
47 docsFor sublayoutP (endpoint, action')
49 where sublayoutP = Proxy :: Proxy sublayout
51 action' = action & rqbody .~ sampleByteString p
52 p = Proxy :: Proxy a