diff --git a/rosa.cabal b/rosa.cabal index a691713..35fb977 100644 --- a/rosa.cabal +++ b/rosa.cabal @@ -29,7 +29,7 @@ executable rosa default-language: Haskell2010 other-extensions: RecordWildCards, OverloadedStrings build-depends: base >=4.8 && <5.0 , aeson, text, - vector, unordered-containers, + vector, uri-encode, unordered-containers, wreq, lens, bytestring, directory, optparse-applicative, namecoin-update ghc-options: -O2 diff --git a/src/Main.hs b/src/Main.hs index 7784197..e7adec6 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,22 +1,33 @@ -{-# LANGUAGE RecordWildCards, OverloadedStrings #-} +{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE OverloadedStrings #-} + -- | Main module module Main where +-- Rosa modules 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.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 @@ -54,7 +65,7 @@ options = Options <*> switch ( long "block" <> short 'b' - <> help "Show blockchain data (require local connection)" ) + <> help "Show blockchain data" ) <*> switch ( long "raw" <> short 'r' @@ -77,7 +88,7 @@ main :: IO () main = execParser description >>= exec where exec Options{..} = if dnschain - then doDnschain url name raw + then doDnschain url name raw block else doLocal name block conf @@ -106,13 +117,18 @@ doLocal name block conf = do blockInfo = toJSON (delete "value" res) --- | Connect to dnschain api endpoint -doDnschain :: String -> String -> Bool -> IO () -doDnschain url name raw = do - body <- view responseBody <$> get (url++"/v1/namecoin/key/"++name) +-- | 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) if raw - then print body - else putStrLn $ + then putStrLn (unpack body) + else do case decode body of - Just res -> repr res - Nothing -> "Error parsing data" + Nothing -> putStrLn "Error parsing data" + Just res -> putStrLn $ + if block + then repr res + else repr $ (res .| "data") .| "value" + + where (Object x) .| y = x |. y