|
| 1 | +// Deprecated: This repo has moved inside Kubo in order to make it easier to |
| 2 | +// keep kubo and the http-client in sync, please use github.com/ipfs/kubo/client/rpc instead. |
1 | 3 | package httpapi |
2 | 4 |
|
3 | 5 | import ( |
4 | | - "errors" |
5 | | - "fmt" |
| 6 | + "context" |
6 | 7 | "net/http" |
7 | | - "os" |
8 | | - "path/filepath" |
9 | | - "strings" |
10 | 8 |
|
11 | | - iface "github.com/ipfs/boxo/coreiface" |
12 | | - caopts "github.com/ipfs/boxo/coreiface/options" |
13 | | - "github.com/mitchellh/go-homedir" |
| 9 | + "github.com/ipfs/kubo/client/rpc" |
14 | 10 | ma "github.com/multiformats/go-multiaddr" |
15 | | - manet "github.com/multiformats/go-multiaddr/net" |
16 | 11 | ) |
17 | 12 |
|
18 | | -const ( |
19 | | - DefaultPathName = ".ipfs" |
20 | | - DefaultPathRoot = "~/" + DefaultPathName |
21 | | - DefaultApiFile = "api" |
22 | | - EnvDir = "IPFS_PATH" |
23 | | -) |
| 13 | +// Deprecated: use [rpc.DefaultPathName] instead. |
| 14 | +const DefaultPathName = rpc.DefaultPathName |
24 | 15 |
|
25 | | -// ErrApiNotFound if we fail to find a running daemon. |
26 | | -var ErrApiNotFound = errors.New("ipfs api address could not be found") |
27 | | - |
28 | | -// HttpApi implements github.com/ipfs/interface-go-ipfs-core/CoreAPI using |
29 | | -// IPFS HTTP API. |
30 | | -// |
31 | | -// For interface docs see |
32 | | -// https://godoc.org/github.com/ipfs/interface-go-ipfs-core#CoreAPI |
33 | | -type HttpApi struct { |
34 | | - url string |
35 | | - httpcli http.Client |
36 | | - Headers http.Header |
37 | | - applyGlobal func(*requestBuilder) |
38 | | -} |
| 16 | +// Deprecated: use [rpc.DefaultPathRoot] instead. |
| 17 | +const DefaultPathRoot = rpc.DefaultPathRoot |
39 | 18 |
|
40 | | -// NewLocalApi tries to construct new HttpApi instance communicating with local |
41 | | -// IPFS daemon |
42 | | -// |
43 | | -// Daemon api address is pulled from the $IPFS_PATH/api file. |
44 | | -// If $IPFS_PATH env var is not present, it defaults to ~/.ipfs |
45 | | -func NewLocalApi() (*HttpApi, error) { |
46 | | - baseDir := os.Getenv(EnvDir) |
47 | | - if baseDir == "" { |
48 | | - baseDir = DefaultPathRoot |
49 | | - } |
| 19 | +// Deprecated: use [rpc.DefaultApiFile] instead. |
| 20 | +const DefaultApiFile = rpc.DefaultApiFile |
50 | 21 |
|
51 | | - return NewPathApi(baseDir) |
52 | | -} |
| 22 | +// Deprecated: use [rpc.EnvDir] instead. |
| 23 | +const EnvDir = rpc.EnvDir |
53 | 24 |
|
54 | | -// NewPathApi constructs new HttpApi by pulling api address from specified |
55 | | -// ipfspath. Api file should be located at $ipfspath/api |
56 | | -func NewPathApi(ipfspath string) (*HttpApi, error) { |
57 | | - a, err := ApiAddr(ipfspath) |
58 | | - if err != nil { |
59 | | - if os.IsNotExist(err) { |
60 | | - err = ErrApiNotFound |
61 | | - } |
62 | | - return nil, err |
63 | | - } |
64 | | - return NewApi(a) |
65 | | -} |
| 25 | +// Deprecated: use [rpc.ErrApiNotFound] instead. |
| 26 | +var ErrApiNotFound = rpc.ErrApiNotFound |
66 | 27 |
|
67 | | -// ApiAddr reads api file in specified ipfs path |
68 | | -func ApiAddr(ipfspath string) (ma.Multiaddr, error) { |
69 | | - baseDir, err := homedir.Expand(ipfspath) |
70 | | - if err != nil { |
71 | | - return nil, err |
72 | | - } |
| 28 | +// Deprecated: use [rpc.HttpApi] instead. |
| 29 | +type HttpApi = rpc.HttpApi |
73 | 30 |
|
74 | | - apiFile := filepath.Join(baseDir, DefaultApiFile) |
| 31 | +// Deprecated: use [rpc.BlockAPI] instead. |
| 32 | +type BlockAPI = rpc.BlockAPI |
75 | 33 |
|
76 | | - api, err := os.ReadFile(apiFile) |
77 | | - if err != nil { |
78 | | - return nil, err |
79 | | - } |
| 34 | +// Deprecated: use [rpc.HttpDagServ] instead. |
| 35 | +type HttpDagServ = rpc.HttpDagServ |
80 | 36 |
|
81 | | - return ma.NewMultiaddr(strings.TrimSpace(string(api))) |
82 | | -} |
| 37 | +// Deprecated: use [rpc.DhtAPI] instead. |
| 38 | +type DhtAPI = rpc.DhtAPI |
83 | 39 |
|
84 | | -// NewApi constructs HttpApi with specified endpoint |
85 | | -func NewApi(a ma.Multiaddr) (*HttpApi, error) { |
86 | | - c := &http.Client{ |
87 | | - Transport: &http.Transport{ |
88 | | - Proxy: http.ProxyFromEnvironment, |
89 | | - DisableKeepAlives: true, |
90 | | - }, |
91 | | - } |
92 | | - |
93 | | - return NewApiWithClient(a, c) |
94 | | -} |
| 40 | +// Deprecated: use [rpc.KeyAPI] instead. |
| 41 | +type KeyAPI = rpc.KeyAPI |
95 | 42 |
|
96 | | -// NewApiWithClient constructs HttpApi with specified endpoint and custom http client |
97 | | -func NewApiWithClient(a ma.Multiaddr, c *http.Client) (*HttpApi, error) { |
98 | | - _, url, err := manet.DialArgs(a) |
99 | | - if err != nil { |
100 | | - return nil, err |
101 | | - } |
102 | | - |
103 | | - if a, err := ma.NewMultiaddr(url); err == nil { |
104 | | - _, host, err := manet.DialArgs(a) |
105 | | - if err == nil { |
106 | | - url = host |
107 | | - } |
108 | | - } |
109 | | - |
110 | | - proto := "http://" |
111 | | - |
112 | | - // By default, DialArgs is going to provide details suitable for connecting |
113 | | - // a socket to, but not really suitable for making an informed choice of http |
114 | | - // protocol. For multiaddresses specifying tls and/or https we want to make |
115 | | - // a https request instead of a http request. |
116 | | - protocols := a.Protocols() |
117 | | - for _, p := range protocols { |
118 | | - if p.Code == ma.P_HTTPS || p.Code == ma.P_TLS { |
119 | | - proto = "https://" |
120 | | - break |
121 | | - } |
122 | | - } |
123 | | - |
124 | | - return NewURLApiWithClient(proto+url, c) |
125 | | -} |
| 43 | +// Deprecated: use [rpc.NameAPI] instead. |
| 44 | +type NameAPI = rpc.NameAPI |
126 | 45 |
|
127 | | -func NewURLApiWithClient(url string, c *http.Client) (*HttpApi, error) { |
128 | | - api := &HttpApi{ |
129 | | - url: url, |
130 | | - httpcli: *c, |
131 | | - Headers: make(map[string][]string), |
132 | | - applyGlobal: func(*requestBuilder) {}, |
133 | | - } |
134 | | - |
135 | | - // We don't support redirects. |
136 | | - api.httpcli.CheckRedirect = func(_ *http.Request, _ []*http.Request) error { |
137 | | - return fmt.Errorf("unexpected redirect") |
138 | | - } |
139 | | - return api, nil |
140 | | -} |
| 46 | +// Deprecated: use [rpc.ObjectAPI] instead. |
| 47 | +type ObjectAPI = rpc.ObjectAPI |
141 | 48 |
|
142 | | -func (api *HttpApi) WithOptions(opts ...caopts.ApiOption) (iface.CoreAPI, error) { |
143 | | - options, err := caopts.ApiOptions(opts...) |
144 | | - if err != nil { |
145 | | - return nil, err |
146 | | - } |
| 49 | +// Deprecated: use [rpc.PinAPI] instead. |
| 50 | +type PinAPI = rpc.PinAPI |
147 | 51 |
|
148 | | - subApi := *api |
149 | | - subApi.applyGlobal = func(req *requestBuilder) { |
150 | | - if options.Offline { |
151 | | - req.Option("offline", options.Offline) |
152 | | - } |
153 | | - } |
| 52 | +// Deprecated: use [rpc.PubsubAPI] instead. |
| 53 | +type PubsubAPI = rpc.PubsubAPI |
154 | 54 |
|
155 | | - return &subApi, nil |
156 | | -} |
| 55 | +// Deprecated: use [rpc.RoutingAPI] instead. |
| 56 | +type RoutingAPI = rpc.RoutingAPI |
157 | 57 |
|
158 | | -func (api *HttpApi) Request(command string, args ...string) RequestBuilder { |
159 | | - headers := make(map[string]string) |
160 | | - if api.Headers != nil { |
161 | | - for k := range api.Headers { |
162 | | - headers[k] = api.Headers.Get(k) |
163 | | - } |
164 | | - } |
165 | | - return &requestBuilder{ |
166 | | - command: command, |
167 | | - args: args, |
168 | | - shell: api, |
169 | | - headers: headers, |
170 | | - } |
171 | | -} |
| 58 | +// Deprecated: use [rpc.SwarmAPI] instead. |
| 59 | +type SwarmAPI = rpc.SwarmAPI |
172 | 60 |
|
173 | | -func (api *HttpApi) Unixfs() iface.UnixfsAPI { |
174 | | - return (*UnixfsAPI)(api) |
175 | | -} |
| 61 | +// Deprecated: use [rpc.UnixfsAPI] instead. |
| 62 | +type UnixfsAPI = rpc.UnixfsAPI |
176 | 63 |
|
177 | | -func (api *HttpApi) Block() iface.BlockAPI { |
178 | | - return (*BlockAPI)(api) |
| 64 | +// Deprecated: use [rpc.NewLocalApi] instead. |
| 65 | +func NewLocalApi() (*HttpApi, error) { |
| 66 | + return rpc.NewLocalApi() |
179 | 67 | } |
180 | 68 |
|
181 | | -func (api *HttpApi) Dag() iface.APIDagService { |
182 | | - return (*HttpDagServ)(api) |
| 69 | +// Deprecated: use [rpc.NewPathApi] instead. |
| 70 | +func NewPathApi(ipfspath string) (*HttpApi, error) { |
| 71 | + return rpc.NewPathApi(ipfspath) |
183 | 72 | } |
184 | 73 |
|
185 | | -func (api *HttpApi) Name() iface.NameAPI { |
186 | | - return (*NameAPI)(api) |
| 74 | +// Deprecated: use [rpc.ApiAddr] instead. |
| 75 | +func ApiAddr(ipfspath string) (ma.Multiaddr, error) { |
| 76 | + return rpc.ApiAddr(ipfspath) |
187 | 77 | } |
188 | 78 |
|
189 | | -func (api *HttpApi) Key() iface.KeyAPI { |
190 | | - return (*KeyAPI)(api) |
| 79 | +// Deprecated: use [rpc.NewApi] instead. |
| 80 | +func NewApi(a ma.Multiaddr) (*HttpApi, error) { |
| 81 | + return rpc.NewApi(a) |
191 | 82 | } |
192 | 83 |
|
193 | | -func (api *HttpApi) Pin() iface.PinAPI { |
194 | | - return (*PinAPI)(api) |
| 84 | +// Deprecated: use [rpc.NewApiWithClient] instead. |
| 85 | +func NewApiWithClient(a ma.Multiaddr, c *http.Client) (*HttpApi, error) { |
| 86 | + return rpc.NewApiWithClient(a, c) |
195 | 87 | } |
196 | 88 |
|
197 | | -func (api *HttpApi) Object() iface.ObjectAPI { |
198 | | - return (*ObjectAPI)(api) |
| 89 | +// Deprecated: use [rpc.NewURLApiWithClient] instead. |
| 90 | +func NewURLApiWithClient(url string, c *http.Client) (*HttpApi, error) { |
| 91 | + return rpc.NewURLApiWithClient(url, c) |
199 | 92 | } |
200 | 93 |
|
201 | | -func (api *HttpApi) Dht() iface.DhtAPI { |
202 | | - return (*DhtAPI)(api) |
203 | | -} |
| 94 | +// Deprecated: use [rpc.Request] instead. |
| 95 | +type Request = rpc.Request |
204 | 96 |
|
205 | | -func (api *HttpApi) Swarm() iface.SwarmAPI { |
206 | | - return (*SwarmAPI)(api) |
| 97 | +// Deprecated: use [rpc.NewRequest] instead. |
| 98 | +func NewRequest(ctx context.Context, url, command string, args ...string) *Request { |
| 99 | + return rpc.NewRequest(ctx, url, command, args...) |
207 | 100 | } |
208 | 101 |
|
209 | | -func (api *HttpApi) PubSub() iface.PubSubAPI { |
210 | | - return (*PubsubAPI)(api) |
211 | | -} |
| 102 | +// Deprecated: use [rpc.RequestBuilder] instead. |
| 103 | +type RequestBuilder = rpc.RequestBuilder |
212 | 104 |
|
213 | | -func (api *HttpApi) Routing() iface.RoutingAPI { |
214 | | - return (*RoutingAPI)(api) |
215 | | -} |
| 105 | +// Deprecated: use [rpc.Error] instead. |
| 106 | +type Error = rpc.Error |
| 107 | + |
| 108 | +// Deprecated: use [rpc.Response] instead. |
| 109 | +type Response = rpc.Response |
0 commit comments