git.iko.soy/typeable/servant
on master help sign in
haddocks for Get
1dd253dAlp Mestanogullari
src/Servant/API/Get.hs+20−2
@@ -17,11 +17,25 @@ import Servant.Common.Req
17import Servant.Docs
17import Servant.Docs
18import Servant.Server
18import Servant.Server
19
19
20−-- | Endpoint for simple GET requests. The server doesn't receive any arguments
20+-- | Endpoint for simple GET requests. Serves the result as JSON.
21−-- and serves the contained type as JSON.
21+--
22+-- Example:
23+--
24+-- > type MyApi = "books" :> Get [Book]
22data Get a
25data Get a
23 deriving Typeable
26 deriving Typeable
24
27
28+-- | When implementing the handler for a 'Get' endpoint,
29+-- just like for 'Servant.API.Delete.Delete', 'Servant.API.Post.Post'
30+-- and 'Servant.API.Put.Put', the handler code runs in the
31+-- @EitherT (Int, String) IO@ monad, where the 'Int' represents
32+-- the status code and the 'String' a message, returned in case of
33+-- failure. You can quite handily use 'Control.Monad.Trans.EitherT.left'
34+-- to quickly fail if some conditions are not met.
35+--
36+-- If successfully returning a value, we just require that its type has
37+-- a 'ToJSON' instance and servant takes care of encoding it for you,
38+-- yielding status code 200 along the way.
25instance ToJSON result => HasServer (Get result) where
39instance ToJSON result => HasServer (Get result) where
26 type Server (Get result) = EitherT (Int, String) IO result
40 type Server (Get result) = EitherT (Int, String) IO result
27 route Proxy action request respond
41 route Proxy action request respond
@@ -36,6 +50,10 @@ instance ToJSON result => HasServer (Get result) where
36 respond $ failWith WrongMethod
50 respond $ failWith WrongMethod
37 | otherwise = respond $ failWith NotFound
51 | otherwise = respond $ failWith NotFound
38
52
53+-- | If you have a 'Get' endpoint in your API, the client
54+-- side querying function that is created when calling 'client'
55+-- will just require an argument that specifies the scheme, host
56+-- and port to send the request to.
39instance FromJSON result => HasClient (Get result) where
57instance FromJSON result => HasClient (Get result) where
40 type Client (Get result) = BaseUrl -> EitherT String IO result
58 type Client (Get result) = BaseUrl -> EitherT String IO result
41 clientWithRoute Proxy req host =
59 clientWithRoute Proxy req host =