Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit 449b614

Browse files
authored
Merge pull request #1 from ipfs/feat/implement
Initial implementation
2 parents e823507 + 7941544 commit 449b614

21 files changed

+2806
-0
lines changed

.gx/lastpubver

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.0: QmfWSu5jNoBxBm5Vh1hwoha3DSj4u9R31WZNghgyh43iJd

.travis.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
os:
2+
- linux
3+
4+
sudo: false
5+
6+
language: go
7+
8+
go:
9+
- 1.11.x
10+
11+
install:
12+
- make deps
13+
- go get -d github.com/ipfs/go-ipfs
14+
- (cd $GOPATH/src/github.com/ipfs/go-ipfs; make install)
15+
16+
script:
17+
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
18+
19+
cache:
20+
directories:
21+
- $GOPATH/src/gx
22+
23+
notifications:
24+
email: false
25+
26+
env: GOTFLAGS="-race"

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
all: deps
2+
gx:
3+
go get github.com/whyrusleeping/gx
4+
go get github.com/whyrusleeping/gx-go
5+
deps: gx
6+
gx --verbose install --global
7+
gx-go rewrite
8+
test: deps
9+
gx test -v -race -coverprofile=coverage.txt -covermode=atomic .
10+
rw:
11+
gx-go rewrite
12+
rwundo:
13+
gx-go rewrite --undo
14+
publish: rwundo
15+
gx publish
16+
.PHONY: all gx deps test rw rwundo publish

api.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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

Comments
 (0)