From f083a88e769103a60b0fabab8a09c978ce439669 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 21 Jan 2016 14:49:28 -0800 Subject: [PATCH] api: /swagger.json serves swagger 2.0 api docs --- hledger-api/hledger-api.hs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/hledger-api/hledger-api.hs b/hledger-api/hledger-api.hs index b390fd1c1..94b9429cb 100644 --- a/hledger-api/hledger-api.hs +++ b/hledger-api/hledger-api.hs @@ -71,7 +71,7 @@ main = do args <- getArgs >>= parseArgsOrExit doc when (isPresent args (longOption "help")) $ exitWithUsage doc when (isPresent args (longOption "version")) $ putStrLn version >> exitSuccess - when (isPresent args (longOption "swagger")) $ BL8.putStrLn (encode swaggerDoc) >> exitSuccess + when (isPresent args (longOption "swagger")) $ BL8.putStrLn (encode swaggerSpec) >> exitSuccess let defp = "8001" p <- case readMay $ getArgWithDefault args defp (longOption "port") of Nothing -> exitWithUsage doc @@ -96,11 +96,13 @@ type HledgerApi = :<|> "commodities" :> Get '[JSON] [Commodity] :<|> "accounts" :> Get '[JSON] [Account] :<|> "accounttransactions" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport - -- :<|> "swagger" :> Get '[JSON] Swagger :<|> Raw +type HledgerApi' = ("swagger.json" :> Get '[JSON] Swagger) :<|> HledgerApi + hledgerApiApp :: FilePath -> Journal -> Wai.Application -hledgerApiApp staticdir j = Servant.serve api server +hledgerApiApp staticdir j = + Servant.serve (Proxy :: Proxy HledgerApi') (return swaggerSpec :<|> server) where api :: Proxy HledgerApi api = Proxy @@ -113,7 +115,6 @@ hledgerApiApp staticdir j = Servant.serve api server :<|> commoditiesH :<|> accountsH :<|> accounttransactionsH - -- :<|> swaggerH :<|> serveDirectory staticdir where accountnamesH = return $ journalAccountNames j @@ -131,7 +132,6 @@ hledgerApiApp staticdir j = Servant.serve api server q = Hledger.Query.Any --filterQuery (not . queryIsDepth) $ queryFromOpts d ropts' thisacctq = Acct $ accountNameToAccountRegex a -- includes subs return $ accountTransactionsReport ropts j q thisacctq - -- swaggerH = return swaggerDoc instance ToJSON ClearedStatus where toJSON = genericToJSON defaultOptions -- avoiding https://github.com/bos/aeson/issues/290 instance ToJSON GenericSourcePos where toJSON = genericToJSON defaultOptions @@ -175,14 +175,13 @@ instance ToJSON AccountTransactionsReport where toJSON = genericToJSON defaultOp -- swagger api doc -swaggerDoc :: Swagger -swaggerDoc = toSwagger (Proxy :: Proxy HledgerApi) +swaggerSpec :: Swagger +swaggerSpec = toSwagger (Proxy :: Proxy HledgerApi) & info.infoTitle .~ "hledger API" & info.infoVersion .~ "0.0.0.1" & info.infoDescription ?~ "This is the API provided by hledger-api for reading hledger data" & info.infoLicense ?~ License "GPLv3+" (Nothing) --- instance ToSchema Swagger instance ToSchema ClearedStatus instance ToSchema GenericSourcePos instance ToSchema Decimal