url escape the name for dnschain API

This commit is contained in:
Michele Guerini Rocco 2019-11-10 11:07:41 +01:00
parent 7305b4f3c9
commit 682dc387ef
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450
2 changed files with 38 additions and 22 deletions

View File

@ -29,7 +29,7 @@ executable rosa
default-language: Haskell2010 default-language: Haskell2010
other-extensions: RecordWildCards, OverloadedStrings other-extensions: RecordWildCards, OverloadedStrings
build-depends: base >=4.8 && <5.0 , aeson, text, build-depends: base >=4.8 && <5.0 , aeson, text,
vector, unordered-containers, vector, uri-encode, unordered-containers,
wreq, lens, bytestring, directory, wreq, lens, bytestring, directory,
optparse-applicative, namecoin-update optparse-applicative, namecoin-update
ghc-options: -O2 ghc-options: -O2

View File

@ -1,22 +1,33 @@
{-# LANGUAGE RecordWildCards, OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
-- | Main module -- | Main module
module Main where module Main where
-- Rosa modules
import Json import Json
-- Networking
import Namecoin (rpcRequest, uri)
import Network.URI.Encode (encode)
import Network.Wreq (get, responseBody)
-- IO
import Options.Applicative
import System.Directory (XdgDirectory(..), getXdgDirectory)
import qualified Data.Text.IO as T
-- Data manipulation
import Data.Text (Text)
import Data.Aeson (Value(..), decode, toJSON)
import Data.HashMap.Strict (delete)
import Data.ByteString.Lazy.Char8 (pack, unpack)
-- Misc
import Data.Maybe (fromMaybe)
import Control.Lens (view) import Control.Lens (view)
import Control.Monad (when) import Control.Monad (when)
import Namecoin (Error, rpcRequest, uri)
import Network.Wreq (get, responseBody)
import System.Directory (XdgDirectory(..), getXdgDirectory)
import Data.Aeson (Value(..), decode, toJSON)
import Data.Maybe (fromJust, fromMaybe)
import Data.HashMap.Strict (delete)
import Data.ByteString.Lazy.Char8 (pack)
import Data.Monoid
import Options.Applicative
import qualified Data.Text.IO as T
-- * CLI interface -- * CLI interface
@ -54,7 +65,7 @@ options = Options
<*> switch <*> switch
( long "block" ( long "block"
<> short 'b' <> short 'b'
<> help "Show blockchain data (require local connection)" ) <> help "Show blockchain data" )
<*> switch <*> switch
( long "raw" ( long "raw"
<> short 'r' <> short 'r'
@ -77,7 +88,7 @@ main :: IO ()
main = execParser description >>= exec where main = execParser description >>= exec where
exec Options{..} = exec Options{..} =
if dnschain if dnschain
then doDnschain url name raw then doDnschain url name raw block
else doLocal name block conf else doLocal name block conf
@ -106,13 +117,18 @@ doLocal name block conf = do
blockInfo = toJSON (delete "value" res) blockInfo = toJSON (delete "value" res)
-- | Connect to dnschain api endpoint -- | Connect to dnschain API endpoint
doDnschain :: String -> String -> Bool -> IO () doDnschain :: String -> String -> Bool -> Bool -> IO ()
doDnschain url name raw = do doDnschain url name raw block = do
body <- view responseBody <$> get (url++"/v1/namecoin/key/"++name) body <- view responseBody <$> get (url++"/v1/namecoin/key/"++encode name)
if raw if raw
then print body then putStrLn (unpack body)
else putStrLn $ else do
case decode body of case decode body of
Just res -> repr res Nothing -> putStrLn "Error parsing data"
Nothing -> "Error parsing data" Just res -> putStrLn $
if block
then repr res
else repr $ (res .| "data") .| "value"
where (Object x) .| y = x |. y