{-# LANGUAGE OverloadedStrings #-}

-- |
-- Module      :  Network.Ipfs.Api.Block
-- Copyright   :  Aleksandr Krupenkin 2016-2021
-- License     :  Apache-2.0
--
-- Maintainer  :  mail@akru.me
-- Stability   :  experimental
-- Portability :  unknown
--
-- Api calls with `block` prefix.
--

module Network.Ipfs.Api.Block where

import           Control.Monad.IO.Class         (MonadIO)
import           Data.Aeson                     (decode)
import           Data.Text                      (Text)
import           Network.HTTP.Client            (responseBody)

import           Network.Ipfs.Api.Internal      (_blockGet, _blockStat)
import           Network.Ipfs.Api.Internal.Call (call, multipartCall)
import           Network.Ipfs.Api.Types         (BlockObj, BlockReturnType)
import           Network.Ipfs.Client            (IpfsT)

-- | Get a raw IPFS block.
get :: MonadIO m => Text -> IpfsT m BlockReturnType
get :: Text -> IpfsT m Text
get = ClientM Text -> IpfsT m Text
forall (m :: * -> *) a. MonadIO m => ClientM a -> IpfsT m a
call (ClientM Text -> IpfsT m Text)
-> (Text -> ClientM Text) -> Text -> IpfsT m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ClientM Text
_blockGet

-- | Store input as an IPFS block.
put :: MonadIO m => Text -> IpfsT m (Maybe BlockObj)
put :: Text -> IpfsT m (Maybe BlockObj)
put = (Response ByteString -> Maybe BlockObj)
-> IpfsT m (Response ByteString) -> IpfsT m (Maybe BlockObj)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Response ByteString -> Maybe BlockObj
decodeResponse (IpfsT m (Response ByteString) -> IpfsT m (Maybe BlockObj))
-> (Text -> IpfsT m (Response ByteString))
-> Text
-> IpfsT m (Maybe BlockObj)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> IpfsT m (Response ByteString)
forall (m :: * -> *).
MonadIO m =>
Text -> Text -> IpfsT m (Response ByteString)
multipartCall Text
"block/put"
  where
    decodeResponse :: Response ByteString -> Maybe BlockObj
decodeResponse = ByteString -> Maybe BlockObj
forall a. FromJSON a => ByteString -> Maybe a
decode (ByteString -> Maybe BlockObj)
-> (Response ByteString -> ByteString)
-> Response ByteString
-> Maybe BlockObj
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Response ByteString -> ByteString
forall body. Response body -> body
responseBody

-- | Print information of a raw IPFS block.
stat :: MonadIO m => Text -> IpfsT m BlockObj
stat :: Text -> IpfsT m BlockObj
stat = ClientM BlockObj -> IpfsT m BlockObj
forall (m :: * -> *) a. MonadIO m => ClientM a -> IpfsT m a
call (ClientM BlockObj -> IpfsT m BlockObj)
-> (Text -> ClientM BlockObj) -> Text -> IpfsT m BlockObj
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ClientM BlockObj
_blockStat