aqua IpfsApi declares *

export get_and_cache
export put, dag_put, dag_get, get_from, dag_get_from, cat_from
export set_timeout, get_external_api_multiaddr
export get_external_swarm_multiaddr, get_local_api_multiaddr

import "@fluencelabs/aqua-lib/builtin.aqua"

import "ipfs.aqua"

alias Multiaddr: string

-- Download file from remote IPFS node to Fluence node and then
-- put that file to local IPFS node, effectively caching it on the local IPFS node.
--
-- Arguments:
--  node - PeerId of the node where execution should happen
--  cid – IPFS Content ID to download
--  from - Multiaddress of IPFS node to download `cid` from
--  error - callback to notify function caller about errors
--
-- Returns:
--  Path on the node's local filesystem. It will be available only during single particle execution.
--
-- Errors:
--  If Ipfs.get_from or Ipfs.put fails, nil is returned.
--  Errors are reported to the `error` callback.
func get_and_cache(
    node: PeerId,
    cid: CID,
    from: Multiaddr,
    error: string, string -> ()
) -> ?CID:
    -- localCid will be the same as cid
    localCid: *CID
    on node:
        -- Download file from remote IPFS to local filesystem
        get <- Ipfs.get_from(cid, from)
        if get.success:
            -- Put file to local IPFS node
            put <- Ipfs.put(get.path)
            if put.success:
                localCid <<- put.hash
            else:
                -- report error in background co-routine
                co error("Ipfs.put failed", put.error)
        else:
            -- report error in background co-routine
            co error("Ipfs.get failed", get.error)
    <- localCid

-- Upload file `path` to IPFS node running on `node`
-- path should exist & be available to `aqua-ipfs`
func put(node: PeerId, path: string) -> IpfsPutResult:
    on node:
        result <- Ipfs.put(path)
    <- result

-- Upload file `path` as a dag to IPFS node running on `node`
-- path should exist & be available to `aqua-ipfs`
func dag_put(node: PeerId, path: string) -> IpfsPutResult:
    on node:
        result <- Ipfs.dag_put(path)
    <- result

-- Returns file path of the dag `cid` from local cache of IPFS node `node`
func dag_get(node: PeerId, cid: CID) -> IpfsGetResult:
    on node:
       result <- Ipfs.dag_get(cid)
    <- result

-- Download file `cid` from IPFS node `from` and save it to `node`
func get_from(node: PeerId, cid: CID, from: Multiaddr) -> IpfsGetResult:
    on node:
        result <- Ipfs.get_from(cid, from)
    <- result

-- Return contents of the dag `cid` from IPFS node `from` and save it to `node`
func dag_get_from(node: PeerId, cid: CID, from: Multiaddr) -> IpfsGetResult:
    on node:
       result <- Ipfs.dag_get_from(cid, from)
    <- result

-- Return contents of the file `cid` from IPFS node `from`
func cat_from(node: PeerId, cid: CID, from: Multiaddr) -> IpfsCatResult:
    on node:
        result <- Ipfs.cat_from(cid, from)
    <- result

-- Set timeout for IPFS calls in `aqua-ipfs`
func set_timeout(node: PeerId, timeout_sec: u64):
    on node:
        Ipfs.set_timeout(timeout_sec)

-- Get externally available multiaddress of IPFS's HTTP RPC endpoint (usually on port 5001)
func get_external_api_multiaddr(node: PeerId) -> IpfsMultiaddrResult:
    on node:
        result <- Ipfs.get_external_api_multiaddr()
    <- result

-- Get externally available multiaddress of IPFS's Swarm endpoint (usually on port 4001)
func get_external_swarm_multiaddr(node: PeerId) -> IpfsMultiaddrResult:
    on node:
        result <- Ipfs.get_external_swarm_multiaddr()
    <- result

-- Get local multiaddress of IPFS's HTTP RPC endpoint (usually on port 5001)
func get_local_api_multiaddr(node: PeerId) -> IpfsMultiaddrResult:
    on node:
        result <- Ipfs.get_local_api_multiaddr()
    <- result