api: /prices, /commodities, cleanup

This commit is contained in:
Simon Michael 2016-01-10 08:58:40 -08:00
parent 921cbe5a9e
commit c7b138a2f6
3 changed files with 17 additions and 14 deletions

View File

@ -42,6 +42,7 @@ executable hledger-api
, hledger == 0.27 , hledger == 0.27
, base >= 4 && < 5 , base >= 4 && < 5
, aeson , aeson
, containers
, Decimal , Decimal
, docopt , docopt
, either , either

View File

@ -16,6 +16,7 @@ import Control.Monad.Trans.Either
import Control.Monad.Trans.Reader import Control.Monad.Trans.Reader
import Data.Aeson import Data.Aeson
import Data.Decimal import Data.Decimal
import qualified Data.Map as M
import Data.Monoid import Data.Monoid
import Data.Proxy import Data.Proxy
import Data.Text import Data.Text
@ -26,11 +27,11 @@ import Safe
import Servant import Servant
import System.Console.Docopt import System.Console.Docopt
import System.Environment (getArgs) import System.Environment (getArgs)
import System.Exit
import System.IO import System.IO
import Text.Printf import Text.Printf
import Hledger.Cli hiding (Reader, version) import Hledger.Cli hiding (Reader, version)
import System.Exit
version="0.27.98" version="0.27.98"
@ -45,6 +46,7 @@ Usage:
Options: Options:
-f --file FILE use a different input file -f --file FILE use a different input file
(default: $LEDGER_FILE or ~/.hledger.journal)
-p --port PORT use a different TCP port (default: 8001) -p --port PORT use a different TCP port (default: 8001)
--version show version --version show version
-h --help show this help -h --help show this help
@ -64,15 +66,9 @@ main = do
requireJournalFileExists f requireJournalFileExists f
readJournalFile Nothing Nothing True f >>= either error' (serveApi f p) readJournalFile Nothing Nothing True f >>= either error' (serveApi f p)
-- serveApi :: CliOpts -> Journal -> IO ()
serveApi :: FilePath -> Int -> Journal -> IO () serveApi :: FilePath -> Int -> Journal -> IO ()
serveApi f p j = do serveApi f p j = do
-- d <- getCurrentDay printf "Starting web api for %s on port %d\nPress ctrl-c to quit\n" f p >> hFlush stdout
-- 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
Warp.run p $ hledgerApiApp j Warp.run p $ hledgerApiApp j
hledgerApiApp :: Journal -> Wai.Application hledgerApiApp :: Journal -> Wai.Application
@ -89,17 +85,21 @@ hledgerApiApp j = Servant.serve hledgerApi hledgerApiServer
type HledgerApi = type HledgerApi =
"accounts" :> Get '[JSON] [AccountName] "accounts" :> Get '[JSON] [AccountName]
:<|> :<|> "transactions" :> Get '[JSON] [Transaction]
"transactions" :> Get '[JSON] [Transaction] :<|> "prices" :> Get '[JSON] [MarketPrice]
:<|> "commodities" :> Get '[JSON] [Commodity]
hledgerServerT :: ServerT HledgerApi (Reader Journal) hledgerServerT :: ServerT HledgerApi (Reader Journal)
hledgerServerT = hledgerServerT =
accountsH accountsH
:<|> :<|> transactionsH
transactionsH :<|> pricesH
:<|> commoditiesH
where where
accountsH = journalAccountNames <$> ask accountsH = journalAccountNames <$> ask
transactionsH = jtxns <$> 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 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 DigitGroupStyle where toJSON = genericToJSON defaultOptions
instance ToJSON MixedAmount where toJSON = genericToJSON defaultOptions instance ToJSON MixedAmount where toJSON = genericToJSON defaultOptions
instance ToJSON Price where toJSON = genericToJSON defaultOptions instance ToJSON Price where toJSON = genericToJSON defaultOptions
instance ToJSON MarketPrice where toJSON = genericToJSON defaultOptions
instance ToJSON Posting instance ToJSON Posting
where where
toJSON Posting{..} = toJSON Posting{..} =

View File

@ -154,6 +154,7 @@ executables:
- hledger == 0.27 - hledger == 0.27
- base >= 4 && < 5 - base >= 4 && < 5
- aeson - aeson
- containers
- Decimal - Decimal
- docopt - docopt
- either - either