This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathapi.go
155 lines (124 loc) · 2.71 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package httpapi
import (
"errors"
"io/ioutil"
gohttp "net/http"
"os"
"path"
"strings"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
homedir "github.com/mitchellh/go-homedir"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
)
const (
DefaultPathName = ".ipfs"
DefaultPathRoot = "~/" + DefaultPathName
DefaultApiFile = "api"
EnvDir = "IPFS_PATH"
)
var ErrNotImplemented = errors.New("not implemented")
type HttpApi struct {
url string
httpcli *gohttp.Client
}
//TODO: Return errors here
func NewLocalApi() iface.CoreAPI {
baseDir := os.Getenv(EnvDir)
if baseDir == "" {
baseDir = DefaultPathRoot
}
return NewPathApi(baseDir)
}
func NewPathApi(p string) iface.CoreAPI {
a := ApiAddr(p)
if a == nil {
return nil
}
return NewApi(a)
}
func ApiAddr(p string) ma.Multiaddr {
baseDir, err := homedir.Expand(p)
if err != nil {
return nil
}
apiFile := path.Join(baseDir, DefaultApiFile)
if _, err := os.Stat(apiFile); err != nil {
return nil
}
api, err := ioutil.ReadFile(apiFile)
if err != nil {
return nil
}
maddr, err := ma.NewMultiaddr(strings.TrimSpace(string(api)))
if err != nil {
return nil
}
return maddr
}
func NewApi(a ma.Multiaddr) *HttpApi { // TODO: should be MAddr?
c := &gohttp.Client{
Transport: &gohttp.Transport{
Proxy: gohttp.ProxyFromEnvironment,
DisableKeepAlives: true,
},
}
return NewApiWithClient(a, c)
}
func NewApiWithClient(a ma.Multiaddr, c *gohttp.Client) *HttpApi {
_, url, err := manet.DialArgs(a)
if err != nil {
return nil // TODO: return that error
}
if a, err := ma.NewMultiaddr(url); err == nil {
_, host, err := manet.DialArgs(a)
if err == nil {
url = host
}
}
return &HttpApi{
url: url,
httpcli: c,
}
}
func (api *HttpApi) WithOptions(...options.ApiOption) (iface.CoreAPI, error) {
return nil, ErrNotImplemented
}
func (api *HttpApi) request(command string, args ...string) *RequestBuilder {
return &RequestBuilder{
command: command,
args: args,
shell: api,
}
}
func (api *HttpApi) Unixfs() iface.UnixfsAPI {
return (*UnixfsAPI)(api)
}
func (api *HttpApi) Block() iface.BlockAPI {
return (*BlockAPI)(api)
}
func (api *HttpApi) Dag() iface.DagAPI {
return nil
}
func (api *HttpApi) Name() iface.NameAPI {
return (*NameAPI)(api)
}
func (api *HttpApi) Key() iface.KeyAPI {
return (*KeyAPI)(api)
}
func (api *HttpApi) Pin() iface.PinAPI {
return nil
}
func (api *HttpApi) Object() iface.ObjectAPI {
return nil
}
func (api *HttpApi) Dht() iface.DhtAPI {
return nil
}
func (api *HttpApi) Swarm() iface.SwarmAPI {
return nil
}
func (api *HttpApi) PubSub() iface.PubSubAPI {
return nil
}