1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Servant.API.Get where
6import Control.Monad.IO.Class
7import Control.Monad.Trans.Either
10import Data.String.Conversions
11import Network.HTTP.Types
17import qualified Network.HTTP.Client as Client
19-- | Endpoint for simple GET requests. The server doesn't receive any arguments
20-- and serves the contained type as JSON.
23instance ToJSON result => HasServer (Get result) where
24 type Server (Get result) = EitherT (Int, String) IO result
25 route Proxy action _globalPathInfo request respond
26 | null (pathInfo request) && requestMethod request == methodGet = do
27 e <- runEitherT action
28 respond $ Just $ case e of
30 responseLBS ok200 [("Content-Type", "application/json")] (encode output)
31 Left (status, message) ->
32 responseLBS (mkStatus status (cs message)) [] (cs message)
33 | otherwise = respond Nothing
35instance FromJSON result => HasClient (Get result) where
36 type Client (Get result) = URI -> EitherT String IO result
37 clientWithRoute Proxy req uri = do
38 innerRequest <- liftIO $ reqToRequest req uri
40 innerResponse <- liftIO $ __withGlobalManager $ \ manager ->
41 Client.httpLbs innerRequest manager
42 when (Client.responseStatus innerResponse /= ok200) $
43 left ("HTTP GET request failed with status: " ++ show (Client.responseStatus innerResponse))
44 maybe (left "HTTP GET request returned invalid json") return $
45 decode' (Client.responseBody innerResponse)
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