[haskell] Simplify API Message JSON decoding
This commit is contained in:
parent
f23fe74dbe
commit
d829fa5ca2
1 changed files with 3 additions and 5 deletions
|
@ -22,8 +22,8 @@ import Network.HTTP.Types.Status
|
||||||
|
|
||||||
import SpaceTraders.APIClient.Errors
|
import SpaceTraders.APIClient.Errors
|
||||||
|
|
||||||
data APIMessage = APIMessage { data_ :: Value } deriving (Show)
|
data FromJSON a => APIMessage a = APIMessage { data_ :: a } deriving (Show)
|
||||||
instance FromJSON APIMessage where
|
instance FromJSON a => FromJSON (APIMessage a) where
|
||||||
parseJSON (Object o) = APIMessage <$> o .: "data"
|
parseJSON (Object o) = APIMessage <$> o .: "data"
|
||||||
parseJSON _ = mzero
|
parseJSON _ = mzero
|
||||||
|
|
||||||
|
@ -49,9 +49,7 @@ send request = do
|
||||||
if status >= 200 && status <= 299
|
if status >= 200 && status <= 299
|
||||||
then case eitherDecode body of
|
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) Null (T.pack $ concat ["Error decoding JSON APIMessage: ", e])
|
||||||
Right r -> case fromJSONValue (data_ r) of
|
Right r -> return . Right $ data_ r
|
||||||
Left e -> return . Left $ APIError (-1001) Null (T.pack $ concat ["Error decoding JSON message contents: ", e])
|
|
||||||
Right m -> return $ Right m
|
|
||||||
else case eitherDecode body of
|
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])
|
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
|
Right e -> case apiErrorCode e of
|
||||||
|
|
Loading…
Add table
Reference in a new issue