aqua-ipfs/aqua/ipfs-api.aqua

113 lines
3.7 KiB
Plaintext
Raw Normal View History

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
2021-07-01 23:14:59 +03:00
import "@fluencelabs/aqua-lib/builtin.aqua"
2021-07-01 23:14:59 +03:00
import "ipfs.aqua"
alias Multiaddr: string
2021-07-26 12:14:39 +03:00
-- Download file from remote IPFS node to Fluence node and then
2021-07-26 12:14:39 +03:00
-- 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.
2021-07-26 12:14:39 +03:00
-- Errors are reported to the `error` callback.
func get_and_cache(
node: PeerId,
cid: CID,
from: Multiaddr,
error: string, string -> ()
2021-08-02 17:50:21 +03:00
) -> ?CID:
-- localCid will be the same as cid
localCid: *CID
2021-07-26 12:14:39 +03:00
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:
2021-08-02 17:50:21 +03:00
localCid <<- put.hash
2021-07-26 12:14:39 +03:00
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)
2021-08-02 17:50:21 +03:00
<- localCid
2021-07-01 23:14:59 +03:00
2021-07-21 20:19:13 +03:00
-- Upload file `path` to IPFS node running on `node`
-- path should exist & be available to `aqua-ipfs`
2021-07-09 15:52:41 +03:00
func put(node: PeerId, path: string) -> IpfsPutResult:
2021-07-01 23:14:59 +03:00
on node:
result <- Ipfs.put(path)
<- result
2021-07-01 23:14:59 +03:00
-- 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
2021-07-26 12:14:39 +03:00
-- Download file `cid` from IPFS node `from` and save it to `node`
func get_from(node: PeerId, cid: CID, from: Multiaddr) -> IpfsGetResult:
2021-07-01 23:14:59 +03:00
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
2021-07-14 22:09:19 +03:00
-- Set timeout for IPFS calls in `aqua-ipfs`
2021-07-14 22:09:19 +03:00
func set_timeout(node: PeerId, timeout_sec: u64):
on node:
Ipfs.set_timeout(timeout_sec)
2021-07-21 20:19:13 +03:00
2021-07-26 12:14:39 +03:00
-- Get externally available multiaddress of IPFS's HTTP RPC endpoint (usually on port 5001)
2021-07-21 20:19:13 +03:00
func get_external_api_multiaddr(node: PeerId) -> IpfsMultiaddrResult:
on node:
result <- Ipfs.get_external_api_multiaddr()
<- result
2021-07-21 20:19:13 +03:00
2021-07-26 12:14:39 +03:00
-- Get externally available multiaddress of IPFS's Swarm endpoint (usually on port 4001)
2021-07-21 20:19:13 +03:00
func get_external_swarm_multiaddr(node: PeerId) -> IpfsMultiaddrResult:
on node:
result <- Ipfs.get_external_swarm_multiaddr()
<- result
2021-07-26 12:14:39 +03:00
-- 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