Implement Key API
This commit was moved from ipfs/go-ipfs-http-client@281b2bf65b19b17e4f0c047ac8f9901306bf171f
Łukasz Magiera committed
Jan 15, 2019 at 14:33 UTC
266c2f92c50df9b50bbc5aa093c793bb5d1a2265
1 file changed
+46
-11
client/httpapi/key.go
+46
-11
@@ -2,6 +2,7 @@ package httpapi
2
3
import (
4
"context"
5
+ "errors"
6
7
"github.com/ipfs/go-ipfs/core/coreapi/interface"
8
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
@@ -49,18 +50,47 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
50
if err != nil {
51
return nil, err
52
}
52
- if err := out.valid(); err != nil {
53
- return nil, err
54
- }
55
- return &out, nil
53
+ return &out, out.valid()
54
}
55
56
func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (iface.Key, bool, error) {
59
- panic("implement me")
57
+ options, err := caopts.KeyRenameOptions(opts...)
58
+ if err != nil {
59
+ return nil, false, err
60
+ }
61
+
62
+ var out struct{
63
+ Was string
64
+ Now string
65
+ Id string
66
+ Overwrite bool
67
+ }
68
+ err = api.core().request("key/rename", oldName, newName).
69
+ Option("force", options.Force).
70
+ Exec(ctx, &out)
71
+ if err != nil {
72
+ return nil, false, err
73
+ }
74
+
75
+ id := &keyOutput{JName: out.Now, Id: out.Id}
76
+ return id, out.Overwrite, id.valid()
77
}
78
79
func (api *KeyAPI) List(ctx context.Context) ([]iface.Key, error) {
63
- panic("implement me")
80
+ var out struct{ Keys []*keyOutput }
81
+ if err := api.core().request("key/list").Exec(ctx, &out); err != nil {
82
+ return nil, err
83
+ }
84
+
85
+ res := make([]iface.Key, len(out.Keys))
86
+ for i, k := range out.Keys {
87
+ if err := k.valid(); err != nil {
88
+ return nil, err
89
+ }
90
+ res[i] = k
91
+ }
92
+
93
+ return res, nil
94
}
95
96
func (api *KeyAPI) Self(ctx context.Context) (iface.Key, error) {
@@ -70,14 +100,19 @@ func (api *KeyAPI) Self(ctx context.Context) (iface.Key, error) {
100
}
101
102
out := keyOutput{JName: "self", Id: id.ID}
73
- if err := out.valid(); err != nil {
74
- return nil, err
75
- }
76
- return &out, nil
103
+ return &out, out.valid()
104
}
105
106
func (api *KeyAPI) Remove(ctx context.Context, name string) (iface.Key, error) {
80
- panic("implement me")
107
+ var out struct{ Keys []keyOutput }
108
+ if err := api.core().request("key/rm", name).Exec(ctx, &out); err != nil {
109
+ return nil, err
110
+ }
111
+ if len(out.Keys) != 1 {
112
+ return nil, errors.New("got unexpected number of keys back")
113
+ }
114
+
115
+ return &out.Keys[0], out.Keys[0].valid()
116
}
117
118
func (api *KeyAPI) core() *HttpApi {