Imprement partian Pin API
This commit was moved from ipfs/go-ipfs-http-client@dbf90eac67d95722e88056ca16fa769a32dcd48f
Łukasz Magiera committed
Jan 8, 2019 at 03:18 UTC
a6636aac599a1a8a4802ccadb885909f0ab88629
3 files changed
+94
-2
client/httpapi/api.go
+1
-1
@@ -135,7 +135,7 @@ func (api *HttpApi) Key() iface.KeyAPI {
135
}
136
137
func (api *HttpApi) Pin() iface.PinAPI {
138
- return nil
138
+ return (*PinAPI)(api)
139
}
140
141
func (api *HttpApi) Object() iface.ObjectAPI {
client/httpapi/api_test.go
+15
-1
@@ -11,6 +11,7 @@ import (
11
"testing"
12
13
"github.com/ipfs/go-ipfs/core/coreapi/interface"
14
+ "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
15
"github.com/ipfs/go-ipfs/core/coreapi/interface/tests"
16
17
local "github.com/ipfs/iptb-plugins/local"
@@ -39,7 +40,7 @@ func (NodeProvider) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int)
40
return nil, err
41
}
42
42
- c := cli.NewCli()
43
+ c := cli.NewCli() //TODO: is there a better way?
44
45
initArgs := []string{"iptb", "--IPTB_ROOT", dir, "auto", "-type", "localipfs", "-count", strconv.FormatInt(int64(n), 10)}
46
if err := c.Run(initArgs); err != nil {
@@ -100,6 +101,19 @@ func (NodeProvider) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int)
101
},
102
}
103
apis[i] = NewApiWithClient(a, c)
104
+
105
+ // node cleanup
106
+ // TODO: pass --empty-repo somehow (how?)
107
+ pins, err := apis[i].Pin().Ls(ctx, options.Pin.Type.Recursive())
108
+ if err != nil {
109
+ return nil, err
110
+ }
111
+ for _, pin := range pins { //TODO: parallel
112
+ if err := apis[i].Pin().Rm(ctx, pin.Path()); err != nil {
113
+ return nil, err
114
+ }
115
+ }
116
+
117
}
118
119
return apis, nil
client/httpapi/pin.go
new
+78
@@ -0,0 +1,78 @@
1
+package httpapi
2
+
3
+import (
4
+ "context"
5
+ "github.com/ipfs/go-cid"
6
+
7
+ "github.com/ipfs/go-ipfs/core/coreapi/interface"
8
+ caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9
+)
10
+
11
+type PinAPI HttpApi
12
+
13
+type pinRefKeyObject struct {
14
+ Type string
15
+}
16
+
17
+type pinRefKeyList struct {
18
+ Keys map[string]pinRefKeyObject
19
+}
20
+
21
+type pin struct {
22
+ path iface.ResolvedPath
23
+ typ string
24
+}
25
+
26
+func (p *pin) Path() iface.ResolvedPath {
27
+ return p.path
28
+}
29
+
30
+func (p *pin) Type() string {
31
+ return p.typ
32
+}
33
+
34
+
35
+func (api *PinAPI) Add(context.Context, iface.Path, ...caopts.PinAddOption) error {
36
+ panic("implement me")
37
+}
38
+
39
+func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) ([]iface.Pin, error) {
40
+ options, err := caopts.PinLsOptions(opts...)
41
+ if err != nil {
42
+ return nil, err
43
+ }
44
+
45
+ var out pinRefKeyList
46
+ err = api.core().request("pin/ls").
47
+ Option("type", options.Type).Exec(ctx, &out)
48
+ if err != nil {
49
+ return nil, err
50
+ }
51
+
52
+ pins := make([]iface.Pin, 0, len(out.Keys))
53
+ for hash, p := range out.Keys {
54
+ c, err := cid.Parse(hash)
55
+ if err != nil {
56
+ return nil, err
57
+ }
58
+ pins = append(pins, &pin{typ: p.Type, path: iface.IpldPath(c)})
59
+ }
60
+
61
+ return pins, nil
62
+}
63
+
64
+func (api *PinAPI) Rm(ctx context.Context, p iface.Path) error {
65
+ return api.core().request("pin/rm", p.String()).Exec(ctx, nil)
66
+}
67
+
68
+func (api *PinAPI) Update(ctx context.Context, from iface.Path, to iface.Path, opts ...caopts.PinUpdateOption) error {
69
+ panic("implement me")
70
+}
71
+
72
+func (api *PinAPI) Verify(context.Context) (<-chan iface.PinStatus, error) {
73
+ panic("implement me")
74
+}
75
+
76
+func (api *PinAPI) core() *HttpApi {
77
+ return (*HttpApi)(api)
78
+}