interface.go 2.53 KB
Newer Older
1 2
// Package iface defines IPFS Core API which is a set of interfaces used to
// interact with IPFS nodes.
3 4 5 6 7 8 9
package iface

import (
	"context"
	"errors"
	"io"

Steven Allen's avatar
Steven Allen committed
10 11
	ipld "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
	cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
12 13
)

14 15
// Path is a generic wrapper for paths used in the API. A path can be resolved
// to a CID using one of Resolve functions in the API.
16 17 18 19 20 21 22 23
type Path interface {
	String() string
	Cid() *cid.Cid
	Root() *cid.Cid
	Resolved() bool
}

// TODO: should we really copy these?
24
//       if we didn't, godoc would generate nice links straight to go-ipld-format
25
type Node ipld.Node
Lars Gierth's avatar
Lars Gierth committed
26
type Link ipld.Link
27 28 29 30 31 32

type Reader interface {
	io.ReadSeeker
	io.Closer
}

33
// CoreAPI defines an unified interface to IPFS for Go programs.
34
type CoreAPI interface {
35
	// Unixfs returns an implementation of Unixfs API
36
	Unixfs() UnixfsAPI
Łukasz Magiera's avatar
Łukasz Magiera committed
37
	Dag() DagAPI
38 39

	// ResolvePath resolves the path using Unixfs resolver
40
	ResolvePath(context.Context, Path) (Path, error)
41 42 43

	// ResolveNode resolves the path (if not resolved already) using Unixfs
	// resolver, gets and returns the resolved Node
Łukasz Magiera's avatar
Łukasz Magiera committed
44
	ResolveNode(context.Context, Path) (Node, error) //TODO: should this get dropped in favor of DagAPI.Get?
45 46
}

47
// UnixfsAPI is the basic interface to immutable files in IPFS
48
type UnixfsAPI interface {
49
	// Add imports the data from the reader into merkledag file
50
	Add(context.Context, io.Reader) (Path, error)
51 52

	// Cat returns a reader for the file
53
	Cat(context.Context, Path) (Reader, error)
54 55

	// Ls returns the list of links in a directory
56
	Ls(context.Context, Path) ([]*Link, error)
57 58
}

Łukasz Magiera's avatar
Łukasz Magiera committed
59 60 61 62 63 64
type DagAPI interface {
	Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error)
	Get(ctx context.Context, path Path) (Node, error)
	Tree(ctx context.Context, path Path, depth int) ([]Path, error)
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
// type ObjectAPI interface {
// 	New() (cid.Cid, Object)
// 	Get(string) (Object, error)
// 	Links(string) ([]*Link, error)
// 	Data(string) (Reader, error)
// 	Stat(string) (ObjectStat, error)
// 	Put(Object) (cid.Cid, error)
// 	SetData(string, Reader) (cid.Cid, error)
// 	AppendData(string, Data) (cid.Cid, error)
// 	AddLink(string, string, string) (cid.Cid, error)
// 	RmLink(string, string) (cid.Cid, error)
// }

// type ObjectStat struct {
// 	Cid            cid.Cid
// 	NumLinks       int
// 	BlockSize      int
// 	LinksSize      int
// 	DataSize       int
// 	CumulativeSize int
// }

var ErrIsDir = errors.New("object is a directory")
var ErrOffline = errors.New("can't resolve, ipfs node is offline")