1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4module Servant.API.Get 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 simple GET requests. The server doesn't receive any arguments
22-- and serves the contained type as JSON.
25instance ToJSON result => HasServer (Get result) where
26 type Server (Get result) = EitherT (Int, String) IO result
27 route Proxy action request respond
28 | null (pathInfo request) && requestMethod request == methodGet = do
29 e <- runEitherT action
30 respond $ Just $ case e of
32 responseLBS ok200 [("Content-Type", "application/json")] (encode output)
33 Left (status, message) ->
34 responseLBS (mkStatus status (cs message)) [] (cs message)
35 | otherwise = respond Nothing
37instance FromJSON result => HasClient (Get result) where
38 type Client (Get result) = URI -> EitherT String IO result
39 clientWithRoute Proxy req uri = do
40 innerRequest <- liftIO $ reqToRequest req uri
42 innerResponse <- liftIO $ __withGlobalManager $ \ manager ->
43 Client.httpLbs innerRequest manager
44 when (Client.responseStatus innerResponse /= ok200) $
45 left ("HTTP GET request failed with status: " ++ show (Client.responseStatus innerResponse))
46 maybe (left "HTTP GET request returned invalid json") return $
47 decode' (Client.responseBody innerResponse)
49instance ToSample a => HasDocs (Get a) where
50 docsFor Proxy (endpoint, action) =
51 single endpoint' action'
53 where endpoint' = endpoint & method .~ DocGET
54 action' = action & response.respBody .~ toSample p
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