implement .Name
This commit was moved from ipfs/go-ipfs-http-client@e19e5f54e48d0909796add6d57e3e74e84eba85e
Łukasz Magiera committed
Jan 8, 2019 at 02:09 UTC
f638bae3a960a74d284503c30086f0bbffbe73c0
1 file changed
+62
-8
client/httpapi/name.go
+62
-8
@@ -2,26 +2,80 @@ package httpapi
2
3
import (
4
"context"
5
+ "fmt"
6
+ "github.com/ipfs/go-ipfs/namesys/opts"
7
8
"github.com/ipfs/go-ipfs/core/coreapi/interface"
7
- "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9
+ caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
10
)
11
12
type NameAPI HttpApi
13
12
-func (api *NameAPI) Publish(ctx context.Context, p iface.Path, opts ...options.NamePublishOption) (iface.IpnsEntry, error) {
13
- return nil, ErrNotImplemented
14
+type ipnsEntry struct {
15
+ JName string `json:"Name"`
16
+ JValue string `json:"Value"`
17
+}
18
+
19
+func (e *ipnsEntry) valid() (iface.Path, error) {
20
+ return iface.ParsePath(e.JValue)
21
+}
22
+
23
+func (e *ipnsEntry) Name() string {
24
+ return e.JName
25
+}
26
+
27
+func (e *ipnsEntry) Value() iface.Path {
28
+ p, _ := e.valid()
29
+ return p
30
}
31
16
-func (api *NameAPI) Search(ctx context.Context, name string, opts ...options.NameResolveOption) (<-chan iface.IpnsResult, error) {
32
+func (api *NameAPI) Publish(ctx context.Context, p iface.Path, opts ...caopts.NamePublishOption) (iface.IpnsEntry, error) {
33
+ options, err := caopts.NamePublishOptions(opts...)
34
+ if err != nil {
35
+ return nil, err
36
+ }
37
+
38
+ req := api.core().request("name/publish", p.String()).
39
+ Option("key", options.Key).
40
+ Option("allow-offline", options.AllowOffline).
41
+ Option("lifetime", options.ValidTime.String()).
42
+ Option("resolve", false)
43
+
44
+ if options.TTL != nil {
45
+ req.Option("ttl", options.TTL.String())
46
+ }
47
+
48
+ var out ipnsEntry
49
+ if err := req.Exec(ctx, &out); err != nil {
50
+ return nil, err
51
+ }
52
+ if _, err := out.valid(); err != nil {
53
+ return nil, err
54
+ }
55
+
56
+ return &out, nil
57
+}
58
+
59
+func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.NameResolveOption) (<-chan iface.IpnsResult, error) {
60
return nil, ErrNotImplemented
61
}
62
20
-func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (iface.Path, error) {
21
- // TODO: options!
63
+func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.NameResolveOption) (iface.Path, error) {
64
+ options, err := caopts.NameResolveOptions(opts...)
65
+ if err != nil {
66
+ return nil, err
67
+ }
68
+
69
+ ropts := nsopts.ProcessOpts(options.ResolveOpts)
70
+ if ropts.Depth != nsopts.DefaultDepthLimit && ropts.Depth != 1 {
71
+ return nil, fmt.Errorf("Name.Resolve: depth other than 1 or %d not supported", nsopts.DefaultDepthLimit)
72
+ }
73
23
- req := api.core().request("name/resolve")
24
- req.Arguments(name)
74
+ req := api.core().request("name/resolve", name).
75
+ Option("nocache", !options.Cache).
76
+ Option("recursive", ropts.Depth != 1).
77
+ Option("dht-record-count", ropts.DhtRecordCount).
78
+ Option("dht-timeout", ropts.DhtTimeout.String())
79
80
var out struct{ Path string }
81
if err := req.Exec(ctx, &out); err != nil {