Implement missing methods
This commit was moved from ipfs/go-ipfs-http-client@b76413dfe55fd33d39a680458282d85565cc1c69
Łukasz Magiera committed
Feb 14, 2019 at 18:45 UTC
e1b14d78c72edf0a782e362afb0e818e032c31cd
3 files changed
+66
-9
client/httpapi/api.go
-3
@@ -1,7 +1,6 @@
1
package httpapi
2
3
import (
4
- "errors"
4
"fmt"
5
"io/ioutil"
6
gohttp "net/http"
@@ -23,8 +22,6 @@ const (
22
EnvDir = "IPFS_PATH"
23
)
24
26
-var ErrNotImplemented = errors.New("not implemented")
27
-
25
type HttpApi struct {
26
url string
27
httpcli gohttp.Client
client/httpapi/name.go
+59
-4
@@ -2,7 +2,9 @@ package httpapi
2
3
import (
4
"context"
5
+ "encoding/json"
6
"fmt"
7
+ "io"
8
9
"github.com/ipfs/interface-go-ipfs-core"
10
caopts "github.com/ipfs/interface-go-ipfs-core/options"
@@ -38,11 +40,11 @@ func (api *NameAPI) Publish(ctx context.Context, p iface.Path, opts ...caopts.Na
40
req := api.core().request("name/publish", p.String()).
41
Option("key", options.Key).
42
Option("allow-offline", options.AllowOffline).
41
- Option("lifetime", options.ValidTime.String()).
43
+ Option("lifetime", options.ValidTime).
44
Option("resolve", false)
45
46
if options.TTL != nil {
45
- req.Option("ttl", options.TTL.String())
47
+ req.Option("ttl", options.TTL)
48
}
49
50
var out ipnsEntry
@@ -57,7 +59,60 @@ func (api *NameAPI) Publish(ctx context.Context, p iface.Path, opts ...caopts.Na
59
}
60
61
func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.NameResolveOption) (<-chan iface.IpnsResult, error) {
60
- return nil, ErrNotImplemented
62
+ options, err := caopts.NameResolveOptions(opts...)
63
+ if err != nil {
64
+ return nil, err
65
+ }
66
+
67
+ ropts := nsopts.ProcessOpts(options.ResolveOpts)
68
+ if ropts.Depth != nsopts.DefaultDepthLimit && ropts.Depth != 1 {
69
+ return nil, fmt.Errorf("Name.Resolve: depth other than 1 or %d not supported", nsopts.DefaultDepthLimit)
70
+ }
71
+
72
+ req := api.core().request("name/resolve", name).
73
+ Option("nocache", !options.Cache).
74
+ Option("recursive", ropts.Depth != 1).
75
+ Option("dht-record-count", ropts.DhtRecordCount).
76
+ Option("dht-timeout", ropts.DhtTimeout).
77
+ Option("stream", true)
78
+ resp, err := req.Send(ctx)
79
+ if err != nil {
80
+ return nil, err
81
+ }
82
+ if resp.Error != nil {
83
+ return nil, resp.Error
84
+ }
85
+
86
+ res := make(chan iface.IpnsResult)
87
+
88
+ go func() {
89
+ defer close(res)
90
+ defer resp.Close()
91
+
92
+ dec := json.NewDecoder(resp.Output)
93
+
94
+ for {
95
+ var out struct{ Path string }
96
+ err := dec.Decode(&out)
97
+ if err == io.EOF {
98
+ return
99
+ }
100
+ var ires iface.IpnsResult
101
+ if err == nil {
102
+ ires.Path, err = iface.ParsePath(out.Path)
103
+ }
104
+
105
+ select {
106
+ case res <- ires:
107
+ case <-ctx.Done():
108
+ }
109
+ if err != nil {
110
+ return
111
+ }
112
+ }
113
+ }()
114
+
115
+ return res, nil
116
}
117
118
func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.NameResolveOption) (iface.Path, error) {
@@ -75,7 +130,7 @@ func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.Nam
130
Option("nocache", !options.Cache).
131
Option("recursive", ropts.Depth != 1).
132
Option("dht-record-count", ropts.DhtRecordCount).
78
- Option("dht-timeout", ropts.DhtTimeout.String())
133
+ Option("dht-timeout", ropts.DhtTimeout)
134
135
var out struct{ Path string }
136
if err := req.Exec(ctx, &out); err != nil {
client/httpapi/path.go
+7
-2
@@ -42,6 +42,11 @@ func (api *HttpApi) ResolvePath(ctx context.Context, path iface.Path) (iface.Res
42
return iface.NewResolvedPath(ipath, out.Cid, root, out.RemPath), nil
43
}
44
45
-func (api *HttpApi) ResolveNode(context.Context, iface.Path) (ipld.Node, error) {
46
- return nil, ErrNotImplemented
45
+func (api *HttpApi) ResolveNode(ctx context.Context, p iface.Path) (ipld.Node, error) {
46
+ rp, err := api.ResolvePath(ctx, p)
47
+ if err != nil {
48
+ return nil, err
49
+ }
50
+
51
+ return api.Dag().Get(ctx, rp.Cid())
52
}