mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-01 06:17:56 +08:00
api.WithOption
This commit was moved from ipfs/go-ipfs-http-client@634b00bf1a
This commit is contained in:
parent
0f7c83956b
commit
af197cb6d9
@ -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 {
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user