From 8a885e8738daa3f0581ffd5b40c0ab575acbbe41 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Sun, 10 Nov 2019 11:36:06 +0100 Subject: [PATCH] implement --raw in local RPC --- src/Main.hs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index e7adec6..46f3f29 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -9,8 +9,8 @@ import Json -- Networking import Namecoin (rpcRequest, uri) -import Network.URI.Encode (encode) import Network.Wreq (get, responseBody) +import qualified Network.URI.Encode as U -- IO import Options.Applicative @@ -19,7 +19,7 @@ import qualified Data.Text.IO as T -- Data manipulation import Data.Text (Text) -import Data.Aeson (Value(..), decode, toJSON) +import Data.Aeson (Value(..), encode, decode, toJSON) import Data.HashMap.Strict (delete) import Data.ByteString.Lazy.Char8 (pack, unpack) @@ -52,16 +52,16 @@ options = Options <> short 'u' <> value "http://namecoin.dns" <> metavar "URL" - <> help "Use custom api url" ) + <> help "Use custom API URL" ) <*> (optional $ strOption $ long "conf" <> short 'c' <> metavar "FILE" - <> help "Use custom api url" ) + <> help "Use custom namecoin config file" ) <*> switch ( long "dnschain" <> short 'd' - <> help "Use dnschain api" ) + <> help "Use dnschain API " ) <*> switch ( long "block" <> short 'b' @@ -89,7 +89,7 @@ main = execParser description >>= exec where exec Options{..} = if dnschain then doDnschain url name raw block - else doLocal name block conf + else doLocal name raw block conf -- | Load namecoin configuration @@ -103,15 +103,18 @@ apiURI path = do -- | Connect to local namecoin node -doLocal :: String -> Bool -> Maybe FilePath -> IO () -doLocal name block conf = do +doLocal :: String -> Bool -> Bool -> Maybe FilePath -> IO () +doLocal name raw block conf = do uri <- apiURI conf req <- rpcRequest uri "name_show" [name] case req of Left err -> putStrLn ("The lookup failed: " ++ err) Right (Object res) -> do - pprint $ tryParse (res |: "value") - when block (pprint blockInfo) + if raw + then putStrLn (unpack $ encode res) + else do + pprint $ tryParse (res |: "value") + when block (pprint blockInfo) where tryParse = fromMaybe (res |. "value") . decode . pack blockInfo = toJSON (delete "value" res) @@ -120,7 +123,7 @@ doLocal name block conf = do -- | Connect to dnschain API endpoint doDnschain :: String -> String -> Bool -> Bool -> IO () doDnschain url name raw block = do - body <- view responseBody <$> get (url++"/v1/namecoin/key/"++encode name) + body <- view responseBody <$> get (url++"/v1/namecoin/key/"++U.encode name) if raw then putStrLn (unpack body) else do