packages feed

servant-swagger-1.1.11 / example / src / Todo.hs

{-# LANGUAGE DataKinds                  #-}{-# LANGUAGE DeriveDataTypeable         #-}{-# LANGUAGE DeriveGeneric              #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}{-# LANGUAGE OverloadedStrings          #-}{-# LANGUAGE TypeOperators              #-}module Todo whereimport           Control.Lensimport           Data.Aesonimport           Data.Aeson.Encode.Pretty   (encodePretty)import qualified Data.ByteString.Lazy.Char8 as BL8import           Data.Proxyimport           Data.Swaggerimport           Data.Text                  (Text)import           Data.Time                  (UTCTime (..), fromGregorian)import           Data.Typeable              (Typeable)import           GHC.Genericsimport           Servantimport           Servant.SwaggertodoAPI :: Proxy TodoAPItodoAPI = Proxy-- | The API of a Todo service.type TodoAPI    = "todo" :> Get '[JSON] [Todo] :<|> "todo" :> ReqBody '[JSON] Todo :> Post '[JSON] TodoId :<|> "todo" :> Capture "id" TodoId :> Get '[JSON] Todo :<|> "todo" :> Capture "id" TodoId :> ReqBody '[JSON] Todo :> Put '[JSON] TodoId-- | API for serving @swagger.json@.type SwaggerAPI = "swagger.json" :> Get '[JSON] Swagger-- | Combined API of a Todo service with Swagger documentation.type API = SwaggerAPI :<|> TodoAPI-- | A single Todo entry.data Todo = Todo  { created :: UTCTime  -- ^ Creation datetime.  , summary :: Text     -- ^ Task summary.  } deriving (Show, Generic, Typeable)-- | A unique Todo entry ID.newtype TodoId = TodoId Int  deriving (Show, Generic, Typeable, ToJSON, FromHttpApiData)instance ToJSON Todoinstance FromJSON Todoinstance ToSchema Todo where  declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy    & mapped.schema.description ?~ "This is some real Todo right here"    & mapped.schema.example ?~ toJSON (Todo (UTCTime (fromGregorian 2015 12 31) 0) "get milk")instance ToParamSchema TodoIdinstance ToSchema TodoId-- | Swagger spec for Todo API.todoSwagger :: SwaggertodoSwagger = toSwagger todoAPI  & info.title   .~ "Todo API"  & info.version .~ "1.0"  & info.description ?~ "This is an API that tests swagger integration"  & info.license ?~ ("MIT" & url ?~ URL "http://mit.com")-- | Combined server of a Todo service with Swagger documentation.server :: Server APIserver = return todoSwagger :<|> error "not implemented"-- | Output generated @swagger.json@ file for the @'TodoAPI'@.writeSwaggerJSON :: IO ()writeSwaggerJSON = BL8.writeFile "example/swagger.json" (encodePretty todoSwagger)