2014-11-27 18:28:01 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2014-12-20 21:58:07 +01:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2014-11-27 18:28:01 +01:00
|
|
|
import Data.Aeson
|
2014-12-10 16:43:43 +01:00
|
|
|
import Data.Proxy
|
2014-11-27 18:28:01 +01:00
|
|
|
import Data.Text
|
|
|
|
import GHC.Generics
|
2014-12-10 16:43:43 +01:00
|
|
|
import Servant.API
|
2014-11-27 18:28:01 +01:00
|
|
|
import Servant.Docs
|
|
|
|
|
|
|
|
-- * Example
|
|
|
|
|
|
|
|
-- | A greet message data type
|
|
|
|
newtype Greet = Greet { msg :: Text }
|
|
|
|
deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance FromJSON Greet
|
|
|
|
instance ToJSON Greet
|
|
|
|
|
|
|
|
-- We add some useful annotations to our captures,
|
|
|
|
-- query parameters and request body to make the docs
|
|
|
|
-- really helpful.
|
|
|
|
instance ToCapture (Capture "name" Text) where
|
|
|
|
toCapture _ = DocCapture "name" "name of the person to greet"
|
|
|
|
|
|
|
|
instance ToCapture (Capture "greetid" Text) where
|
|
|
|
toCapture _ = DocCapture "greetid" "identifier of the greet msg to remove"
|
|
|
|
|
|
|
|
instance ToParam (QueryParam "capital" Bool) where
|
|
|
|
toParam _ =
|
|
|
|
DocQueryParam "capital"
|
|
|
|
["true", "false"]
|
|
|
|
"Get the greeting message in uppercase (true) or not (false). Default is false."
|
|
|
|
Normal
|
|
|
|
|
2015-01-06 14:30:01 +01:00
|
|
|
instance ToParam (MatrixParam "lang" String) where
|
|
|
|
toParam _ =
|
|
|
|
DocQueryParam "lang"
|
|
|
|
["en", "sv", "fr"]
|
|
|
|
"Get the greeting message selected language. Default is en."
|
|
|
|
Normal
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
instance ToSample Greet where
|
|
|
|
toSample = Just $ Greet "Hello, haskeller!"
|
|
|
|
|
2015-01-04 16:53:02 +01:00
|
|
|
toSamples =
|
|
|
|
[ ("If you use ?capital=true", Greet "HELLO, HASKELLER")
|
|
|
|
, ("If you use ?capital=false", Greet "Hello, haskeller")
|
|
|
|
]
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
-- API specification
|
|
|
|
type TestApi =
|
|
|
|
-- GET /hello/:name?capital={true, false} returns a Greet as JSON
|
2015-01-06 14:30:01 +01:00
|
|
|
"hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
|
2014-11-27 18:28:01 +01:00
|
|
|
|
|
|
|
-- POST /greet with a Greet as JSON in the request body,
|
|
|
|
-- returns a Greet as JSON
|
|
|
|
:<|> "greet" :> ReqBody Greet :> Post Greet
|
|
|
|
|
|
|
|
-- DELETE /greet/:greetid
|
|
|
|
:<|> "greet" :> Capture "greetid" Text :> Delete
|
|
|
|
|
|
|
|
testApi :: Proxy TestApi
|
|
|
|
testApi = Proxy
|
|
|
|
|
|
|
|
-- Generate the data that lets us have API docs. This
|
|
|
|
-- is derived from the type as well as from
|
|
|
|
-- the 'ToCapture', 'ToParam' and 'ToSample' instances from above.
|
|
|
|
docsGreet :: API
|
|
|
|
docsGreet = docs testApi
|
|
|
|
|
|
|
|
main :: IO ()
|
2014-12-20 21:58:07 +01:00
|
|
|
main = putStrLn $ markdown docsGreet
|