[haskell] Refactored JSON parsing code
This commit is contained in:
parent
0f279a06d8
commit
8249bf432a
4 changed files with 22 additions and 29 deletions
|
@ -11,7 +11,6 @@ module SpaceTraders.APIClient.Client
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent
|
import Control.Concurrent
|
||||||
import Control.Monad
|
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.Aeson.Types
|
import Data.Aeson.Types
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
@ -23,8 +22,7 @@ import SpaceTraders.APIClient.Errors
|
||||||
|
|
||||||
data FromJSON a => APIMessage a = APIMessage { data_ :: a } deriving (Show)
|
data FromJSON a => APIMessage a = APIMessage { data_ :: a } deriving (Show)
|
||||||
instance FromJSON a => FromJSON (APIMessage a) where
|
instance FromJSON a => FromJSON (APIMessage a) where
|
||||||
parseJSON (Object o) = APIMessage <$> o .: "data"
|
parseJSON = withObject "APIMessage" $ \o -> APIMessage <$> o .: "data"
|
||||||
parseJSON _ = mzero
|
|
||||||
|
|
||||||
defaultReq :: Request
|
defaultReq :: Request
|
||||||
defaultReq = setRequestHost "api.spacetraders.io"
|
defaultReq = setRequestHost "api.spacetraders.io"
|
||||||
|
|
|
@ -6,7 +6,6 @@ module SpaceTraders.APIClient.Errors
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
import Control.Monad
|
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.Time
|
import Data.Time
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
@ -16,7 +15,7 @@ data APIError = APIError Int T.Text Value
|
||||||
deriving Show
|
deriving Show
|
||||||
instance Exception APIError
|
instance Exception APIError
|
||||||
instance FromJSON APIError where
|
instance FromJSON APIError where
|
||||||
parseJSON (Object o) = do
|
parseJSON = withObject "APIError" $ \o -> do
|
||||||
e <- o .: "error"
|
e <- o .: "error"
|
||||||
code <- e .: "code"
|
code <- e .: "code"
|
||||||
d <- e .: "data"
|
d <- e .: "data"
|
||||||
|
@ -25,7 +24,6 @@ instance FromJSON APIError where
|
||||||
_ -> APIError <$> pure code
|
_ -> APIError <$> pure code
|
||||||
<*> e .: "message"
|
<*> e .: "message"
|
||||||
<*> pure d
|
<*> pure d
|
||||||
parseJSON _ = mzero
|
|
||||||
|
|
||||||
data RateLimit = RateLimit { limitBurst :: Int
|
data RateLimit = RateLimit { limitBurst :: Int
|
||||||
, limitPerSecond :: Int
|
, limitPerSecond :: Int
|
||||||
|
@ -35,11 +33,10 @@ data RateLimit = RateLimit { limitBurst :: Int
|
||||||
, retryAfter :: Double
|
, retryAfter :: Double
|
||||||
} deriving Show
|
} deriving Show
|
||||||
instance FromJSON RateLimit where
|
instance FromJSON RateLimit where
|
||||||
parseJSON (Object o) = do
|
parseJSON = withObject "RateLimit" $ \o ->
|
||||||
RateLimit <$> o .: "limitBurst"
|
RateLimit <$> o .: "limitBurst"
|
||||||
<*> o .: "limitPerSecond"
|
<*> o .: "limitPerSecond"
|
||||||
<*> o .: "type"
|
<*> o .: "type"
|
||||||
<*> o .: "remaining"
|
<*> o .: "remaining"
|
||||||
<*> o .: "reset"
|
<*> o .: "reset"
|
||||||
<*> o .: "retryAfter"
|
<*> o .: "retryAfter"
|
||||||
parseJSON _ = mzero
|
|
||||||
|
|
|
@ -9,7 +9,6 @@ module SpaceTraders.Model.Contract
|
||||||
, Terms(..)
|
, Terms(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Monad
|
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.Time
|
import Data.Time
|
||||||
import GHC.Generics
|
import GHC.Generics
|
||||||
|
@ -25,7 +24,8 @@ data Contract = Contract { accepted :: Bool
|
||||||
, terms :: Terms
|
, terms :: Terms
|
||||||
} deriving (Generic, Show)
|
} deriving (Generic, Show)
|
||||||
instance FromJSON Contract where
|
instance FromJSON Contract where
|
||||||
parseJSON (Object o) = Contract <$> o .: "accepted"
|
parseJSON = withObject "Contract" $ \o ->
|
||||||
|
Contract <$> o .: "accepted"
|
||||||
<*> o .: "id"
|
<*> o .: "id"
|
||||||
<*> o .: "type"
|
<*> o .: "type"
|
||||||
<*> o .: "expiration"
|
<*> o .: "expiration"
|
||||||
|
@ -33,7 +33,6 @@ instance FromJSON Contract where
|
||||||
<*> o .: "factionSymbol"
|
<*> o .: "factionSymbol"
|
||||||
<*> o .: "fulfilled"
|
<*> o .: "fulfilled"
|
||||||
<*> o .: "terms"
|
<*> o .: "terms"
|
||||||
parseJSON _ = mzero
|
|
||||||
instance ToJSON Contract where
|
instance ToJSON Contract where
|
||||||
toEncoding (Contract a i ty e d fa fu te) = pairs ( "accepted" .= a
|
toEncoding (Contract a i ty e d fa fu te) = pairs ( "accepted" .= a
|
||||||
<> "id" .= i
|
<> "id" .= i
|
||||||
|
|
|
@ -7,7 +7,6 @@ module SpaceTraders.Model.Route
|
||||||
, RouteEndpoint(..)
|
, RouteEndpoint(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Monad
|
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.Time
|
import Data.Time
|
||||||
import GHC.Generics
|
import GHC.Generics
|
||||||
|
@ -26,12 +25,12 @@ data RouteEndpoint = RouteEndpoint { routeEndpointType :: T.Text
|
||||||
, y :: Int
|
, y :: Int
|
||||||
} deriving (Generic, Show)
|
} deriving (Generic, Show)
|
||||||
instance FromJSON RouteEndpoint where
|
instance FromJSON RouteEndpoint where
|
||||||
parseJSON (Object o) = RouteEndpoint <$> o .: "type"
|
parseJSON = withObject "RouteEndpoint" $ \o ->
|
||||||
|
RouteEndpoint <$> o .: "type"
|
||||||
<*> o .: "symbol"
|
<*> o .: "symbol"
|
||||||
<*> o .: "systemSymbol"
|
<*> o .: "systemSymbol"
|
||||||
<*> o .: "x"
|
<*> o .: "x"
|
||||||
<*> o .: "y"
|
<*> o .: "y"
|
||||||
parseJSON _ = mzero
|
|
||||||
instance ToJSON RouteEndpoint where
|
instance ToJSON RouteEndpoint where
|
||||||
toEncoding (RouteEndpoint t s ss xx yy) = pairs ( "type" .= t
|
toEncoding (RouteEndpoint t s ss xx yy) = pairs ( "type" .= t
|
||||||
<> "symbol" .= s
|
<> "symbol" .= s
|
||||||
|
|
Loading…
Add table
Reference in a new issue