From c7b138a2f6b997f2c3fc332c177043b428933afb Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 10 Jan 2016 08:58:40 -0800 Subject: [PATCH] api: /prices, /commodities, cleanup --- hledger-api/hledger-api.cabal | 1 + hledger-api/hledger-api.hs | 29 +++++++++++++++-------------- hledger-api/package.yaml | 1 + 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/hledger-api/hledger-api.cabal b/hledger-api/hledger-api.cabal index e39e9506d..2ba2f0f71 100644 --- a/hledger-api/hledger-api.cabal +++ b/hledger-api/hledger-api.cabal @@ -42,6 +42,7 @@ executable hledger-api , hledger == 0.27 , base >= 4 && < 5 , aeson + , containers , Decimal , docopt , either diff --git a/hledger-api/hledger-api.hs b/hledger-api/hledger-api.hs index 59debb85f..d7b22a909 100644 --- a/hledger-api/hledger-api.hs +++ b/hledger-api/hledger-api.hs @@ -16,6 +16,7 @@ import Control.Monad.Trans.Either import Control.Monad.Trans.Reader import Data.Aeson import Data.Decimal +import qualified Data.Map as M import Data.Monoid import Data.Proxy import Data.Text @@ -26,11 +27,11 @@ import Safe import Servant import System.Console.Docopt import System.Environment (getArgs) +import System.Exit import System.IO import Text.Printf import Hledger.Cli hiding (Reader, version) -import System.Exit version="0.27.98" @@ -45,6 +46,7 @@ Usage: Options: -f --file FILE use a different input file + (default: $LEDGER_FILE or ~/.hledger.journal) -p --port PORT use a different TCP port (default: 8001) --version show version -h --help show this help @@ -64,15 +66,9 @@ main = do requireJournalFileExists f readJournalFile Nothing Nothing True f >>= either error' (serveApi f p) --- serveApi :: CliOpts -> Journal -> IO () serveApi :: FilePath -> Int -> Journal -> IO () serveApi f p j = do - -- d <- getCurrentDay - -- let j' = - -- filterJournalTransactions (queryFromOpts d $ reportopts_ opts) $ - -- journalApplyAliases (aliasesFromOpts opts) $ - -- j - printf "Starting web api serving %s on port %d\nPress ctrl-c to quit\n" f p >> hFlush stdout + printf "Starting web api for %s on port %d\nPress ctrl-c to quit\n" f p >> hFlush stdout Warp.run p $ hledgerApiApp j hledgerApiApp :: Journal -> Wai.Application @@ -88,18 +84,22 @@ hledgerApiApp j = Servant.serve hledgerApi hledgerApiServer readerToEither = Nat $ \r -> return (runReader r j) type HledgerApi = - "accounts" :> Get '[JSON] [AccountName] - :<|> - "transactions" :> Get '[JSON] [Transaction] + "accounts" :> Get '[JSON] [AccountName] + :<|> "transactions" :> Get '[JSON] [Transaction] + :<|> "prices" :> Get '[JSON] [MarketPrice] + :<|> "commodities" :> Get '[JSON] [Commodity] hledgerServerT :: ServerT HledgerApi (Reader Journal) hledgerServerT = - accountsH - :<|> - transactionsH + accountsH + :<|> transactionsH + :<|> pricesH + :<|> commoditiesH where accountsH = journalAccountNames <$> ask transactionsH = jtxns <$> ask + pricesH = jmarketprices <$> ask + commoditiesH = (M.keys . jcommoditystyles) <$> ask instance ToJSON ClearedStatus where toJSON = genericToJSON defaultOptions -- avoid https://github.com/bos/aeson/issues/290 @@ -110,6 +110,7 @@ instance ToJSON Side where toJSON = genericToJSON defaultOptions instance ToJSON DigitGroupStyle where toJSON = genericToJSON defaultOptions instance ToJSON MixedAmount where toJSON = genericToJSON defaultOptions instance ToJSON Price where toJSON = genericToJSON defaultOptions +instance ToJSON MarketPrice where toJSON = genericToJSON defaultOptions instance ToJSON Posting where toJSON Posting{..} = diff --git a/hledger-api/package.yaml b/hledger-api/package.yaml index ba5e92ed0..ef0e44023 100644 --- a/hledger-api/package.yaml +++ b/hledger-api/package.yaml @@ -154,6 +154,7 @@ executables: - hledger == 0.27 - base >= 4 && < 5 - aeson + - containers - Decimal - docopt - either