module Blockfrost.Types.Common
( URLVersion (..)
, Healthy (..)
, ServerTime (..)
, Metric (..)
) where
import Blockfrost.Types.Shared
import Data.Aeson
import Data.Text (Text)
import Deriving.Aeson
import Servant.Docs (ToSample (..), samples, singleSample)
data URLVersion = URLVersion
{ URLVersion -> Text
_urlVersionUrl :: Text
, URLVersion -> Text
_urlVersionVersion :: Text
}
deriving stock (Int -> URLVersion -> ShowS
[URLVersion] -> ShowS
URLVersion -> String
(Int -> URLVersion -> ShowS)
-> (URLVersion -> String)
-> ([URLVersion] -> ShowS)
-> Show URLVersion
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> URLVersion -> ShowS
showsPrec :: Int -> URLVersion -> ShowS
$cshow :: URLVersion -> String
show :: URLVersion -> String
$cshowList :: [URLVersion] -> ShowS
showList :: [URLVersion] -> ShowS
Show, URLVersion -> URLVersion -> Bool
(URLVersion -> URLVersion -> Bool)
-> (URLVersion -> URLVersion -> Bool) -> Eq URLVersion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: URLVersion -> URLVersion -> Bool
== :: URLVersion -> URLVersion -> Bool
$c/= :: URLVersion -> URLVersion -> Bool
/= :: URLVersion -> URLVersion -> Bool
Eq, (forall x. URLVersion -> Rep URLVersion x)
-> (forall x. Rep URLVersion x -> URLVersion) -> Generic URLVersion
forall x. Rep URLVersion x -> URLVersion
forall x. URLVersion -> Rep URLVersion x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. URLVersion -> Rep URLVersion x
from :: forall x. URLVersion -> Rep URLVersion x
$cto :: forall x. Rep URLVersion x -> URLVersion
to :: forall x. Rep URLVersion x -> URLVersion
Generic)
deriving (Maybe URLVersion
Value -> Parser [URLVersion]
Value -> Parser URLVersion
(Value -> Parser URLVersion)
-> (Value -> Parser [URLVersion])
-> Maybe URLVersion
-> FromJSON URLVersion
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser URLVersion
parseJSON :: Value -> Parser URLVersion
$cparseJSONList :: Value -> Parser [URLVersion]
parseJSONList :: Value -> Parser [URLVersion]
$comittedField :: Maybe URLVersion
omittedField :: Maybe URLVersion
FromJSON, [URLVersion] -> Value
[URLVersion] -> Encoding
URLVersion -> Bool
URLVersion -> Value
URLVersion -> Encoding
(URLVersion -> Value)
-> (URLVersion -> Encoding)
-> ([URLVersion] -> Value)
-> ([URLVersion] -> Encoding)
-> (URLVersion -> Bool)
-> ToJSON URLVersion
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: URLVersion -> Value
toJSON :: URLVersion -> Value
$ctoEncoding :: URLVersion -> Encoding
toEncoding :: URLVersion -> Encoding
$ctoJSONList :: [URLVersion] -> Value
toJSONList :: [URLVersion] -> Value
$ctoEncodingList :: [URLVersion] -> Encoding
toEncodingList :: [URLVersion] -> Encoding
$comitField :: URLVersion -> Bool
omitField :: URLVersion -> Bool
ToJSON)
via CustomJSON '[FieldLabelModifier '[StripPrefix "_urlVersion", CamelToSnake]] URLVersion
instance ToSample URLVersion where
toSamples :: Proxy URLVersion -> [(Text, URLVersion)]
toSamples = [(Text, URLVersion)] -> Proxy URLVersion -> [(Text, URLVersion)]
forall a. a -> Proxy URLVersion -> a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Text, URLVersion)] -> Proxy URLVersion -> [(Text, URLVersion)])
-> [(Text, URLVersion)] -> Proxy URLVersion -> [(Text, URLVersion)]
forall a b. (a -> b) -> a -> b
$ URLVersion -> [(Text, URLVersion)]
forall a. a -> [(Text, a)]
singleSample (URLVersion -> [(Text, URLVersion)])
-> URLVersion -> [(Text, URLVersion)]
forall a b. (a -> b) -> a -> b
$
Text -> Text -> URLVersion
URLVersion Text
"http://blockfrost.io" Text
"0.0.0"
newtype Healthy = Healthy
{ Healthy -> Bool
isHealthy :: Bool
}
deriving stock (Int -> Healthy -> ShowS
[Healthy] -> ShowS
Healthy -> String
(Int -> Healthy -> ShowS)
-> (Healthy -> String) -> ([Healthy] -> ShowS) -> Show Healthy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Healthy -> ShowS
showsPrec :: Int -> Healthy -> ShowS
$cshow :: Healthy -> String
show :: Healthy -> String
$cshowList :: [Healthy] -> ShowS
showList :: [Healthy] -> ShowS
Show, Healthy -> Healthy -> Bool
(Healthy -> Healthy -> Bool)
-> (Healthy -> Healthy -> Bool) -> Eq Healthy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Healthy -> Healthy -> Bool
== :: Healthy -> Healthy -> Bool
$c/= :: Healthy -> Healthy -> Bool
/= :: Healthy -> Healthy -> Bool
Eq, (forall x. Healthy -> Rep Healthy x)
-> (forall x. Rep Healthy x -> Healthy) -> Generic Healthy
forall x. Rep Healthy x -> Healthy
forall x. Healthy -> Rep Healthy x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Healthy -> Rep Healthy x
from :: forall x. Healthy -> Rep Healthy x
$cto :: forall x. Rep Healthy x -> Healthy
to :: forall x. Rep Healthy x -> Healthy
Generic)
deriving (Maybe Healthy
Value -> Parser [Healthy]
Value -> Parser Healthy
(Value -> Parser Healthy)
-> (Value -> Parser [Healthy]) -> Maybe Healthy -> FromJSON Healthy
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser Healthy
parseJSON :: Value -> Parser Healthy
$cparseJSONList :: Value -> Parser [Healthy]
parseJSONList :: Value -> Parser [Healthy]
$comittedField :: Maybe Healthy
omittedField :: Maybe Healthy
FromJSON, [Healthy] -> Value
[Healthy] -> Encoding
Healthy -> Bool
Healthy -> Value
Healthy -> Encoding
(Healthy -> Value)
-> (Healthy -> Encoding)
-> ([Healthy] -> Value)
-> ([Healthy] -> Encoding)
-> (Healthy -> Bool)
-> ToJSON Healthy
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: Healthy -> Value
toJSON :: Healthy -> Value
$ctoEncoding :: Healthy -> Encoding
toEncoding :: Healthy -> Encoding
$ctoJSONList :: [Healthy] -> Value
toJSONList :: [Healthy] -> Value
$ctoEncodingList :: [Healthy] -> Encoding
toEncodingList :: [Healthy] -> Encoding
$comitField :: Healthy -> Bool
omitField :: Healthy -> Bool
ToJSON)
via CustomJSON '[FieldLabelModifier '[CamelToSnake]] Healthy
instance ToSample Healthy where
toSamples :: Proxy Healthy -> [(Text, Healthy)]
toSamples = [(Text, Healthy)] -> Proxy Healthy -> [(Text, Healthy)]
forall a. a -> Proxy Healthy -> a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Text, Healthy)] -> Proxy Healthy -> [(Text, Healthy)])
-> [(Text, Healthy)] -> Proxy Healthy -> [(Text, Healthy)]
forall a b. (a -> b) -> a -> b
$ Healthy -> [(Text, Healthy)]
forall a. a -> [(Text, a)]
singleSample (Healthy -> [(Text, Healthy)]) -> Healthy -> [(Text, Healthy)]
forall a b. (a -> b) -> a -> b
$ Bool -> Healthy
Healthy Bool
False
newtype ServerTime = ServerTime
{ ServerTime -> POSIXTime
serverTime :: POSIXTime
}
deriving stock (Int -> ServerTime -> ShowS
[ServerTime] -> ShowS
ServerTime -> String
(Int -> ServerTime -> ShowS)
-> (ServerTime -> String)
-> ([ServerTime] -> ShowS)
-> Show ServerTime
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ServerTime -> ShowS
showsPrec :: Int -> ServerTime -> ShowS
$cshow :: ServerTime -> String
show :: ServerTime -> String
$cshowList :: [ServerTime] -> ShowS
showList :: [ServerTime] -> ShowS
Show, ServerTime -> ServerTime -> Bool
(ServerTime -> ServerTime -> Bool)
-> (ServerTime -> ServerTime -> Bool) -> Eq ServerTime
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ServerTime -> ServerTime -> Bool
== :: ServerTime -> ServerTime -> Bool
$c/= :: ServerTime -> ServerTime -> Bool
/= :: ServerTime -> ServerTime -> Bool
Eq, (forall x. ServerTime -> Rep ServerTime x)
-> (forall x. Rep ServerTime x -> ServerTime) -> Generic ServerTime
forall x. Rep ServerTime x -> ServerTime
forall x. ServerTime -> Rep ServerTime x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ServerTime -> Rep ServerTime x
from :: forall x. ServerTime -> Rep ServerTime x
$cto :: forall x. Rep ServerTime x -> ServerTime
to :: forall x. Rep ServerTime x -> ServerTime
Generic)
instance FromJSON ServerTime where
parseJSON :: Value -> Parser ServerTime
parseJSON = String
-> (Object -> Parser ServerTime) -> Value -> Parser ServerTime
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"ServerTime"
((Object -> Parser ServerTime) -> Value -> Parser ServerTime)
-> (Object -> Parser ServerTime) -> Value -> Parser ServerTime
forall a b. (a -> b) -> a -> b
$ \Object
v -> Object
v Object -> Key -> Parser Value
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"server_time"
Parser Value -> (Value -> Parser ServerTime) -> Parser ServerTime
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Value
t -> POSIXTime -> ServerTime
ServerTime (POSIXTime -> ServerTime)
-> (Integer -> POSIXTime) -> Integer -> ServerTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> POSIXTime
millisecondsToPosix (Integer -> ServerTime) -> Parser Integer -> Parser ServerTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
parseJSON @Integer Value
t
instance ToJSON ServerTime where
toJSON :: ServerTime -> Value
toJSON (ServerTime POSIXTime
t) =
[Pair] -> Value
object [
Key
"server_time" Key -> Integer -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= POSIXTime -> Integer
posixToMilliseconds POSIXTime
t
]
instance ToSample ServerTime where
toSamples :: Proxy ServerTime -> [(Text, ServerTime)]
toSamples Proxy ServerTime
_ = ServerTime -> [(Text, ServerTime)]
forall a. a -> [(Text, a)]
singleSample (ServerTime -> [(Text, ServerTime)])
-> ServerTime -> [(Text, ServerTime)]
forall a b. (a -> b) -> a -> b
$ POSIXTime -> ServerTime
ServerTime (POSIXTime -> ServerTime) -> POSIXTime -> ServerTime
forall a b. (a -> b) -> a -> b
$ Integer -> POSIXTime
millisecondsToPosix Integer
1603400958947
data Metric = Metric {
Metric -> POSIXTime
_metricTime :: POSIXTime
, Metric -> Integer
_metricCalls :: Integer
}
deriving stock (Int -> Metric -> ShowS
[Metric] -> ShowS
Metric -> String
(Int -> Metric -> ShowS)
-> (Metric -> String) -> ([Metric] -> ShowS) -> Show Metric
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Metric -> ShowS
showsPrec :: Int -> Metric -> ShowS
$cshow :: Metric -> String
show :: Metric -> String
$cshowList :: [Metric] -> ShowS
showList :: [Metric] -> ShowS
Show, Metric -> Metric -> Bool
(Metric -> Metric -> Bool)
-> (Metric -> Metric -> Bool) -> Eq Metric
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Metric -> Metric -> Bool
== :: Metric -> Metric -> Bool
$c/= :: Metric -> Metric -> Bool
/= :: Metric -> Metric -> Bool
Eq, (forall x. Metric -> Rep Metric x)
-> (forall x. Rep Metric x -> Metric) -> Generic Metric
forall x. Rep Metric x -> Metric
forall x. Metric -> Rep Metric x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Metric -> Rep Metric x
from :: forall x. Metric -> Rep Metric x
$cto :: forall x. Rep Metric x -> Metric
to :: forall x. Rep Metric x -> Metric
Generic)
deriving (Maybe Metric
Value -> Parser [Metric]
Value -> Parser Metric
(Value -> Parser Metric)
-> (Value -> Parser [Metric]) -> Maybe Metric -> FromJSON Metric
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser Metric
parseJSON :: Value -> Parser Metric
$cparseJSONList :: Value -> Parser [Metric]
parseJSONList :: Value -> Parser [Metric]
$comittedField :: Maybe Metric
omittedField :: Maybe Metric
FromJSON, [Metric] -> Value
[Metric] -> Encoding
Metric -> Bool
Metric -> Value
Metric -> Encoding
(Metric -> Value)
-> (Metric -> Encoding)
-> ([Metric] -> Value)
-> ([Metric] -> Encoding)
-> (Metric -> Bool)
-> ToJSON Metric
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: Metric -> Value
toJSON :: Metric -> Value
$ctoEncoding :: Metric -> Encoding
toEncoding :: Metric -> Encoding
$ctoJSONList :: [Metric] -> Value
toJSONList :: [Metric] -> Value
$ctoEncodingList :: [Metric] -> Encoding
toEncodingList :: [Metric] -> Encoding
$comitField :: Metric -> Bool
omitField :: Metric -> Bool
ToJSON)
via CustomJSON '[FieldLabelModifier '[StripPrefix "_metric", CamelToSnake]] Metric
instance ToSample Metric where
toSamples :: Proxy Metric -> [(Text, Metric)]
toSamples Proxy Metric
_ = [Metric] -> [(Text, Metric)]
forall a. [a] -> [(Text, a)]
samples ([Metric] -> [(Text, Metric)]) -> [Metric] -> [(Text, Metric)]
forall a b. (a -> b) -> a -> b
$
[ POSIXTime -> Integer -> Metric
Metric POSIXTime
1612543884 Integer
42
, POSIXTime -> Integer -> Metric
Metric POSIXTime
1614523884 Integer
6942
]
instance {-# OVERLAPS #-} ToJSON (Text, Metric) where
toJSON :: (Text, Metric) -> Value
toJSON (Text
enp, Metric
m) = case Metric -> Value
forall a. ToJSON a => a -> Value
toJSON Metric
m of
(Object Object
o) -> Object -> Value
Object (Object
o Object -> Object -> Object
forall a. Semigroup a => a -> a -> a
<> (Key
"endpoint" Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text -> Value
forall a. ToJSON a => a -> Value
toJSON Text
enp)))
Value
_ -> String -> Value
forall a. HasCallStack => String -> a
error String
"Absurd"
instance {-# OVERLAPS #-} FromJSON (Text, Metric) where
parseJSON :: Value -> Parser (Text, Metric)
parseJSON v :: Value
v@(Object Object
o) = do
Text
enp <- Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"endpoint"
Metric
ticker <- Value -> Parser Metric
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v
(Text, Metric) -> Parser (Text, Metric)
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
enp, Metric
ticker)
parseJSON Value
_ = String -> Parser (Text, Metric)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unexpected type for (Text, Metric)"
instance {-# OVERLAPS #-} ToSample (Text, Metric) where
toSamples :: Proxy (Text, Metric) -> [(Text, (Text, Metric))]
toSamples Proxy (Text, Metric)
_ = [(Text, Metric)] -> [(Text, (Text, Metric))]
forall a. [a] -> [(Text, a)]
samples ([(Text, Metric)] -> [(Text, (Text, Metric))])
-> [(Text, Metric)] -> [(Text, (Text, Metric))]
forall a b. (a -> b) -> a -> b
$
[ (Text
"block", POSIXTime -> Integer -> Metric
Metric POSIXTime
1612543814 Integer
182)
, (Text
"epoch", POSIXTime -> Integer -> Metric
Metric POSIXTime
1612543814 Integer
42)
, (Text
"block", POSIXTime -> Integer -> Metric
Metric POSIXTime
1612543812 Integer
775)
, (Text
"epoch", POSIXTime -> Integer -> Metric
Metric POSIXTime
1612523884 Integer
4)
, (Text
"block", POSIXTime -> Integer -> Metric
Metric POSIXTime
1612553884 Integer
89794)
]