-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONRequestExamples.hs
More file actions
39 lines (32 loc) · 1.2 KB
/
JSONRequestExamples.hs
File metadata and controls
39 lines (32 loc) · 1.2 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
module JSONRequestExamples
(
jsonRequestExamples
) where
import Splitwise
import Control.Monad.IO.Class (liftIO)
import Data.Aeson.Encode.Pretty (encodePretty)
import qualified Data.ByteString.Lazy.Char8 as LBS
import Data.Default
import Data.String (fromString)
import System.Environment
-- | Demonstrates using the JSON variant functions to get raw Aeson Value responses.
-- Each *JSON function mirrors its typed counterpart but returns the full JSON response.
jsonRequestExamples :: IO ()
jsonRequestExamples = do
apiAuth <- SplitwiseAPIKeyAuth . fromString <$> getEnv "SPLITWISE_API_TOKEN"
result <- runSplitwise apiAuth $ do
-- Simple GET
currentUser <- getCurrentUserJSON
liftIO $ putStrLn "Current user:"
liftIO $ LBS.putStrLn $ encodePretty currentUser
-- GET with path parameter
groups <- getGroupsJSON
liftIO $ putStrLn "\nGroups:"
liftIO $ LBS.putStrLn $ encodePretty groups
-- Paginated GET
expenses <- getAllExpensesJSON def
liftIO $ putStrLn $ "\nExpenses: " <> show (length expenses)
mapM_ (liftIO . LBS.putStrLn . encodePretty) (take 2 expenses)
case result of
Left err -> putStrLn $ "Error: " <> show err
Right () -> putStrLn "Done"