mirror of
https://github.com/simonmichael/hledger.git
synced 2024-12-26 03:42:25 +03:00
api: --swagger shows swagger 2.0 api docs
This commit is contained in:
parent
ae9defb718
commit
b9b1cff22d
@ -27,7 +27,8 @@ cabal-version: >= 1.10
|
|||||||
|
|
||||||
extra-source-files:
|
extra-source-files:
|
||||||
CHANGES
|
CHANGES
|
||||||
examples/01-accounts.html
|
examples/01.html
|
||||||
|
examples/02.html
|
||||||
|
|
||||||
source-repository head
|
source-repository head
|
||||||
type: git
|
type: git
|
||||||
@ -42,12 +43,16 @@ executable hledger-api
|
|||||||
, hledger == 0.27
|
, hledger == 0.27
|
||||||
, base >= 4 && < 5
|
, base >= 4 && < 5
|
||||||
, aeson
|
, aeson
|
||||||
|
, bytestring
|
||||||
, containers
|
, containers
|
||||||
, Decimal
|
, Decimal
|
||||||
, docopt
|
, docopt
|
||||||
, either
|
, either
|
||||||
|
, lens
|
||||||
, safe
|
, safe
|
||||||
, servant-server
|
, servant-server
|
||||||
|
, servant-swagger
|
||||||
|
, swagger2
|
||||||
, text
|
, text
|
||||||
, transformers
|
, transformers
|
||||||
, wai
|
, wai
|
||||||
|
@ -11,21 +11,25 @@
|
|||||||
|
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
|
import Control.Lens ((&), (.~), (?~))
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
import Control.Monad.IO.Class
|
import Control.Monad.IO.Class
|
||||||
import Control.Monad.Trans.Either
|
import Control.Monad.Trans.Either
|
||||||
import Control.Monad.Trans.Reader
|
import Control.Monad.Trans.Reader
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
|
import qualified Data.ByteString.Lazy.Char8 as BL8
|
||||||
import Data.Decimal
|
import Data.Decimal
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import Data.Monoid
|
import Data.Monoid
|
||||||
import Data.Proxy
|
import Data.Proxy
|
||||||
|
import Data.Swagger
|
||||||
import Data.Text hiding (map,reverse)
|
import Data.Text hiding (map,reverse)
|
||||||
import GHC.Generics
|
import GHC.Generics
|
||||||
import Network.Wai as Wai
|
import Network.Wai as Wai
|
||||||
import Network.Wai.Handler.Warp as Warp
|
import Network.Wai.Handler.Warp as Warp
|
||||||
import Safe
|
import Safe
|
||||||
import Servant
|
import Servant
|
||||||
|
import Servant.Swagger
|
||||||
import System.Console.Docopt
|
import System.Console.Docopt
|
||||||
import System.Environment (getArgs)
|
import System.Environment (getArgs)
|
||||||
import System.Exit
|
import System.Exit
|
||||||
@ -37,6 +41,7 @@ import Hledger.Cli hiding (Reader, version)
|
|||||||
|
|
||||||
version="0.27.98"
|
version="0.27.98"
|
||||||
|
|
||||||
|
-- https://github.com/docopt/docopt.hs#readme
|
||||||
doc :: Docopt
|
doc :: Docopt
|
||||||
doc = [docopt|
|
doc = [docopt|
|
||||||
hledger-api 0.27.98
|
hledger-api 0.27.98
|
||||||
@ -45,6 +50,9 @@ Serves hledger data and reports as a JSON web API.
|
|||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
hledger-api [options]
|
hledger-api [options]
|
||||||
|
start API server
|
||||||
|
hledger-api --swagger
|
||||||
|
print API docs in Swagger 2.0 format
|
||||||
hledger-api --version
|
hledger-api --version
|
||||||
hledger-api --help
|
hledger-api --help
|
||||||
|
|
||||||
@ -63,6 +71,7 @@ main = do
|
|||||||
args <- getArgs >>= parseArgsOrExit doc
|
args <- getArgs >>= parseArgsOrExit doc
|
||||||
when (isPresent args (longOption "help")) $ exitWithUsage doc
|
when (isPresent args (longOption "help")) $ exitWithUsage doc
|
||||||
when (isPresent args (longOption "version")) $ putStrLn version >> exitSuccess
|
when (isPresent args (longOption "version")) $ putStrLn version >> exitSuccess
|
||||||
|
when (isPresent args (longOption "swagger")) $ BL8.putStrLn (encode swaggerDoc) >> exitSuccess
|
||||||
let defp = "8001"
|
let defp = "8001"
|
||||||
p <- case readMay $ getArgWithDefault args defp (longOption "port") of
|
p <- case readMay $ getArgWithDefault args defp (longOption "port") of
|
||||||
Nothing -> exitWithUsage doc
|
Nothing -> exitWithUsage doc
|
||||||
@ -87,6 +96,7 @@ type HledgerApi =
|
|||||||
:<|> "commodities" :> Get '[JSON] [Commodity]
|
:<|> "commodities" :> Get '[JSON] [Commodity]
|
||||||
:<|> "accounts" :> Get '[JSON] [Account]
|
:<|> "accounts" :> Get '[JSON] [Account]
|
||||||
:<|> "accounttransactions" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport
|
:<|> "accounttransactions" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport
|
||||||
|
-- :<|> "swagger" :> Get '[JSON] Swagger
|
||||||
:<|> Raw
|
:<|> Raw
|
||||||
|
|
||||||
hledgerApiApp :: FilePath -> Journal -> Wai.Application
|
hledgerApiApp :: FilePath -> Journal -> Wai.Application
|
||||||
@ -103,6 +113,7 @@ hledgerApiApp staticdir j = Servant.serve api server
|
|||||||
:<|> commoditiesH
|
:<|> commoditiesH
|
||||||
:<|> accountsH
|
:<|> accountsH
|
||||||
:<|> accounttransactionsH
|
:<|> accounttransactionsH
|
||||||
|
-- :<|> swaggerH
|
||||||
:<|> serveDirectory staticdir
|
:<|> serveDirectory staticdir
|
||||||
where
|
where
|
||||||
accountnamesH = return $ journalAccountNames j
|
accountnamesH = return $ journalAccountNames j
|
||||||
@ -120,6 +131,7 @@ hledgerApiApp staticdir j = Servant.serve api server
|
|||||||
q = Hledger.Query.Any --filterQuery (not . queryIsDepth) $ queryFromOpts d ropts'
|
q = Hledger.Query.Any --filterQuery (not . queryIsDepth) $ queryFromOpts d ropts'
|
||||||
thisacctq = Acct $ accountNameToAccountRegex a -- includes subs
|
thisacctq = Acct $ accountNameToAccountRegex a -- includes subs
|
||||||
return $ accountTransactionsReport ropts j q thisacctq
|
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 ClearedStatus where toJSON = genericToJSON defaultOptions -- avoiding https://github.com/bos/aeson/issues/290
|
||||||
instance ToJSON GenericSourcePos where toJSON = genericToJSON defaultOptions
|
instance ToJSON GenericSourcePos where toJSON = genericToJSON defaultOptions
|
||||||
@ -160,3 +172,35 @@ instance ToJSON Account where
|
|||||||
,"asubs" .= toJSON (map toJSON $ asubs a)
|
,"asubs" .= toJSON (map toJSON $ asubs a)
|
||||||
]
|
]
|
||||||
instance ToJSON AccountTransactionsReport where toJSON = genericToJSON defaultOptions
|
instance ToJSON AccountTransactionsReport where toJSON = genericToJSON defaultOptions
|
||||||
|
|
||||||
|
-- swagger api doc
|
||||||
|
|
||||||
|
swaggerDoc :: Swagger
|
||||||
|
swaggerDoc = 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
|
||||||
|
where
|
||||||
|
declareNamedSchema _proxy = pure (Just "Decimal", schema)
|
||||||
|
where
|
||||||
|
schema = mempty
|
||||||
|
& schemaType .~ SwaggerNumber
|
||||||
|
& schemaExample ?~ toJSON (100 :: Decimal)
|
||||||
|
instance ToSchema Amount
|
||||||
|
instance ToSchema AmountStyle
|
||||||
|
instance ToSchema Side
|
||||||
|
instance ToSchema DigitGroupStyle
|
||||||
|
instance ToSchema MixedAmount
|
||||||
|
instance ToSchema Price
|
||||||
|
instance ToSchema MarketPrice
|
||||||
|
instance ToSchema PostingType
|
||||||
|
instance ToSchema Posting
|
||||||
|
instance ToSchema Transaction
|
||||||
|
instance ToSchema Account
|
||||||
|
-- instance ToSchema AccountTransactionsReport
|
||||||
|
@ -154,12 +154,16 @@ executables:
|
|||||||
- hledger == 0.27
|
- hledger == 0.27
|
||||||
- base >= 4 && < 5
|
- base >= 4 && < 5
|
||||||
- aeson
|
- aeson
|
||||||
|
- bytestring
|
||||||
- containers
|
- containers
|
||||||
- Decimal
|
- Decimal
|
||||||
- docopt
|
- docopt
|
||||||
- either
|
- either
|
||||||
|
- lens
|
||||||
- safe
|
- safe
|
||||||
- servant-server
|
- servant-server
|
||||||
|
- servant-swagger
|
||||||
|
- swagger2
|
||||||
- text
|
- text
|
||||||
- transformers
|
- transformers
|
||||||
- wai
|
- wai
|
||||||
|
Loading…
Reference in New Issue
Block a user