diff options
author | Julien Dessaux | 2023-07-07 22:33:43 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-07-07 22:34:09 +0200 |
commit | f23fe74dbe264cebe6e18eabac0600c4b06b5caf (patch) | |
tree | b862a89ce02cdaeaf19e9539749e48c3c7881621 /haskell/src/SpaceTraders/APIClient/Errors.hs | |
parent | [javascript] Updated dependencies (diff) | |
download | spacetraders-f23fe74dbe264cebe6e18eabac0600c4b06b5caf.tar.gz spacetraders-f23fe74dbe264cebe6e18eabac0600c4b06b5caf.tar.bz2 spacetraders-f23fe74dbe264cebe6e18eabac0600c4b06b5caf.zip |
[haskell] back off when hitting the rate limit
Diffstat (limited to '')
-rw-r--r-- | haskell/src/SpaceTraders/APIClient/Errors.hs | 42 |
1 files changed, 42 insertions, 0 deletions
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 |