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

Commit 634b00b

Browse files
committed
api.WithOption
1 parent 6169321 commit 634b00b

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

api.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"strings"
1010

1111
"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"
1313
homedir "github.com/mitchellh/go-homedir"
1414
ma "github.com/multiformats/go-multiaddr"
1515
manet "github.com/multiformats/go-multiaddr-net"
@@ -27,6 +27,8 @@ var ErrNotImplemented = errors.New("not implemented")
2727
type HttpApi struct {
2828
url string
2929
httpcli *gohttp.Client
30+
31+
applyGlobal func(*RequestBuilder)
3032
}
3133

3234
//TODO: Return errors here
@@ -99,11 +101,24 @@ func NewApiWithClient(a ma.Multiaddr, c *gohttp.Client) *HttpApi {
99101
return &HttpApi{
100102
url: url,
101103
httpcli: c,
104+
applyGlobal: func(*RequestBuilder) {},
102105
}
103106
}
104107

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
107122
}
108123

109124
func (api *HttpApi) request(command string, args ...string) *RequestBuilder {

api_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"testing"
1212

1313
"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"
1515
"github.com/ipfs/go-ipfs/core/coreapi/interface/tests"
1616

1717
local "github.com/ipfs/iptb-plugins/local"
@@ -104,7 +104,7 @@ func (NodeProvider) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int)
104104

105105
// node cleanup
106106
// 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())
108108
if err != nil {
109109
return nil, err
110110
}

requestbuilder.go

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func (r *RequestBuilder) Header(name, value string) *RequestBuilder {
8686

8787
// Send sends the request and return the response.
8888
func (r *RequestBuilder) Send(ctx context.Context) (*Response, error) {
89+
r.shell.applyGlobal(r)
90+
8991
req := NewRequest(ctx, r.shell.url, r.command, r.args...)
9092
req.Opts = r.opts
9193
req.Headers = r.headers

response.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Response struct {
2121
func (r *Response) Close() error {
2222
if r.Output != nil {
2323
// 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
2525
return r.Output.Close()
2626
}
2727
return nil

0 commit comments

Comments
 (0)