|
| 1 | +package httpapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + gohttp "net/http" |
| 7 | + "os" |
| 8 | + "path" |
| 9 | + "strings" |
| 10 | + |
| 11 | + iface "github.com/ipfs/interface-go-ipfs-core" |
| 12 | + caopts "github.com/ipfs/interface-go-ipfs-core/options" |
| 13 | + homedir "github.com/mitchellh/go-homedir" |
| 14 | + ma "github.com/multiformats/go-multiaddr" |
| 15 | + manet "github.com/multiformats/go-multiaddr-net" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + DefaultPathName = ".ipfs" |
| 20 | + DefaultPathRoot = "~/" + DefaultPathName |
| 21 | + DefaultApiFile = "api" |
| 22 | + EnvDir = "IPFS_PATH" |
| 23 | +) |
| 24 | + |
| 25 | +// HttpApi implements github.com/ipfs/interface-go-ipfs-core/CoreAPI using |
| 26 | +// IPFS HTTP API. |
| 27 | +// |
| 28 | +// For interface docs see |
| 29 | +// https://godoc.org/github.com/ipfs/interface-go-ipfs-core#CoreAPI |
| 30 | +type HttpApi struct { |
| 31 | + url string |
| 32 | + httpcli gohttp.Client |
| 33 | + |
| 34 | + applyGlobal func(*RequestBuilder) |
| 35 | +} |
| 36 | + |
| 37 | +// NewLocalApi tries to construct new HttpApi instance communicating with local |
| 38 | +// IPFS daemon |
| 39 | +// |
| 40 | +// Daemon api address is pulled from the $IPFS_PATH/api file. |
| 41 | +// If $IPFS_PATH env var is not present, it defaults to ~/.ipfs |
| 42 | +func NewLocalApi() (iface.CoreAPI, error) { |
| 43 | + baseDir := os.Getenv(EnvDir) |
| 44 | + if baseDir == "" { |
| 45 | + baseDir = DefaultPathRoot |
| 46 | + } |
| 47 | + |
| 48 | + return NewPathApi(baseDir) |
| 49 | +} |
| 50 | + |
| 51 | +// NewPathApi constructs new HttpApi by pulling api address from specified |
| 52 | +// ipfspath. Api file should be located at $ipfspath/api |
| 53 | +func NewPathApi(ipfspath string) (iface.CoreAPI, error) { |
| 54 | + a, err := ApiAddr(ipfspath) |
| 55 | + if err != nil { |
| 56 | + if os.IsNotExist(err) { |
| 57 | + err = nil |
| 58 | + } |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + return NewApi(a) |
| 62 | +} |
| 63 | + |
| 64 | +// ApiAddr reads api file in specified ipfs path |
| 65 | +func ApiAddr(ipfspath string) (ma.Multiaddr, error) { |
| 66 | + baseDir, err := homedir.Expand(ipfspath) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + apiFile := path.Join(baseDir, DefaultApiFile) |
| 72 | + |
| 73 | + api, err := ioutil.ReadFile(apiFile) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + return ma.NewMultiaddr(strings.TrimSpace(string(api))) |
| 79 | +} |
| 80 | + |
| 81 | +// NewApi constructs HttpApi with specified endpoint |
| 82 | +func NewApi(a ma.Multiaddr) (*HttpApi, error) { |
| 83 | + c := &gohttp.Client{ |
| 84 | + Transport: &gohttp.Transport{ |
| 85 | + Proxy: gohttp.ProxyFromEnvironment, |
| 86 | + DisableKeepAlives: true, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + return NewApiWithClient(a, c) |
| 91 | +} |
| 92 | + |
| 93 | +// NewApiWithClient constructs HttpApi with specified endpoint and custom http client |
| 94 | +func NewApiWithClient(a ma.Multiaddr, c *gohttp.Client) (*HttpApi, error) { |
| 95 | + _, url, err := manet.DialArgs(a) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + if a, err := ma.NewMultiaddr(url); err == nil { |
| 101 | + _, host, err := manet.DialArgs(a) |
| 102 | + if err == nil { |
| 103 | + url = host |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + api := &HttpApi{ |
| 108 | + url: url, |
| 109 | + httpcli: *c, |
| 110 | + applyGlobal: func(*RequestBuilder) {}, |
| 111 | + } |
| 112 | + |
| 113 | + // We don't support redirects. |
| 114 | + api.httpcli.CheckRedirect = func(_ *gohttp.Request, _ []*gohttp.Request) error { |
| 115 | + return fmt.Errorf("unexpected redirect") |
| 116 | + } |
| 117 | + |
| 118 | + return api, nil |
| 119 | +} |
| 120 | + |
| 121 | +func (api *HttpApi) WithOptions(opts ...caopts.ApiOption) (iface.CoreAPI, error) { |
| 122 | + options, err := caopts.ApiOptions(opts...) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + |
| 127 | + subApi := *api |
| 128 | + subApi.applyGlobal = func(req *RequestBuilder) { |
| 129 | + if options.Offline { |
| 130 | + req.Option("offline", options.Offline) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return &subApi, nil |
| 135 | +} |
| 136 | + |
| 137 | +func (api *HttpApi) request(command string, args ...string) *RequestBuilder { |
| 138 | + return &RequestBuilder{ |
| 139 | + command: command, |
| 140 | + args: args, |
| 141 | + shell: api, |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func (api *HttpApi) Unixfs() iface.UnixfsAPI { |
| 146 | + return (*UnixfsAPI)(api) |
| 147 | +} |
| 148 | + |
| 149 | +func (api *HttpApi) Block() iface.BlockAPI { |
| 150 | + return (*BlockAPI)(api) |
| 151 | +} |
| 152 | + |
| 153 | +func (api *HttpApi) Dag() iface.APIDagService { |
| 154 | + return (*HttpDagServ)(api) |
| 155 | +} |
| 156 | + |
| 157 | +func (api *HttpApi) Name() iface.NameAPI { |
| 158 | + return (*NameAPI)(api) |
| 159 | +} |
| 160 | + |
| 161 | +func (api *HttpApi) Key() iface.KeyAPI { |
| 162 | + return (*KeyAPI)(api) |
| 163 | +} |
| 164 | + |
| 165 | +func (api *HttpApi) Pin() iface.PinAPI { |
| 166 | + return (*PinAPI)(api) |
| 167 | +} |
| 168 | + |
| 169 | +func (api *HttpApi) Object() iface.ObjectAPI { |
| 170 | + return (*ObjectAPI)(api) |
| 171 | +} |
| 172 | + |
| 173 | +func (api *HttpApi) Dht() iface.DhtAPI { |
| 174 | + return (*DhtAPI)(api) |
| 175 | +} |
| 176 | + |
| 177 | +func (api *HttpApi) Swarm() iface.SwarmAPI { |
| 178 | + return (*SwarmAPI)(api) |
| 179 | +} |
| 180 | + |
| 181 | +func (api *HttpApi) PubSub() iface.PubSubAPI { |
| 182 | + return (*PubsubAPI)(api) |
| 183 | +} |
0 commit comments