{-|
Module      : Gargantext.API.HashedResponse
Description :
Copyright   : (c) CNRS, 2020-Present
License     : AGPL + CECILL v3
Maintainer  : team@gargantext.org
Stability   : experimental
Portability : POSIX

-}

module Gargantext.API.HashedResponse where

import Data.Aeson
import Data.Swagger
import Data.Text (Text)
import GHC.Generics (Generic)

import Gargantext.Prelude
import qualified Gargantext.Prelude.Crypto.Hash as Crypto (hash)

data HashedResponse a = HashedResponse { HashedResponse a -> Text
hash :: Text, HashedResponse a -> a
value :: a }
  deriving ((forall x. HashedResponse a -> Rep (HashedResponse a) x)
-> (forall x. Rep (HashedResponse a) x -> HashedResponse a)
-> Generic (HashedResponse a)
forall x. Rep (HashedResponse a) x -> HashedResponse a
forall x. HashedResponse a -> Rep (HashedResponse a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (HashedResponse a) x -> HashedResponse a
forall a x. HashedResponse a -> Rep (HashedResponse a) x
$cto :: forall a x. Rep (HashedResponse a) x -> HashedResponse a
$cfrom :: forall a x. HashedResponse a -> Rep (HashedResponse a) x
Generic)

instance ToSchema a => ToSchema (HashedResponse a)
instance ToJSON a => ToJSON (HashedResponse a) where
  toJSON :: HashedResponse a -> Value
toJSON = Options -> HashedResponse a -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
defaultOptions

constructHashedResponse :: ToJSON a => a -> HashedResponse a
constructHashedResponse :: a -> HashedResponse a
constructHashedResponse a
v = HashedResponse :: forall a. Text -> a -> HashedResponse a
HashedResponse { hash :: Text
hash = ByteString -> Text
forall a. IsHashable a => a -> Text
Crypto.hash (ByteString -> Text) -> ByteString -> Text
forall a b. (a -> b) -> a -> b
$ a -> ByteString
forall a. ToJSON a => a -> ByteString
encode a
v, value :: a
value = a
v }