packages feed

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

{-# LANGUAGE DataKinds                  #-}{-# LANGUAGE DeriveDataTypeable         #-}{-# LANGUAGE DeriveGeneric              #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}{-# LANGUAGE OverloadedStrings          #-}{-# LANGUAGE TypeOperators              #-}module Todo whereimport Control.Lensimport Data.Aesonimport Data.Proxyimport Data.Text (Text)import Data.Time (UTCTime(..), fromGregorian)import Data.Typeable (Typeable)import Data.Swaggerimport 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, FromText)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"