summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/APIClient
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-20 00:24:31 +0200
committerJulien Dessaux2023-07-20 00:24:31 +0200
commit24f6c8eb90555b81a96b142fec8057b05d334035 (patch)
treed85d9f71cb9dc6472d6cb4e95a0d2ca256308ce8 /haskell/src/SpaceTraders/APIClient
parent[haskell] Fixed models' serialization (diff)
downloadspacetraders-24f6c8eb90555b81a96b142fec8057b05d334035.tar.gz
spacetraders-24f6c8eb90555b81a96b142fec8057b05d334035.tar.bz2
spacetraders-24f6c8eb90555b81a96b142fec8057b05d334035.zip
[haskell] Finalized the agent initialization, refresh and reset
Diffstat (limited to 'haskell/src/SpaceTraders/APIClient')
-rw-r--r--haskell/src/SpaceTraders/APIClient/Client.hs3
-rw-r--r--haskell/src/SpaceTraders/APIClient/Ships.hs24
-rw-r--r--haskell/src/SpaceTraders/APIClient/Systems.hs10
3 files changed, 15 insertions, 22 deletions
diff --git a/haskell/src/SpaceTraders/APIClient/Client.hs b/haskell/src/SpaceTraders/APIClient/Client.hs
index c7b58e0..1ca03ee 100644
--- a/haskell/src/SpaceTraders/APIClient/Client.hs
+++ b/haskell/src/SpaceTraders/APIClient/Client.hs
@@ -2,8 +2,7 @@
{-# LANGUAGE OverloadedStrings #-}
module SpaceTraders.APIClient.Client
- ( APIError(..)
- , APIMessage(..)
+ ( APIMessage(..)
, APIPaginatedResponse
, APIResponse
, defaultReq
diff --git a/haskell/src/SpaceTraders/APIClient/Ships.hs b/haskell/src/SpaceTraders/APIClient/Ships.hs
index 0efbb5d..e2cf15c 100644
--- a/haskell/src/SpaceTraders/APIClient/Ships.hs
+++ b/haskell/src/SpaceTraders/APIClient/Ships.hs
@@ -3,34 +3,28 @@
{-# LANGUAGE OverloadedStrings #-}
module SpaceTraders.APIClient.Ships
- ( listShips
+ ( myShips
) where
-import Control.Exception
---import qualified Data.Text as T
---import qualified Database.SQLite.Simple as S
import Network.HTTP.Simple
import SpaceTraders
import SpaceTraders.APIClient.Client
import SpaceTraders.APIClient.Pagination
---import SpaceTraders.Database.Ships
+import SpaceTraders.Database.Ships
import SpaceTraders.Model.Ship(Ship)
-import Debug.Trace
-listShips :: SpaceTradersT (APIResponse [Ship])
-listShips = do
+myShips :: SpaceTradersT (APIResponse [Ship])
+myShips = do
listShips' Pagination{limit=20, page=1, total=0}
where
listShips' :: Pagination -> SpaceTradersT (APIResponse [Ship])
listShips' p = do
- resp <- sendPaginated (Just p) $ setRequestPath "/v2/my/ships"
+ resp <- sendPaginated (Just p) $ setRequestPath "/v2/my/ships" :: SpaceTradersT (APIPaginatedResponse [Ship])
case resp of
- Left e -> throw e
+ Left e -> return $ Left e
Right (APIMessage r (Just p')) -> do
- liftIO $ traceIO $ show p'
- --if (length r == 0 || ((page p') * (limit p')
- --addShips conn r
- --listShips' (nextPage p')
- return $ Right r
+ mapM_ setShip r
+ if (limit p' * page p' < total p') then listShips' (nextPage p')
+ else Right <$> getShips
_ -> undefined
diff --git a/haskell/src/SpaceTraders/APIClient/Systems.hs b/haskell/src/SpaceTraders/APIClient/Systems.hs
index 2e275ee..eb15b4d 100644
--- a/haskell/src/SpaceTraders/APIClient/Systems.hs
+++ b/haskell/src/SpaceTraders/APIClient/Systems.hs
@@ -3,7 +3,7 @@
{-# LANGUAGE OverloadedStrings #-}
module SpaceTraders.APIClient.Systems
- ( listSystems
+ ( initSystems
) where
import Control.Exception
@@ -15,8 +15,8 @@ import SpaceTraders.APIClient.Pagination
import SpaceTraders.Database.Systems
import SpaceTraders.Model.System(System)
-listSystems :: SpaceTradersT (APIResponse [System])
-listSystems = do
+initSystems :: SpaceTradersT (APIResponse [System])
+initSystems = do
s <- getSystems
listSystems' Pagination{limit=20, page=((length s) `div` 20) + 1, total=0}
where
@@ -25,8 +25,8 @@ listSystems = do
resp <- sendPaginated (Just p) $ setRequestPath "/v2/systems"
case resp of
Left e -> throw e
- Right (APIMessage [] _) -> Right <$> getSystems
Right (APIMessage r (Just p')) -> do
addSystems r
- listSystems' (nextPage p')
+ if (limit p' * page p' < total p') then listSystems' (nextPage p')
+ else Right <$> getSystems
_ -> undefined