diff options
author | Julien Dessaux | 2023-07-09 05:49:09 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-07-09 05:49:09 +0200 |
commit | d829fa5ca23a2a19cd4db7018404f1f25d6f8a4e (patch) | |
tree | f98f58111458374c15a9fc9870a197921086ce96 /haskell/src/SpaceTraders/APIClient | |
parent | [haskell] back off when hitting the rate limit (diff) | |
download | spacetraders-d829fa5ca23a2a19cd4db7018404f1f25d6f8a4e.tar.gz spacetraders-d829fa5ca23a2a19cd4db7018404f1f25d6f8a4e.tar.bz2 spacetraders-d829fa5ca23a2a19cd4db7018404f1f25d6f8a4e.zip |
[haskell] Simplify API Message JSON decoding
Diffstat (limited to '')
-rw-r--r-- | haskell/src/SpaceTraders/APIClient/Client.hs | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/haskell/src/SpaceTraders/APIClient/Client.hs b/haskell/src/SpaceTraders/APIClient/Client.hs index 4e9e9d1..00cab11 100644 --- a/haskell/src/SpaceTraders/APIClient/Client.hs +++ b/haskell/src/SpaceTraders/APIClient/Client.hs @@ -22,8 +22,8 @@ import Network.HTTP.Types.Status import SpaceTraders.APIClient.Errors -data APIMessage = APIMessage { data_ :: Value } deriving (Show) -instance FromJSON APIMessage where +data FromJSON a => APIMessage a = APIMessage { data_ :: a } deriving (Show) +instance FromJSON a => FromJSON (APIMessage a) where parseJSON (Object o) = APIMessage <$> o .: "data" parseJSON _ = mzero @@ -49,9 +49,7 @@ send request = do 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]) - Right r -> case fromJSONValue (data_ r) of - Left e -> return . Left $ APIError (-1001) Null (T.pack $ concat ["Error decoding JSON message contents: ", e]) - Right m -> return $ Right m + 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 |