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 -- Networking
import Namecoin (rpcRequest, uri) import Namecoin (rpcRequest, uri)
import Network.URI.Encode (encode)
import Network.Wreq (get, responseBody) import Network.Wreq (get, responseBody)
import qualified Network.URI.Encode as U
-- IO -- IO
import Options.Applicative import Options.Applicative
@ -19,7 +19,7 @@ import qualified Data.Text.IO as T
-- Data manipulation -- Data manipulation
import Data.Text (Text) import Data.Text (Text)
import Data.Aeson (Value(..), decode, toJSON) import Data.Aeson (Value(..), encode, decode, toJSON)
import Data.HashMap.Strict (delete) import Data.HashMap.Strict (delete)
import Data.ByteString.Lazy.Char8 (pack, unpack) import Data.ByteString.Lazy.Char8 (pack, unpack)
@ -52,16 +52,16 @@ options = Options
<> short 'u' <> short 'u'
<> value "http://namecoin.dns" <> value "http://namecoin.dns"
<> metavar "URL" <> metavar "URL"
<> help "Use custom api url" ) <> help "Use custom API URL" )
<*> (optional $ strOption $ <*> (optional $ strOption $
long "conf" long "conf"
<> short 'c' <> short 'c'
<> metavar "FILE" <> metavar "FILE"
<> help "Use custom api url" ) <> help "Use custom namecoin config file" )
<*> switch <*> switch
( long "dnschain" ( long "dnschain"
<> short 'd' <> short 'd'
<> help "Use dnschain api" ) <> help "Use dnschain API " )
<*> switch <*> switch
( long "block" ( long "block"
<> short 'b' <> short 'b'
@ -89,7 +89,7 @@ main = execParser description >>= exec where
exec Options{..} = exec Options{..} =
if dnschain if dnschain
then doDnschain url name raw block then doDnschain url name raw block
else doLocal name block conf else doLocal name raw block conf
-- | Load namecoin configuration -- | Load namecoin configuration
@ -103,15 +103,18 @@ apiURI path = do
-- | Connect to local namecoin node -- | Connect to local namecoin node
doLocal :: String -> Bool -> Maybe FilePath -> IO () doLocal :: String -> Bool -> Bool -> Maybe FilePath -> IO ()
doLocal name block conf = do doLocal name raw block conf = do
uri <- apiURI conf uri <- apiURI conf
req <- rpcRequest uri "name_show" [name] req <- rpcRequest uri "name_show" [name]
case req of case req of
Left err -> putStrLn ("The lookup failed: " ++ err) Left err -> putStrLn ("The lookup failed: " ++ err)
Right (Object res) -> do Right (Object res) -> do
pprint $ tryParse (res |: "value") if raw
when block (pprint blockInfo) then putStrLn (unpack $ encode res)
else do
pprint $ tryParse (res |: "value")
when block (pprint blockInfo)
where where
tryParse = fromMaybe (res |. "value") . decode . pack tryParse = fromMaybe (res |. "value") . decode . pack
blockInfo = toJSON (delete "value" res) blockInfo = toJSON (delete "value" res)
@ -120,7 +123,7 @@ doLocal name block conf = do
-- | Connect to dnschain API endpoint -- | Connect to dnschain API endpoint
doDnschain :: String -> String -> Bool -> Bool -> IO () doDnschain :: String -> String -> Bool -> Bool -> IO ()
doDnschain url name raw block = do 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 if raw
then putStrLn (unpack body) then putStrLn (unpack body)
else do else do