Browse Source

BrokerProtocol: new response for error

master
Denis Tereshkin 9 years ago
parent
commit
dc450d6206
  1. 5
      libatrade.cabal
  2. 7
      src/ATrade/Broker/Protocol.hs
  3. 3
      test/ArbitraryInstances.hs

5
libatrade.cabal

@ -15,6 +15,7 @@ cabal-version: >=1.10
library library
hs-source-dirs: src hs-source-dirs: src
ghc-options: -Wincomplete-patterns
exposed-modules: ATrade.Types exposed-modules: ATrade.Types
, ATrade.QuoteSource.Server , ATrade.QuoteSource.Server
, ATrade.Broker.Protocol , ATrade.Broker.Protocol
@ -37,7 +38,7 @@ library
executable libatrade-exe executable libatrade-exe
hs-source-dirs: app hs-source-dirs: app
main-is: Main.hs main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wincomplete-patterns
build-depends: base build-depends: base
, libatrade , libatrade
, pretty-hex , pretty-hex
@ -69,7 +70,7 @@ test-suite libatrade-test
, bytestring , bytestring
, monad-loops , monad-loops
, uuid , uuid
ghc-options: -threaded -rtsopts -with-rtsopts=-N ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wincomplete-patterns
default-language: Haskell2010 default-language: Haskell2010
other-modules: ArbitraryInstances other-modules: ArbitraryInstances
, TestBrokerProtocol , TestBrokerProtocol

7
src/ATrade/Broker/Protocol.hs

@ -47,6 +47,7 @@ instance ToJSON BrokerServerRequest where
data BrokerServerResponse = ResponseOrderSubmitted OrderId data BrokerServerResponse = ResponseOrderSubmitted OrderId
| ResponseOrderCancelled OrderId | ResponseOrderCancelled OrderId
| ResponseNotifications [Notification] | ResponseNotifications [Notification]
| ResponseError T.Text
deriving (Eq, Show) deriving (Eq, Show)
instance FromJSON BrokerServerResponse where instance FromJSON BrokerServerResponse where
@ -59,12 +60,16 @@ instance FromJSON BrokerServerResponse where
return $ ResponseOrderCancelled oid return $ ResponseOrderCancelled oid
| HM.member "notifications" obj -> do | HM.member "notifications" obj -> do
notifications <- obj .: "notifications" notifications <- obj .: "notifications"
ResponseNotifications <$> parseJSON notifications) ResponseNotifications <$> parseJSON notifications
| HM.member "error" obj -> do
error <- obj .: "error"
ResponseError <$> parseJSON error)
instance ToJSON BrokerServerResponse where instance ToJSON BrokerServerResponse where
toJSON (ResponseOrderSubmitted oid) = object [ "order-id" .= oid ] toJSON (ResponseOrderSubmitted oid) = object [ "order-id" .= oid ]
toJSON (ResponseOrderCancelled oid) = object [ "order-cancelled" .= oid ] toJSON (ResponseOrderCancelled oid) = object [ "order-cancelled" .= oid ]
toJSON (ResponseNotifications notifications) = object [ "notifications" .= notifications ] toJSON (ResponseNotifications notifications) = object [ "notifications" .= notifications ]
toJSON (ResponseError errorMessage) = object [ "error" .= errorMessage ]
data Notification = OrderNotification OrderId OrderState | TradeNotification Trade data Notification = OrderNotification OrderId OrderState | TradeNotification Trade
deriving (Eq, Show) deriving (Eq, Show)

3
test/ArbitraryInstances.hs

@ -111,8 +111,9 @@ instance Arbitrary BrokerServerRequest where
instance Arbitrary BrokerServerResponse where instance Arbitrary BrokerServerResponse where
arbitrary = do arbitrary = do
t <- choose (1, 3) :: Gen Int t <- choose (1, 4) :: Gen Int
if | t == 1 -> ResponseOrderSubmitted <$> arbitrary if | t == 1 -> ResponseOrderSubmitted <$> arbitrary
| t == 2 -> ResponseOrderCancelled <$> arbitrary | t == 2 -> ResponseOrderCancelled <$> arbitrary
| t == 3 -> ResponseNotifications <$> arbitrary | t == 3 -> ResponseNotifications <$> arbitrary
| t == 4 -> ResponseError <$> arbitrary

Loading…
Cancel
Save