api.WithOption

This commit was moved from ipfs/go-ipfs-http-client@634b00bf1a
This commit is contained in:
Łukasz Magiera 2019-01-08 14:46:52 +01:00
parent 0f7c83956b
commit af197cb6d9
4 changed files with 23 additions and 6 deletions

View File

@ -9,7 +9,7 @@ import (
"strings"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
caopts "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"
@ -27,6 +27,8 @@ var ErrNotImplemented = errors.New("not implemented")
type HttpApi struct {
url string
httpcli *gohttp.Client
applyGlobal func(*RequestBuilder)
}
//TODO: Return errors here
@ -99,11 +101,24 @@ func NewApiWithClient(a ma.Multiaddr, c *gohttp.Client) *HttpApi {
return &HttpApi{
url: url,
httpcli: c,
applyGlobal: func(*RequestBuilder) {},
}
}
func (api *HttpApi) WithOptions(...options.ApiOption) (iface.CoreAPI, error) {
return nil, ErrNotImplemented
func (api *HttpApi) WithOptions(opts ...caopts.ApiOption) (iface.CoreAPI, error) {
options, err := caopts.ApiOptions(opts...)
if err != nil {
return nil, err
}
subApi := *api
subApi.applyGlobal = func(req *RequestBuilder) {
if options.Offline {
req.Option("offline", options.Offline)
}
}
return &subApi, nil
}
func (api *HttpApi) request(command string, args ...string) *RequestBuilder {

View File

@ -11,7 +11,7 @@ import (
"testing"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
"github.com/ipfs/go-ipfs/core/coreapi/interface/tests"
local "github.com/ipfs/iptb-plugins/local"
@ -104,7 +104,7 @@ func (NodeProvider) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int)
// node cleanup
// TODO: pass --empty-repo somehow (how?)
pins, err := apis[i].Pin().Ls(ctx, options.Pin.Type.Recursive())
pins, err := apis[i].Pin().Ls(ctx, caopts.Pin.Type.Recursive())
if err != nil {
return nil, err
}

View File

@ -86,6 +86,8 @@ func (r *RequestBuilder) Header(name, value string) *RequestBuilder {
// Send sends the request and return the response.
func (r *RequestBuilder) Send(ctx context.Context) (*Response, error) {
r.shell.applyGlobal(r)
req := NewRequest(ctx, r.shell.url, r.command, r.args...)
req.Opts = r.opts
req.Headers = r.headers

View File

@ -21,7 +21,7 @@ type Response struct {
func (r *Response) Close() error {
if r.Output != nil {
// always drain output (response body)
ioutil.ReadAll(r.Output)
//ioutil.ReadAll(r.Output) // TODO: might not be a good idea in case there is a lot of data
return r.Output.Close()
}
return nil