-- | Transaction Id

module Blockfrost.Types.Shared.TxHash
  ( TxHash (..)
  ) where

import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.String (IsString (..))
import Data.Text (Text)
import qualified Data.Text
import GHC.Generics
import Servant.API (Capture, FromHttpApiData (..), ToHttpApiData (..))
import Servant.Docs (DocCapture (..), ToCapture (..), ToSample (..), samples)

-- | Id (hash) of the transaction
newtype TxHash = TxHash { TxHash -> Text
unTxHash :: Text }
  deriving stock (Int -> TxHash -> ShowS
[TxHash] -> ShowS
TxHash -> String
(Int -> TxHash -> ShowS)
-> (TxHash -> String) -> ([TxHash] -> ShowS) -> Show TxHash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxHash] -> ShowS
$cshowList :: [TxHash] -> ShowS
show :: TxHash -> String
$cshow :: TxHash -> String
showsPrec :: Int -> TxHash -> ShowS
$cshowsPrec :: Int -> TxHash -> ShowS
Show, TxHash -> TxHash -> Bool
(TxHash -> TxHash -> Bool)
-> (TxHash -> TxHash -> Bool) -> Eq TxHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxHash -> TxHash -> Bool
$c/= :: TxHash -> TxHash -> Bool
== :: TxHash -> TxHash -> Bool
$c== :: TxHash -> TxHash -> Bool
Eq, (forall x. TxHash -> Rep TxHash x)
-> (forall x. Rep TxHash x -> TxHash) -> Generic TxHash
forall x. Rep TxHash x -> TxHash
forall x. TxHash -> Rep TxHash x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxHash x -> TxHash
$cfrom :: forall x. TxHash -> Rep TxHash x
Generic)
  deriving newtype (ByteString -> Either Text TxHash
Text -> Either Text TxHash
(Text -> Either Text TxHash)
-> (ByteString -> Either Text TxHash)
-> (Text -> Either Text TxHash)
-> FromHttpApiData TxHash
forall a.
(Text -> Either Text a)
-> (ByteString -> Either Text a)
-> (Text -> Either Text a)
-> FromHttpApiData a
parseQueryParam :: Text -> Either Text TxHash
$cparseQueryParam :: Text -> Either Text TxHash
parseHeader :: ByteString -> Either Text TxHash
$cparseHeader :: ByteString -> Either Text TxHash
parseUrlPiece :: Text -> Either Text TxHash
$cparseUrlPiece :: Text -> Either Text TxHash
FromHttpApiData, TxHash -> ByteString
TxHash -> Builder
TxHash -> Text
(TxHash -> Text)
-> (TxHash -> Builder)
-> (TxHash -> ByteString)
-> (TxHash -> Text)
-> ToHttpApiData TxHash
forall a.
(a -> Text)
-> (a -> Builder)
-> (a -> ByteString)
-> (a -> Text)
-> ToHttpApiData a
toQueryParam :: TxHash -> Text
$ctoQueryParam :: TxHash -> Text
toHeader :: TxHash -> ByteString
$ctoHeader :: TxHash -> ByteString
toEncodedUrlPiece :: TxHash -> Builder
$ctoEncodedUrlPiece :: TxHash -> Builder
toUrlPiece :: TxHash -> Text
$ctoUrlPiece :: TxHash -> Text
ToHttpApiData)

instance IsString TxHash where
  fromString :: String -> TxHash
fromString = Text -> TxHash
TxHash (Text -> TxHash) -> (String -> Text) -> String -> TxHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Data.Text.pack

instance ToJSON TxHash where
  toJSON :: TxHash -> Value
toJSON = Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Text -> Value) -> (TxHash -> Text) -> TxHash -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxHash -> Text
unTxHash
  toEncoding :: TxHash -> Encoding
toEncoding = Text -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding (Text -> Encoding) -> (TxHash -> Text) -> TxHash -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxHash -> Text
unTxHash
instance FromJSON TxHash where
  parseJSON :: Value -> Parser TxHash
parseJSON = (Text -> TxHash) -> Parser Text -> Parser TxHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> TxHash
TxHash (Parser Text -> Parser TxHash)
-> (Value -> Parser Text) -> Value -> Parser TxHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Text
forall a. FromJSON a => Value -> Parser a
parseJSON

instance ToSample TxHash where
    toSamples :: Proxy TxHash -> [(Text, TxHash)]
toSamples Proxy TxHash
_ = [TxHash] -> [(Text, TxHash)]
forall a. [a] -> [(Text, a)]
samples ([TxHash] -> [(Text, TxHash)]) -> [TxHash] -> [(Text, TxHash)]
forall a b. (a -> b) -> a -> b
$ (Text -> TxHash) -> [Text] -> [TxHash]
forall a b. (a -> b) -> [a] -> [b]
map Text -> TxHash
TxHash
      [ Text
"8788591983aa73981fc92d6cddbbe643959f5a784e84b8bee0db15823f575a5b"
      , Text
"52e748c4dec58b687b90b0b40d383b9fe1f24c1a833b7395cdf07dd67859f46f"
      , Text
"e8073fd5318ff43eca18a852527166aa8008bee9ee9e891f585612b7e4ba700b"
      ]

instance ToCapture (Capture "hash" TxHash) where
  toCapture :: Proxy (Capture "hash" TxHash) -> DocCapture
toCapture Proxy (Capture "hash" TxHash)
_ = String -> String -> DocCapture
DocCapture String
"hash" String
"Hash of the requested transaction."