implement --raw in local RPC

This commit is contained in:
Michele Guerini Rocco 2019-11-10 11:36:06 +01:00
parent d1f45bd43c
commit 8a885e8738
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450

View File

@ -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