diff options
author | Julien Dessaux | 2023-07-09 14:31:31 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-07-09 14:31:31 +0200 |
commit | 0f279a06d87c1bbe742ed102b60b86ef74807ad0 (patch) | |
tree | d3405b2cb90ebd32ade8e788ca82ae375acaf6f0 | |
parent | [haskell] Simplify API Message JSON decoding (diff) | |
download | spacetraders-0f279a06d87c1bbe742ed102b60b86ef74807ad0.tar.gz spacetraders-0f279a06d87c1bbe742ed102b60b86ef74807ad0.tar.bz2 spacetraders-0f279a06d87c1bbe742ed102b60b86ef74807ad0.zip |
[haskell] Simplify API Error JSON decoding
Diffstat (limited to '')
-rw-r--r-- | haskell/src/SpaceTraders/APIClient/Client.hs | 23 | ||||
-rw-r--r-- | haskell/src/SpaceTraders/APIClient/Errors.hs | 17 |
2 files changed, 16 insertions, 24 deletions
diff --git a/haskell/src/SpaceTraders/APIClient/Client.hs b/haskell/src/SpaceTraders/APIClient/Client.hs index 00cab11..0bf92a8 100644 --- a/haskell/src/SpaceTraders/APIClient/Client.hs +++ b/haskell/src/SpaceTraders/APIClient/Client.hs @@ -11,7 +11,6 @@ module SpaceTraders.APIClient.Client ) where import Control.Concurrent -import Control.Exception import Control.Monad import Data.Aeson import Data.Aeson.Types @@ -48,21 +47,11 @@ send request = do body = getResponseBody response if status >= 200 && status <= 299 then case eitherDecode body of - Left e -> return . Left $ APIError (-1000) Null (T.pack $ concat ["Error decoding JSON APIMessage: ", e]) + Left e -> return . Left $ APIError (-1000) (T.pack $ concat ["Error decoding JSON APIMessage: ", e]) Null Right r -> return . Right $ data_ r else case eitherDecode body of - Left e -> return . Left $ APIError (-status) Null (T.pack $ concat ["Error decoding JSON APIError: ", e, ". Got HTTP body: ", show body]) - Right e -> case apiErrorCode e of - 429 -> do -- We are being rate limited - let d = apiErrorData e - w <- case fromJSONValue d of - Left _ -> throwIO e - Right e' -> return $ retryAfter e' - threadDelay (1_000_000 * (round w)) - send request - _ -> return $ Left e - ---handleAPIError :: SomeException -> IO (Maybe RegisterMessage) ---handleAPIError e = do --- print e --- return Nothing + Left e -> return . Left $ APIError (-status) (T.pack $ concat ["Error decoding JSON APIError: ", e, ". Got HTTP body: ", show body]) Null + Right (APIRateLimit r) -> do + threadDelay (1_000_000 * (round $ retryAfter r)) + send request + Right e -> return $ Left e diff --git a/haskell/src/SpaceTraders/APIClient/Errors.hs b/haskell/src/SpaceTraders/APIClient/Errors.hs index 4ab47bc..e4e5513 100644 --- a/haskell/src/SpaceTraders/APIClient/Errors.hs +++ b/haskell/src/SpaceTraders/APIClient/Errors.hs @@ -11,17 +11,20 @@ import Data.Aeson import Data.Time import qualified Data.Text as T -data APIError = APIError { apiErrorCode :: Int - , apiErrorData :: Value - , apiErrorMessage :: T.Text - } deriving Show +data APIError = APIError Int T.Text Value + | APIRateLimit RateLimit + deriving Show instance Exception APIError instance FromJSON APIError where parseJSON (Object o) = do e <- o .: "error" - APIError <$> e .: "code" - <*> e .: "data" - <*> e .: "message" + code <- e .: "code" + d <- e .: "data" + case code of + 429 -> APIRateLimit <$> parseJSON d + _ -> APIError <$> pure code + <*> e .: "message" + <*> pure d parseJSON _ = mzero data RateLimit = RateLimit { limitBurst :: Int |