diff options
author | Julien Dessaux | 2023-07-10 00:31:13 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-07-10 00:31:13 +0200 |
commit | 8249bf432abcf4aad261a79ac2d802c0ac3a0391 (patch) | |
tree | d9d89135dcefe6c388b8fa7789288bc431634ecf /haskell/src/SpaceTraders/APIClient/Errors.hs | |
parent | [haskell] Simplify API Error JSON decoding (diff) | |
download | spacetraders-8249bf432abcf4aad261a79ac2d802c0ac3a0391.tar.gz spacetraders-8249bf432abcf4aad261a79ac2d802c0ac3a0391.tar.bz2 spacetraders-8249bf432abcf4aad261a79ac2d802c0ac3a0391.zip |
[haskell] Refactored JSON parsing code
Diffstat (limited to '')
-rw-r--r-- | haskell/src/SpaceTraders/APIClient/Errors.hs | 7 |
1 files changed, 2 insertions, 5 deletions
diff --git a/haskell/src/SpaceTraders/APIClient/Errors.hs b/haskell/src/SpaceTraders/APIClient/Errors.hs index e4e5513..a7f5d21 100644 --- a/haskell/src/SpaceTraders/APIClient/Errors.hs +++ b/haskell/src/SpaceTraders/APIClient/Errors.hs @@ -6,7 +6,6 @@ module SpaceTraders.APIClient.Errors ) where import Control.Exception -import Control.Monad import Data.Aeson import Data.Time import qualified Data.Text as T @@ -16,7 +15,7 @@ data APIError = APIError Int T.Text Value deriving Show instance Exception APIError instance FromJSON APIError where - parseJSON (Object o) = do + parseJSON = withObject "APIError" $ \o -> do e <- o .: "error" code <- e .: "code" d <- e .: "data" @@ -25,7 +24,6 @@ instance FromJSON APIError where _ -> APIError <$> pure code <*> e .: "message" <*> pure d - parseJSON _ = mzero data RateLimit = RateLimit { limitBurst :: Int , limitPerSecond :: Int @@ -35,11 +33,10 @@ data RateLimit = RateLimit { limitBurst :: Int , retryAfter :: Double } deriving Show instance FromJSON RateLimit where - parseJSON (Object o) = do + parseJSON = withObject "RateLimit" $ \o -> RateLimit <$> o .: "limitBurst" <*> o .: "limitPerSecond" <*> o .: "type" <*> o .: "remaining" <*> o .: "reset" <*> o .: "retryAfter" - parseJSON _ = mzero |