From f23fe74dbe264cebe6e18eabac0600c4b06b5caf Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 7 Jul 2023 22:33:43 +0200 Subject: [haskell] back off when hitting the rate limit --- haskell/src/SpaceTraders/APIClient/Errors.hs | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 haskell/src/SpaceTraders/APIClient/Errors.hs (limited to 'haskell/src/SpaceTraders/APIClient/Errors.hs') diff --git a/haskell/src/SpaceTraders/APIClient/Errors.hs b/haskell/src/SpaceTraders/APIClient/Errors.hs new file mode 100644 index 0000000..4ab47bc --- /dev/null +++ b/haskell/src/SpaceTraders/APIClient/Errors.hs @@ -0,0 +1,42 @@ +{-# LANGUAGE OverloadedStrings #-} + +module SpaceTraders.APIClient.Errors + ( APIError(..) + , RateLimit(..) + ) where + +import Control.Exception +import Control.Monad +import Data.Aeson +import Data.Time +import qualified Data.Text as T + +data APIError = APIError { apiErrorCode :: Int + , apiErrorData :: Value + , apiErrorMessage :: T.Text + } deriving Show +instance Exception APIError +instance FromJSON APIError where + parseJSON (Object o) = do + e <- o .: "error" + APIError <$> e .: "code" + <*> e .: "data" + <*> e .: "message" + parseJSON _ = mzero + +data RateLimit = RateLimit { limitBurst :: Int + , limitPerSecond :: Int + , rateLimitType :: T.Text + , remaining :: Int + , reset :: UTCTime + , retryAfter :: Double + } deriving Show +instance FromJSON RateLimit where + parseJSON (Object o) = do + RateLimit <$> o .: "limitBurst" + <*> o .: "limitPerSecond" + <*> o .: "type" + <*> o .: "remaining" + <*> o .: "reset" + <*> o .: "retryAfter" + parseJSON _ = mzero -- cgit v1.2.3