summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/APIClient
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-16 23:31:00 +0200
committerJulien Dessaux2023-07-17 00:15:05 +0200
commite084d260ff0439f04ab62eba7229309d79ed55c4 (patch)
treee2355a9dad8123352474cb8533d625f87fdcde0d /haskell/src/SpaceTraders/APIClient
parent[haskell] Implemented pagination and systems list api call (diff)
downloadspacetraders-e084d260ff0439f04ab62eba7229309d79ed55c4.tar.gz
spacetraders-e084d260ff0439f04ab62eba7229309d79ed55c4.tar.bz2
spacetraders-e084d260ff0439f04ab62eba7229309d79ed55c4.zip
[haskell] Add a SpaceTradersT and handle server reset api message
Diffstat (limited to 'haskell/src/SpaceTraders/APIClient')
-rw-r--r--haskell/src/SpaceTraders/APIClient/Agent.hs9
-rw-r--r--haskell/src/SpaceTraders/APIClient/Errors.hs10
2 files changed, 16 insertions, 3 deletions
diff --git a/haskell/src/SpaceTraders/APIClient/Agent.hs b/haskell/src/SpaceTraders/APIClient/Agent.hs
index 7773972..023a4f4 100644
--- a/haskell/src/SpaceTraders/APIClient/Agent.hs
+++ b/haskell/src/SpaceTraders/APIClient/Agent.hs
@@ -13,14 +13,17 @@ import GHC.Generics
import qualified Data.Text as T
import Network.HTTP.Simple
+import qualified SpaceTraders as ST
import SpaceTraders.APIClient.Client
import SpaceTraders.Model.Agent(Agent)
import SpaceTraders.Model.Ship(Ship)
import SpaceTraders.Model.Contract
-myAgent :: T.Text -> IO (APIResponse Agent)
-myAgent t = send $ setRequestPath "/v2/my/agent"
- $ tokenReq t
+myAgent :: ST.SpaceTradersT (APIResponse Agent)
+myAgent = do
+ c <- ST.ask
+ ST.liftIO $ send $ setRequestPath "/v2/my/agent"
+ $ tokenReq (ST.token c)
data RegisterRequest = RegisterRequest { faction :: T.Text
, symbol :: T.Text
diff --git a/haskell/src/SpaceTraders/APIClient/Errors.hs b/haskell/src/SpaceTraders/APIClient/Errors.hs
index a7f5d21..2b74784 100644
--- a/haskell/src/SpaceTraders/APIClient/Errors.hs
+++ b/haskell/src/SpaceTraders/APIClient/Errors.hs
@@ -1,17 +1,22 @@
+{-# LANGUAGE DeriveAnyClass #-}
+{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module SpaceTraders.APIClient.Errors
( APIError(..)
, RateLimit(..)
+ , ResetHappened(..)
) where
import Control.Exception
import Data.Aeson
import Data.Time
import qualified Data.Text as T
+import GHC.Generics
data APIError = APIError Int T.Text Value
| APIRateLimit RateLimit
+ | APIResetHappened ResetHappened
deriving Show
instance Exception APIError
instance FromJSON APIError where
@@ -20,6 +25,7 @@ instance FromJSON APIError where
code <- e .: "code"
d <- e .: "data"
case code of
+ 401 -> APIResetHappened <$> parseJSON d
429 -> APIRateLimit <$> parseJSON d
_ -> APIError <$> pure code
<*> e .: "message"
@@ -40,3 +46,7 @@ instance FromJSON RateLimit where
<*> o .: "remaining"
<*> o .: "reset"
<*> o .: "retryAfter"
+
+data ResetHappened = ResetHappened { actual :: T.Text
+ , expected :: T.Text
+ } deriving (FromJSON, Generic, Show, ToJSON)