@@ -2,26 +2,80 @@ package httpapi
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "github.com/ipfs/go-ipfs/namesys/opts"
5
7
6
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"
8
10
)
9
11
10
12
type NameAPI HttpApi
11
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
14
30
}
15
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 ) {
17
60
return nil , ErrNotImplemented
18
61
}
19
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
+ }
22
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 ())
25
79
26
80
var out struct { Path string }
27
81
if err := req .Exec (ctx , & out ); err != nil {
0 commit comments