servant/src/Servant/API/ReqBody.hs

53 lines
1.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
2014-10-28 16:29:04 +01:00
module Servant.API.ReqBody where
import Control.Applicative
import Data.Aeson
import Data.Proxy
import Network.Wai
import Servant.API.Sub
import Servant.Client
import Servant.Common.Req
import Servant.Docs
import Servant.Server
-- * Request Body support
2014-10-28 16:29:04 +01:00
data ReqBody a
instance (FromJSON a, HasServer sublayout)
2014-10-28 16:29:04 +01:00
=> HasServer (ReqBody a :> sublayout) where
2014-10-28 16:29:04 +01:00
type Server (ReqBody a :> sublayout) =
a -> Server sublayout
route Proxy subserver request respond = do
mrqbody <- decode' <$> lazyRequestBody request
case mrqbody of
Nothing -> respond $ failWith InvalidBody
Just v -> route (Proxy :: Proxy sublayout) (subserver v) request respond
instance (ToJSON a, HasClient sublayout)
2014-10-28 16:29:04 +01:00
=> HasClient (ReqBody a :> sublayout) where
2014-10-28 16:29:04 +01:00
type Client (ReqBody a :> sublayout) =
a -> Client sublayout
clientWithRoute Proxy req body =
clientWithRoute (Proxy :: Proxy sublayout) $
setRQBody (encode body) req
instance (ToSample a, HasDocs sublayout)
2014-10-28 16:29:04 +01:00
=> HasDocs (ReqBody a :> sublayout) where
docsFor Proxy (endpoint, action) =
docsFor sublayoutP (endpoint, action')
where sublayoutP = Proxy :: Proxy sublayout
2014-11-13 08:19:14 +01:00
action' = action & rqbody .~ sampleByteString p
p = Proxy :: Proxy a