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.
24instance ToJSON a => HasServer (Post a) where
25 type Server (Post a) = EitherT (Int, String) IO a
27 route Proxy action _globalPathInfo request respond
28 | null (pathInfo request) && requestMethod request == methodPost = do
29 e <- runEitherT action
30 respond $ Just $ case e of
32 responseLBS status201 [("Content-Type", "application/json")] (encode out)
33 Left (status, message) ->
34 responseLBS (mkStatus status (cs message)) [] (cs message)
35 | otherwise = respond Nothing
37instance FromJSON a => HasClient (Post a) where
38 type Client (Post a) = URI -> EitherT String IO a
40 clientWithRoute Proxy req uri = do
41 partialRequest <- liftIO $ reqToRequest req uri
43 let request = partialRequest { Client.method = methodPost
46 innerResponse <- liftIO . __withGlobalManager $ \ manager ->
47 Client.httpLbs request manager
49 when (Client.responseStatus innerResponse /= status201) $
50 left ("HTTP POST request failed with status: " ++ show (Client.responseStatus innerResponse))
52 maybe (left "HTTP POST request returned invalid json") return $
53 decode' (Client.responseBody innerResponse)
55instance ToSample a => HasDocs (Post a) where
56 docsFor Proxy (endpoint, action) =
57 single endpoint' action'
59 where endpoint' = endpoint & method .~ DocPOST
61 action' = action & response.respBody .~ toSample p
62 & 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