diff options
Diffstat (limited to '')
-rw-r--r-- | haskell/src/SpaceTraders/APIClient/Agent.hs | 9 | ||||
-rw-r--r-- | haskell/src/SpaceTraders/APIClient/Errors.hs | 10 |
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) |