{-# LANGUAGE OverloadedStrings #-}
module Network.Ipfs.Api.Core where
import qualified Codec.Archive.Tar as Tar
import Control.Monad.IO.Class (MonadIO (..))
import Data.Aeson (decode)
import Data.ByteString.Lazy (fromStrict)
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
import Network.HTTP.Client (responseBody)
import Servant.API.ContentTypes (NoContent)
import Network.Ipfs.Api.Internal (_cat, _dns, _get, _id,
_idPeer, _ls, _shutdown,
_version)
import Network.Ipfs.Api.Internal.Call (call, multipartCall,
streamCall)
import Network.Ipfs.Api.Internal.Stream (_ping, _refs, _refsLocal)
import Network.Ipfs.Api.Types (AddObj, CatReturnType,
DnsObj, IdObj, LsObj,
VersionObj)
import Network.Ipfs.Client (IpfsT)
cat :: MonadIO m => Text -> IpfsT m CatReturnType
cat = call . _cat
add :: MonadIO m => Text -> IpfsT m (Maybe AddObj)
add = fmap decodeResponse . multipartCall "add"
where
decodeResponse = decode . responseBody
ls :: MonadIO m => Text -> IpfsT m LsObj
ls = call . _ls
get :: MonadIO m => Text -> IpfsT m Text
get hash = do
ret <- call $ _get hash
do liftIO $ Tar.unpack "getResponseDirectory" . Tar.read . fromStrict $ encodeUtf8 ret
pure "The content has been stored in getResponseDirectory."
version :: MonadIO m => IpfsT m VersionObj
version = call _version
id :: MonadIO m => IpfsT m IdObj
id = call _id
idPeer :: MonadIO m => Text -> IpfsT m IdObj
idPeer = call . _idPeer
dns :: MonadIO m => Text -> IpfsT m DnsObj
dns = call . _dns
refs :: MonadIO m => Text -> m ()
refs = streamCall . _refs
refsLocal :: MonadIO m => m ()
refsLocal = streamCall _refsLocal
ping :: MonadIO m => Text -> m ()
ping = streamCall . _ping
shutdown :: MonadIO m => IpfsT m NoContent
shutdown = call _shutdown