1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4module Servant.API.Post where
7import Control.Monad.IO.Class
8import Control.Monad.Trans.Either
11import Data.String.Conversions
12import Network.HTTP.Types
19import qualified Network.HTTP.Client as Client
21-- | Endpoint for POST requests. The type variable represents the type of the
22-- response body (not the request body, use 'Servant.API.RQBody.RQBody' for
26instance ToJSON a => HasServer (Post a) where
27 type Server (Post a) = EitherT (Int, String) IO a
29 route Proxy action request respond
30 | null (pathInfo request) && requestMethod request == methodPost = do
31 e <- runEitherT action
32 respond $ Just $ case e of
34 responseLBS status201 [("Content-Type", "application/json")] (encode out)
35 Left (status, message) ->
36 responseLBS (mkStatus status (cs message)) [] (cs message)
37 | otherwise = respond Nothing
39instance FromJSON a => HasClient (Post a) where
40 type Client (Post a) = URI -> EitherT String IO a
42 clientWithRoute Proxy req uri = do
43 partialRequest <- liftIO $ reqToRequest req uri
45 let request = partialRequest { Client.method = methodPost
48 innerResponse <- liftIO . __withGlobalManager $ \ manager ->
49 Client.httpLbs request manager
51 when (Client.responseStatus innerResponse /= status201) $
52 left ("HTTP POST request failed with status: " ++ show (Client.responseStatus innerResponse))
54 maybe (left "HTTP POST request returned invalid json") return $
55 decode' (Client.responseBody innerResponse)
57instance ToSample a => HasDocs (Post a) where
58 docsFor Proxy (endpoint, action) =
59 single endpoint' action'
61 where endpoint' = endpoint & method .~ DocPOST
63 action' = action & response.respBody .~ toSample p
64 & 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