diff options
author | Julien Dessaux | 2023-07-21 00:01:02 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-07-21 00:01:02 +0200 |
commit | dcd0a7a9b2612f383c1f627c72c27868c367058d (patch) | |
tree | 773b322f5b353cd39fbaf4161deba93968d20b56 /haskell/src/SpaceTraders/APIClient | |
parent | [haskell] Finalized the agent initialization, refresh and reset (diff) | |
download | spacetraders-dcd0a7a9b2612f383c1f627c72c27868c367058d.tar.gz spacetraders-dcd0a7a9b2612f383c1f627c72c27868c367058d.tar.bz2 spacetraders-dcd0a7a9b2612f383c1f627c72c27868c367058d.zip |
[haskell] abstracted away common database access patterns
Diffstat (limited to 'haskell/src/SpaceTraders/APIClient')
-rw-r--r-- | haskell/src/SpaceTraders/APIClient/Contracts.hs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/haskell/src/SpaceTraders/APIClient/Contracts.hs b/haskell/src/SpaceTraders/APIClient/Contracts.hs new file mode 100644 index 0000000..a62eb26 --- /dev/null +++ b/haskell/src/SpaceTraders/APIClient/Contracts.hs @@ -0,0 +1,30 @@ +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE OverloadedStrings #-} + +module SpaceTraders.APIClient.Contracts + ( myContracts + ) where + +import Network.HTTP.Simple + +import SpaceTraders +import SpaceTraders.APIClient.Client +import SpaceTraders.APIClient.Pagination +import SpaceTraders.Database.Contracts +import SpaceTraders.Model.Contract(Contract) + +myContracts :: SpaceTradersT (APIResponse [Contract]) +myContracts = do + listContracts' Pagination{limit=20, page=1, total=0} + where + listContracts' :: Pagination -> SpaceTradersT (APIResponse [Contract]) + listContracts' p = do + resp <- sendPaginated (Just p) $ setRequestPath "/v2/my/contracts" :: SpaceTradersT (APIPaginatedResponse [Contract]) + case resp of + Left e -> return $ Left e + Right (APIMessage r (Just p')) -> do + mapM_ setContract r + if (limit p' * page p' < total p') then listContracts' (nextPage p') + else Right <$> getContracts + _ -> undefined |