Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit e19e5f5

Browse files
committed
implement .Name
1 parent 16f77b2 commit e19e5f5

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

name.go

+62-8
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,80 @@ package httpapi
22

33
import (
44
"context"
5+
"fmt"
6+
"github.com/ipfs/go-ipfs/namesys/opts"
57

68
"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"
810
)
911

1012
type NameAPI HttpApi
1113

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
1430
}
1531

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) {
1760
return nil, ErrNotImplemented
1861
}
1962

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+
}
2273

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())
2579

2680
var out struct{ Path string }
2781
if err := req.Exec(ctx, &out); err != nil {

0 commit comments

Comments
 (0)