-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpenseExamples.hs
More file actions
38 lines (33 loc) · 1.29 KB
/
ExpenseExamples.hs
File metadata and controls
38 lines (33 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module ExpenseExamples
(
getTwoPagesOfExpenses
) where
import Splitwise
import Splitwise.Data.Cursors
import Control.Monad.Loops (unfoldrM)
import Data.Aeson.Schema
import Data.Default
import Data.String (fromString)
import System.Environment
-- | Fetch at most 2 pages of 2 expenses each (up to 4 expenses total)
-- using the sample 'ExpensesPaginateTwoCursor' pagination strategy.
getTwoPagesOfExpenses :: IO ()
getTwoPagesOfExpenses = do
apiAuth <- SplitwiseAPIKeyAuth . fromString <$> getEnv "SPLITWISE_API_TOKEN"
result <- runSplitwise apiAuth $ do
let cursor = mkTwoPagesOfTwoExpensesCursor def
unfoldrM (unfoldWith twoPagesOfTwoExpensesCursorNextState getExpensesWithCursor) (GoCursor cursor)
case result of
Left err -> putStrLn $ "Error: " <> show err
Right pages -> mapM_ printPage (zip [1..] pages)
where
printPage :: (Int, [Object ExpenseSchema]) -> IO ()
printPage (n, expenses) = do
putStrLn $ "Page " <> show n <> ":"
mapM_ printExpense expenses
printExpense :: Object ExpenseSchema -> IO ()
printExpense e = putStrLn $
" " <> show ([get| .id |] e) <> ": " <> show ([get| .description |] e) <> " (" <> show ([get| .cost |] e) <> ")"