1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-27 07:57:30 +08:00

coreapi: dht: refactor options after rebase

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera
2018-07-19 13:27:06 +02:00
committed by Steven Allen
parent 906c747064
commit 77ceb7546b
5 changed files with 21 additions and 21 deletions

View File

@ -65,5 +65,5 @@ func (api *CoreAPI) Pin() coreiface.PinAPI {
// Dht returns the DhtAPI interface implementation backed by the go-ipfs node
func (api *CoreAPI) Dht() coreiface.DhtAPI {
return &DhtAPI{api, nil}
return (*DhtAPI)(api)
}

View File

@ -18,10 +18,7 @@ import (
blockstore "gx/ipfs/Qmeg56ecxRnVv7VWViMrDeEMoBHaNFMs4vQnyQrJ79Zz7i/go-ipfs-blockstore"
)
type DhtAPI struct {
*CoreAPI
*caopts.DhtOptions
}
type DhtAPI CoreAPI
func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
pi, err := api.node.Routing.FindPeer(ctx, peer.ID(p))
@ -38,7 +35,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...
return nil, err
}
rp, err := api.ResolvePath(ctx, p)
rp, err := api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}
@ -62,7 +59,7 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao
return errors.New("cannot provide in offline mode")
}
rp, err := api.ResolvePath(ctx, path)
rp, err := api.core().ResolvePath(ctx, path)
if err != nil {
return err
}
@ -127,3 +124,7 @@ func provideKeysRec(ctx context.Context, r routing.IpfsRouting, bs blockstore.Bl
return nil
}
func (api *DhtAPI) core() coreiface.CoreAPI {
return (*CoreAPI)(api)
}

View File

@ -7,6 +7,7 @@ import (
"testing"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
blocks "gx/ipfs/QmWAzSEoqZ6xU6pu8yL8e5WaMb7wtbfbhhN4p1DknUPtr3/go-block-format"
@ -50,7 +51,7 @@ func TestDhtFindProviders(t *testing.T) {
t.Fatal(err)
}
out, err := apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1))
out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.WithNumProviders(1))
if err != nil {
t.Fatal(err)
}
@ -79,7 +80,7 @@ func TestDhtProvide(t *testing.T) {
nds[0].Blockstore.Put(b)
p := iface.IpfsPath(b.Cid())
out, err := apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1))
out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.WithNumProviders(1))
if err != nil {
t.Fatal(err)
}
@ -95,7 +96,7 @@ func TestDhtProvide(t *testing.T) {
t.Fatal(err)
}
out, err = apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1))
out, err = apis[2].Dht().FindProviders(ctx, p, options.Dht.WithNumProviders(1))
if err != nil {
t.Fatal(err)
}

View File

@ -19,14 +19,6 @@ type DhtAPI interface {
// given a key.
FindProviders(context.Context, Path, ...options.DhtFindProvidersOption) (<-chan pstore.PeerInfo, error)
// WithNumProviders is an option for FindProviders which specifies the
// number of peers to look for. Default is 20
WithNumProviders(numProviders int) options.DhtFindProvidersOption
// Provide announces to the network that you are providing given values
Provide(context.Context, Path, ...options.DhtProvideOption) error
// WithRecursive is an option for Provide which specifies whether to provide
// the given path recursively
WithRecursive(recursive bool) options.DhtProvideOption
}

View File

@ -39,16 +39,22 @@ func DhtFindProvidersOptions(opts ...DhtFindProvidersOption) (*DhtFindProvidersS
return options, nil
}
type DhtOptions struct{}
type dhtOpts struct{}
func (api *DhtOptions) WithRecursive(recursive bool) DhtProvideOption {
var Dht dhtOpts
// WithRecursive is an option for Dht.Provide which specifies whether to provide
// the given path recursively
func (dhtOpts) WithRecursive(recursive bool) DhtProvideOption {
return func(settings *DhtProvideSettings) error {
settings.Recursive = recursive
return nil
}
}
func (api *DhtOptions) WithNumProviders(numProviders int) DhtFindProvidersOption {
// WithNumProviders is an option for Dht.FindProviders which specifies the
// number of peers to look for. Default is 20
func (dhtOpts) WithNumProviders(numProviders int) DhtFindProvidersOption {
return func(settings *DhtFindProvidersSettings) error {
settings.NumProviders = numProviders
return nil