-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetExamples.hs
More file actions
81 lines (64 loc) · 2.62 KB
/
GetExamples.hs
File metadata and controls
81 lines (64 loc) · 2.62 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module GetExamples
(
allGetRequests
) where
import Splitwise
import Control.Monad.IO.Class (liftIO)
import Data.Aeson.Schema
import Data.Default
import Data.Maybe (listToMaybe)
import Data.String (fromString)
import System.Environment
-- | Calls every GET endpoint, using results from earlier calls to feed IDs into later ones.
-- Note: getAllExpenses paginates through all expenses, which may be slow for accounts with many expenses.
allGetRequests :: IO ()
allGetRequests = do
apiAuth <- SplitwiseAPIKeyAuth . fromString <$> getEnv "SPLITWISE_API_TOKEN"
result <- runSplitwise apiAuth $ do
-- Users
currentUser <- getCurrentUser
liftIO $ putStrLn $ "Current user: " <> show ([get| .first_name |] currentUser)
user <- getUser ([get| .id |] currentUser)
liftIO $ putStrLn $ "Get user: " <> show ([get| .first_name |] user)
-- Groups
groups <- getGroups
liftIO $ putStrLn $ "Groups: " <> show (length groups)
case listToMaybe groups of
Just g -> do
group <- getGroup ([get| .id |] g)
liftIO $ putStrLn $ " First group: " <> show ([get| .name |] group)
Nothing -> liftIO $ putStrLn " No groups found"
-- Friends
friends <- getFriends
liftIO $ putStrLn $ "Friends: " <> show (length friends)
case listToMaybe friends of
Just f -> do
friend <- getFriend ([get| .id |] f)
liftIO $ putStrLn $ " First friend: " <> show ([get| .first_name |] friend)
liftIO $ putStrLn $ " Friend group_ids: " <> show ([get| .groups[].group_id |] friend)
Nothing -> liftIO $ putStrLn " No friends found"
-- Expenses
expenses <- getAllExpenses def
liftIO $ putStrLn $ "Expenses: " <> show (length expenses)
case listToMaybe expenses of
Just e -> do
let eid = [get| .id |] e
expense <- getExpense eid
liftIO $ putStrLn $ " First expense: " <> show ([get| .description |] expense)
comments <- getExpenseComments eid
liftIO $ putStrLn $ " Comments on first expense: " <> show (length comments)
Nothing -> liftIO $ putStrLn " No expenses found"
-- Notifications
notifications <- getAllNotifications
liftIO $ putStrLn $ "Notifications: " <> show (length notifications)
-- Other
currencies <- getCurrencies
liftIO $ putStrLn $ "Currencies: " <> show (length currencies)
categories <- getCategories
liftIO $ putStrLn $ "Categories: " <> show (length categories)
case result of
Left err -> putStrLn $ "Error: " <> show err
Right () -> putStrLn "Done"