2014-10-25 01:27:39 +02:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2014-10-28 09:04:27 +01:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2014-10-30 14:54:00 +01:00
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
2014-10-25 01:27:39 +02:00
|
|
|
module Servant.API.Get where
|
|
|
|
|
|
|
|
import Control.Monad.Trans.Either
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Proxy
|
|
|
|
import Data.String.Conversions
|
2014-10-30 14:54:00 +01:00
|
|
|
import Data.Typeable
|
2014-10-25 01:27:39 +02:00
|
|
|
import Network.HTTP.Types
|
|
|
|
import Network.Wai
|
|
|
|
import Servant.Client
|
2014-11-07 11:57:41 +01:00
|
|
|
import Servant.Common.BaseUrl
|
|
|
|
import Servant.Common.Req
|
2014-10-28 09:04:27 +01:00
|
|
|
import Servant.Docs
|
2014-10-25 01:27:39 +02:00
|
|
|
import Servant.Server
|
|
|
|
|
2014-11-22 17:41:35 +01:00
|
|
|
-- | Endpoint for simple GET requests. Serves the result as JSON.
|
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
|
|
|
-- > type MyApi = "books" :> Get [Book]
|
2014-10-25 01:27:39 +02:00
|
|
|
data Get a
|
2014-10-30 14:54:00 +01:00
|
|
|
deriving Typeable
|
2014-10-25 01:27:39 +02:00
|
|
|
|
2014-11-22 17:41:35 +01:00
|
|
|
-- | When implementing the handler for a 'Get' endpoint,
|
|
|
|
-- just like for 'Servant.API.Delete.Delete', 'Servant.API.Post.Post'
|
|
|
|
-- and 'Servant.API.Put.Put', the handler code runs in the
|
|
|
|
-- @EitherT (Int, String) IO@ monad, where the 'Int' represents
|
|
|
|
-- the status code and the 'String' a message, returned in case of
|
|
|
|
-- failure. You can quite handily use 'Control.Monad.Trans.EitherT.left'
|
|
|
|
-- to quickly fail if some conditions are not met.
|
|
|
|
--
|
|
|
|
-- If successfully returning a value, we just require that its type has
|
|
|
|
-- a 'ToJSON' instance and servant takes care of encoding it for you,
|
|
|
|
-- yielding status code 200 along the way.
|
2014-10-25 01:27:39 +02:00
|
|
|
instance ToJSON result => HasServer (Get result) where
|
|
|
|
type Server (Get result) = EitherT (Int, String) IO result
|
2014-10-28 10:42:49 +01:00
|
|
|
route Proxy action request respond
|
2014-10-25 01:27:39 +02:00
|
|
|
| null (pathInfo request) && requestMethod request == methodGet = do
|
|
|
|
e <- runEitherT action
|
2014-10-28 14:34:28 +01:00
|
|
|
respond . succeedWith $ case e of
|
2014-10-25 01:27:39 +02:00
|
|
|
Right output ->
|
|
|
|
responseLBS ok200 [("Content-Type", "application/json")] (encode output)
|
|
|
|
Left (status, message) ->
|
|
|
|
responseLBS (mkStatus status (cs message)) [] (cs message)
|
2014-10-28 14:34:28 +01:00
|
|
|
| null (pathInfo request) && requestMethod request /= methodGet =
|
|
|
|
respond $ failWith WrongMethod
|
|
|
|
| otherwise = respond $ failWith NotFound
|
2014-10-25 01:27:39 +02:00
|
|
|
|
2014-11-22 17:41:35 +01:00
|
|
|
-- | If you have a 'Get' endpoint in your API, the client
|
|
|
|
-- side querying function that is created when calling 'client'
|
|
|
|
-- will just require an argument that specifies the scheme, host
|
|
|
|
-- and port to send the request to.
|
2014-10-25 01:27:39 +02:00
|
|
|
instance FromJSON result => HasClient (Get result) where
|
2014-11-05 13:03:57 +01:00
|
|
|
type Client (Get result) = BaseUrl -> EitherT String IO result
|
2014-10-30 14:54:00 +01:00
|
|
|
clientWithRoute Proxy req host =
|
2014-11-13 12:57:53 +01:00
|
|
|
performRequestJSON methodGet req 200 host
|
2014-10-28 09:04:27 +01:00
|
|
|
|
|
|
|
instance ToSample a => HasDocs (Get a) where
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
single endpoint' action'
|
|
|
|
|
|
|
|
where endpoint' = endpoint & method .~ DocGET
|
2014-11-13 08:19:14 +01:00
|
|
|
action' = action & response.respBody .~ sampleByteString p
|
2014-10-28 09:04:27 +01:00
|
|
|
p = Proxy :: Proxy a
|