1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE DeriveDataTypeable #-}
5module Servant.API.Post where
7import Control.Monad.Trans.Either
10import Data.String.Conversions
12import Network.HTTP.Types
17import Servant.Utils.Client
19-- | Endpoint for POST requests. The type variable represents the type of the
20-- response body (not the request body, use 'Servant.API.RQBody.RQBody' for
25instance ToJSON a => HasServer (Post a) where
26 type Server (Post a) = EitherT (Int, String) IO a
28 route Proxy action request respond
29 | null (pathInfo request) && requestMethod request == methodPost = do
30 e <- runEitherT action
31 respond . succeedWith $ case e of
33 responseLBS status201 [("Content-Type", "application/json")] (encode out)
34 Left (status, message) ->
35 responseLBS (mkStatus status (cs message)) [] (cs message)
36 | null (pathInfo request) && requestMethod request /= methodPost =
37 respond $ failWith WrongMethod
38 | otherwise = respond $ failWith NotFound
40instance FromJSON a => HasClient (Post a) where
41 type Client (Post a) = BaseUrl -> EitherT String IO a
43 clientWithRoute Proxy req uri =
44 performRequest methodPost req 201 uri
46instance ToSample a => HasDocs (Post a) where
47 docsFor Proxy (endpoint, action) =
48 single endpoint' action'
50 where endpoint' = endpoint & method .~ DocPOST
52 action' = action & response.respBody .~ toSample p
53 & response.respStatus .~ 201
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list