1
0
Fork 0

[haskell] Add a SpaceTradersT and handle server reset api message

This commit is contained in:
Julien Dessaux 2023-07-16 23:31:00 +02:00
parent 7bd1c116c2
commit e084d260ff
Signed by: adyxax
GPG key ID: F92E51B86E07177E
6 changed files with 110 additions and 31 deletions

View file

@ -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

View file

@ -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)