|
| 1 | +{-# LANGUAGE DeriveGeneric #-} |
| 2 | +{-# LANGUAGE LambdaCase #-} |
| 3 | + |
| 4 | +module Cardano.Node.Client |
| 5 | + ( -- Node Client |
| 6 | + NodeClient(..) |
| 7 | + , ClientError(..) |
| 8 | + , fromServantError |
| 9 | + |
| 10 | + -- * HTTP instance |
| 11 | + , NodeHttpClient |
| 12 | + , mkHttpClient |
| 13 | + ) where |
| 14 | + |
| 15 | +import Universum |
| 16 | + |
| 17 | +import Data.Aeson (FromJSON) |
| 18 | +import qualified Data.Aeson as Aeson |
| 19 | +import Network.HTTP.Client (Manager) |
| 20 | +import Network.HTTP.Media.MediaType (MediaType) |
| 21 | +import Servant ((:<|>) (..)) |
| 22 | +import Servant.Client (BaseUrl (..), ClientEnv (..), ClientM, |
| 23 | + GenResponse (..), Response, ServantError, client, |
| 24 | + runClientM) |
| 25 | +import qualified Servant.Client as Servant |
| 26 | + |
| 27 | +import Cardano.Node.API (nodeV1Api) |
| 28 | +import Pos.Chain.Txp (Utxo) |
| 29 | +import Pos.Node.API (ForceNtpCheck, NodeInfo, NodeSettings) |
| 30 | +import Pos.Util.Jsend (ResponseStatus (..)) |
| 31 | +import Pos.Util.Servant (APIResponse (..)) |
| 32 | +import Pos.Web.Types (CConfirmedProposalState) |
| 33 | + |
| 34 | + |
| 35 | +-- * Node Client |
| 36 | + |
| 37 | +data NodeClient m |
| 38 | + = NodeClient |
| 39 | + { getUtxo |
| 40 | + :: m Utxo |
| 41 | + |
| 42 | + , getConfirmedProposals |
| 43 | + :: m [CConfirmedProposalState] |
| 44 | + |
| 45 | + , getNodeSettings |
| 46 | + :: m NodeSettings |
| 47 | + |
| 48 | + , getNodeInfo |
| 49 | + :: ForceNtpCheck |
| 50 | + -> m NodeInfo |
| 51 | + |
| 52 | + , applyUpdate |
| 53 | + :: m () |
| 54 | + |
| 55 | + , postponeUpdate |
| 56 | + :: m () |
| 57 | + } deriving (Generic) |
| 58 | + |
| 59 | + |
| 60 | +data ClientError a |
| 61 | + = KnownError a |
| 62 | + | DecodeFailure Text Response |
| 63 | + | UnsupportedContentType MediaType Response |
| 64 | + | InvalidContentTypeHeader Response |
| 65 | + | ConnectionError Text |
| 66 | + deriving (Show, Generic, Eq) |
| 67 | +instance Exception a => Exception (ClientError a) |
| 68 | + |
| 69 | +fromServantError :: FromJSON a => ServantError -> ClientError a |
| 70 | +fromServantError = \case |
| 71 | + Servant.FailureResponse r@(Response _ _ _ body) -> |
| 72 | + case Aeson.decode body of |
| 73 | + Just (APIResponse a ErrorStatus _) -> |
| 74 | + KnownError a |
| 75 | + Just _ -> |
| 76 | + DecodeFailure "API failed with non-error response ?!?" r |
| 77 | + Nothing -> |
| 78 | + DecodeFailure "Invalid / Non-JSEnd API Error Response" r |
| 79 | + Servant.DecodeFailure t r -> |
| 80 | + DecodeFailure t r |
| 81 | + Servant.UnsupportedContentType m r -> |
| 82 | + UnsupportedContentType m r |
| 83 | + Servant.InvalidContentTypeHeader r -> |
| 84 | + InvalidContentTypeHeader r |
| 85 | + Servant.ConnectionError t -> |
| 86 | + ConnectionError t |
| 87 | + |
| 88 | + |
| 89 | +-- * HTTP Instance |
| 90 | + |
| 91 | +type NodeHttpClient = NodeClient (ExceptT (ClientError ()) IO) |
| 92 | + |
| 93 | +mkHttpClient |
| 94 | + :: BaseUrl |
| 95 | + -> Manager |
| 96 | + -> NodeHttpClient |
| 97 | +mkHttpClient baseUrl manager = NodeClient |
| 98 | + { getUtxo = |
| 99 | + run getUtxoR |
| 100 | + , getConfirmedProposals = |
| 101 | + run getConfirmedProposalsR |
| 102 | + , getNodeSettings = |
| 103 | + fmap wrData $ run getNodeSettingsR |
| 104 | + , getNodeInfo = |
| 105 | + fmap wrData . run . getNodeInfoR |
| 106 | + , applyUpdate = |
| 107 | + void $ run applyUpdateR |
| 108 | + , postponeUpdate = |
| 109 | + void $ run postponeUpdateR |
| 110 | + } |
| 111 | + where |
| 112 | + run :: forall a. ClientM a -> ExceptT (ClientError ()) IO a |
| 113 | + run = ExceptT |
| 114 | + . fmap (first fromServantError) |
| 115 | + . flip runClientM (ClientEnv manager baseUrl noCookieJar) |
| 116 | + |
| 117 | + noCookieJar = Nothing |
| 118 | + |
| 119 | + ( getNodeSettingsR |
| 120 | + :<|> getNodeInfoR |
| 121 | + :<|> applyUpdateR |
| 122 | + :<|> postponeUpdateR |
| 123 | + ):<|>( getUtxoR |
| 124 | + :<|> getConfirmedProposalsR |
| 125 | + ) = client nodeV1Api |
0 commit comments