@cryptotaxi247 / kubo / commits / af197cb6d

api.WithOption

This commit was moved from ipfs/go-ipfs-http-client@634b00bf1a42dc7f2a2dc75f85c77e5bb5ef727c

Łukasz Magiera committed Jan 8, 2019 at 14:46 UTC af197cb6d90da1b51525e68f97a30ebfc322e134
4 files changed +23 -6
client/httpapi/api.go
+18 -3
@@ -9,7 +9,7 @@ import (
9 "strings"
10
11 "github.com/ipfs/go-ipfs/core/coreapi/interface"
12 - "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
12 + caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13 homedir "github.com/mitchellh/go-homedir"
14 ma "github.com/multiformats/go-multiaddr"
15 manet "github.com/multiformats/go-multiaddr-net"
@@ -27,6 +27,8 @@ var ErrNotImplemented = errors.New("not implemented")
27 type HttpApi struct {
28 url string
29 httpcli *gohttp.Client
30 +
31 + applyGlobal func(*RequestBuilder)
32 }
33
34 //TODO: Return errors here
@@ -99,11 +101,24 @@ func NewApiWithClient(a ma.Multiaddr, c *gohttp.Client) *HttpApi {
101 return &HttpApi{
102 url: url,
103 httpcli: c,
104 + applyGlobal: func(*RequestBuilder) {},
105 }
106 }
107
105 -func (api *HttpApi) WithOptions(...options.ApiOption) (iface.CoreAPI, error) {
106 - return nil, ErrNotImplemented
108 +func (api *HttpApi) WithOptions(opts ...caopts.ApiOption) (iface.CoreAPI, error) {
109 + options, err := caopts.ApiOptions(opts...)
110 + if err != nil {
111 + return nil, err
112 + }
113 +
114 + subApi := *api
115 + subApi.applyGlobal = func(req *RequestBuilder) {
116 + if options.Offline {
117 + req.Option("offline", options.Offline)
118 + }
119 + }
120 +
121 + return &subApi, nil
122 }
123
124 func (api *HttpApi) request(command string, args ...string) *RequestBuilder {
client/httpapi/api_test.go
+2 -2
@@ -11,7 +11,7 @@ import (
11 "testing"
12
13 "github.com/ipfs/go-ipfs/core/coreapi/interface"
14 - "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
14 + caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
15 "github.com/ipfs/go-ipfs/core/coreapi/interface/tests"
16
17 local "github.com/ipfs/iptb-plugins/local"
@@ -104,7 +104,7 @@ func (NodeProvider) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int)
104
105 // node cleanup
106 // TODO: pass --empty-repo somehow (how?)
107 - pins, err := apis[i].Pin().Ls(ctx, options.Pin.Type.Recursive())
107 + pins, err := apis[i].Pin().Ls(ctx, caopts.Pin.Type.Recursive())
108 if err != nil {
109 return nil, err
110 }
client/httpapi/requestbuilder.go
+2
@@ -86,6 +86,8 @@ func (r *RequestBuilder) Header(name, value string) *RequestBuilder {
86
87 // Send sends the request and return the response.
88 func (r *RequestBuilder) Send(ctx context.Context) (*Response, error) {
89 + r.shell.applyGlobal(r)
90 +
91 req := NewRequest(ctx, r.shell.url, r.command, r.args...)
92 req.Opts = r.opts
93 req.Headers = r.headers
client/httpapi/response.go
+1 -1
@@ -21,7 +21,7 @@ type Response struct {
21 func (r *Response) Close() error {
22 if r.Output != nil {
23 // always drain output (response body)
24 - ioutil.ReadAll(r.Output)
24 + //ioutil.ReadAll(r.Output) // TODO: might not be a good idea in case there is a lot of data
25 return r.Output.Close()
26 }
27 return nil