summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-29 00:32:36 +0200
committerJulien Dessaux2023-07-29 00:32:36 +0200
commit961625c363193905e58221bdc895a19f9f153cce (patch)
tree173f8f284b68be4046f8ef22db654195bfcd7da2 /haskell/src/SpaceTraders
parent[haskell] implemented ships's availability tracking in the database (diff)
downloadspacetraders-961625c363193905e58221bdc895a19f9f153cce.tar.gz
spacetraders-961625c363193905e58221bdc895a19f9f153cce.tar.bz2
spacetraders-961625c363193905e58221bdc895a19f9f153cce.zip
[haskell] implemented dock and orbit api calls
Diffstat (limited to 'haskell/src/SpaceTraders')
-rw-r--r--haskell/src/SpaceTraders/APIClient/Ships.hs33
1 files changed, 31 insertions, 2 deletions
diff --git a/haskell/src/SpaceTraders/APIClient/Ships.hs b/haskell/src/SpaceTraders/APIClient/Ships.hs
index e2cf15c..f4b7290 100644
--- a/haskell/src/SpaceTraders/APIClient/Ships.hs
+++ b/haskell/src/SpaceTraders/APIClient/Ships.hs
@@ -3,16 +3,34 @@
{-# LANGUAGE OverloadedStrings #-}
module SpaceTraders.APIClient.Ships
- ( myShips
+ ( dock
+ , myShips
+ , orbit
) where
+import Data.Aeson.Types
+import qualified Data.Text.Encoding as T
+import GHC.Generics
import Network.HTTP.Simple
import SpaceTraders
import SpaceTraders.APIClient.Client
import SpaceTraders.APIClient.Pagination
import SpaceTraders.Database.Ships
-import SpaceTraders.Model.Ship(Ship)
+import SpaceTraders.Model.Nav
+import SpaceTraders.Model.Ship
+
+data NavMessage = NavMessage { nav :: Nav } deriving (FromJSON, Generic, Show)
+dock :: Ship -> SpaceTradersT (APIResponse Ship)
+dock ship = do
+ resp <- send $ setRequestPath (T.encodeUtf8 $ mconcat ["/v2/my/ships/", symbol ship, "/dock"])
+ . setRequestMethod "POST" :: SpaceTradersT (APIResponse NavMessage)
+ case resp of
+ Left e -> return $ Left e
+ Right (NavMessage n) -> do
+ let s = ship{SpaceTraders.Model.Ship.nav=n}
+ updateShip s Nothing
+ return $ Right s
myShips :: SpaceTradersT (APIResponse [Ship])
myShips = do
@@ -28,3 +46,14 @@ myShips = do
if (limit p' * page p' < total p') then listShips' (nextPage p')
else Right <$> getShips
_ -> undefined
+
+orbit :: Ship -> SpaceTradersT (APIResponse Ship)
+orbit ship = do
+ resp <- send $ setRequestPath (T.encodeUtf8 $ mconcat ["/v2/my/ships/", symbol ship, "/orbit"])
+ . setRequestMethod "POST" :: SpaceTradersT (APIResponse NavMessage)
+ case resp of
+ Left e -> return $ Left e
+ Right (NavMessage n) -> do
+ let s = ship{SpaceTraders.Model.Ship.nav=n}
+ updateShip s Nothing
+ return $ Right s