module Morley.Client.Init
( MorleyClientConfig (..)
, mkMorleyClientEnv
, mkLogAction
, mccAliasPrefixL
, mccEndpointUrlL
, mccTezosClientPathL
, mccMbTezosClientDataDirL
, mccVerbosityL
, mccSecretKeyL
) where
import Colog (cmap, fmtMessage, logTextStderr, msgSeverity)
import Colog.Core (Severity(..), filterBySeverity)
import Morley.Util.Lens
import Servant.Client (BaseUrl(..))
import System.Environment (lookupEnv)
import Morley.Client.Env
import Morley.Client.Logging (ClientLogAction, logFlush)
import Morley.Client.RPC.HttpClient
import Morley.Client.TezosClient.Impl (getTezosClientConfig)
import Morley.Client.TezosClient.Types
import qualified Morley.Tezos.Crypto.Ed25519 as Ed25519
data MorleyClientConfig = MorleyClientConfig
{ MorleyClientConfig -> Maybe Text
mccAliasPrefix :: Maybe Text
, MorleyClientConfig -> Maybe BaseUrl
mccEndpointUrl :: Maybe BaseUrl
, MorleyClientConfig -> FilePath
mccTezosClientPath :: FilePath
, MorleyClientConfig -> Maybe FilePath
mccMbTezosClientDataDir :: Maybe FilePath
, MorleyClientConfig -> Word
mccVerbosity :: Word
, MorleyClientConfig -> Maybe SecretKey
mccSecretKey :: Maybe Ed25519.SecretKey
} deriving stock Int -> MorleyClientConfig -> ShowS
[MorleyClientConfig] -> ShowS
MorleyClientConfig -> FilePath
(Int -> MorleyClientConfig -> ShowS)
-> (MorleyClientConfig -> FilePath)
-> ([MorleyClientConfig] -> ShowS)
-> Show MorleyClientConfig
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [MorleyClientConfig] -> ShowS
$cshowList :: [MorleyClientConfig] -> ShowS
show :: MorleyClientConfig -> FilePath
$cshow :: MorleyClientConfig -> FilePath
showsPrec :: Int -> MorleyClientConfig -> ShowS
$cshowsPrec :: Int -> MorleyClientConfig -> ShowS
Show
makeLensesWith postfixLFields ''MorleyClientConfig
mkMorleyClientEnv :: MonadIO m => MorleyClientConfig -> IO (MorleyClientEnv' m)
mkMorleyClientEnv :: MorleyClientConfig -> IO (MorleyClientEnv' m)
mkMorleyClientEnv MorleyClientConfig{FilePath
Maybe FilePath
Maybe Text
Maybe BaseUrl
Maybe SecretKey
Word
mccSecretKey :: Maybe SecretKey
mccVerbosity :: Word
mccMbTezosClientDataDir :: Maybe FilePath
mccTezosClientPath :: FilePath
mccEndpointUrl :: Maybe BaseUrl
mccAliasPrefix :: Maybe Text
mccSecretKey :: MorleyClientConfig -> Maybe SecretKey
mccVerbosity :: MorleyClientConfig -> Word
mccMbTezosClientDataDir :: MorleyClientConfig -> Maybe FilePath
mccTezosClientPath :: MorleyClientConfig -> FilePath
mccEndpointUrl :: MorleyClientConfig -> Maybe BaseUrl
mccAliasPrefix :: MorleyClientConfig -> Maybe Text
..} = do
Maybe FilePath
envTezosClientPath <- FilePath -> IO (Maybe FilePath)
lookupEnv FilePath
"MORLEY_TEZOS_CLIENT"
let tezosClientPath :: FilePath
tezosClientPath = FilePath -> Maybe FilePath -> FilePath
forall a. a -> Maybe a -> a
fromMaybe FilePath
mccTezosClientPath Maybe FilePath
envTezosClientPath
TezosClientConfig {BaseUrl
tcEndpointUrl :: TezosClientConfig -> BaseUrl
tcEndpointUrl :: BaseUrl
..} <- FilePath -> Maybe FilePath -> IO TezosClientConfig
getTezosClientConfig FilePath
tezosClientPath Maybe FilePath
mccMbTezosClientDataDir
let
endpointUrl :: BaseUrl
endpointUrl = BaseUrl -> Maybe BaseUrl -> BaseUrl
forall a. a -> Maybe a -> a
fromMaybe BaseUrl
tcEndpointUrl Maybe BaseUrl
mccEndpointUrl
tezosClientEnv :: TezosClientEnv
tezosClientEnv = TezosClientEnv :: Maybe Text
-> BaseUrl -> FilePath -> Maybe FilePath -> TezosClientEnv
TezosClientEnv
{ tceAliasPrefix :: Maybe Text
tceAliasPrefix = Maybe Text
mccAliasPrefix
, tceEndpointUrl :: BaseUrl
tceEndpointUrl = BaseUrl
endpointUrl
, tceTezosClientPath :: FilePath
tceTezosClientPath = FilePath
tezosClientPath
, tceMbTezosClientDataDir :: Maybe FilePath
tceMbTezosClientDataDir = Maybe FilePath
mccMbTezosClientDataDir
}
ClientEnv
clientEnv <- BaseUrl -> IO ClientEnv
newClientEnv BaseUrl
endpointUrl
pure MorleyClientEnv :: forall (m :: * -> *).
TezosClientEnv
-> ClientLogAction m
-> Maybe SecretKey
-> ClientEnv
-> MorleyClientEnv' m
MorleyClientEnv
{ mceTezosClient :: TezosClientEnv
mceTezosClient = TezosClientEnv
tezosClientEnv
, mceLogAction :: ClientLogAction m
mceLogAction = Word -> ClientLogAction m
forall (m :: * -> *). MonadIO m => Word -> ClientLogAction m
mkLogAction Word
mccVerbosity
, mceSecretKey :: Maybe SecretKey
mceSecretKey = Maybe SecretKey
mccSecretKey
, mceClientEnv :: ClientEnv
mceClientEnv = ClientEnv
clientEnv
}
mkLogAction :: MonadIO m => Word -> ClientLogAction m
mkLogAction :: Word -> ClientLogAction m
mkLogAction Word
verbosity =
Severity
-> (Msg Severity -> Severity)
-> ClientLogAction m
-> ClientLogAction m
forall (m :: * -> *) a.
Applicative m =>
Severity -> (a -> Severity) -> LogAction m a -> LogAction m a
filterBySeverity Severity
severity Msg Severity -> Severity
forall sev. Msg sev -> sev
msgSeverity (Msg Severity -> Text
fmtMessage (Msg Severity -> Text) -> LogAction m Text -> ClientLogAction m
forall a b (m :: * -> *).
(a -> b) -> LogAction m b -> LogAction m a
`cmap` LogAction m Text
logTextStderrFlush)
where
severity :: Severity
severity = case Word
verbosity of
Word
0 -> Severity
Warning
Word
1 -> Severity
Info
Word
_ -> Severity
Debug
logTextStderrFlush :: LogAction m Text
logTextStderrFlush = LogAction m Text
forall (m :: * -> *). MonadIO m => LogAction m Text
logTextStderr LogAction m Text -> LogAction m Text -> LogAction m Text
forall a. Semigroup a => a -> a -> a
<> Handle -> LogAction m Text
forall (m :: * -> *) a. MonadIO m => Handle -> LogAction m a
logFlush Handle
stderr