Implement Key API

This commit was moved from ipfs/go-ipfs-http-client@281b2bf65b
This commit is contained in:
Łukasz Magiera 2019-01-15 14:33:03 +01:00
parent c213e26542
commit 266c2f92c5

View File

@ -2,6 +2,7 @@ package httpapi
import (
"context"
"errors"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
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
if err != nil {
return nil, err
}
if err := out.valid(); err != nil {
return nil, err
}
return &out, nil
return &out, out.valid()
}
func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (iface.Key, bool, error) {
panic("implement me")
options, err := caopts.KeyRenameOptions(opts...)
if err != nil {
return nil, false, err
}
var out struct{
Was string
Now string
Id string
Overwrite bool
}
err = api.core().request("key/rename", oldName, newName).
Option("force", options.Force).
Exec(ctx, &out)
if err != nil {
return nil, false, err
}
id := &keyOutput{JName: out.Now, Id: out.Id}
return id, out.Overwrite, id.valid()
}
func (api *KeyAPI) List(ctx context.Context) ([]iface.Key, error) {
panic("implement me")
var out struct{ Keys []*keyOutput }
if err := api.core().request("key/list").Exec(ctx, &out); err != nil {
return nil, err
}
res := make([]iface.Key, len(out.Keys))
for i, k := range out.Keys {
if err := k.valid(); err != nil {
return nil, err
}
res[i] = k
}
return res, nil
}
func (api *KeyAPI) Self(ctx context.Context) (iface.Key, error) {
@ -70,14 +100,19 @@ func (api *KeyAPI) Self(ctx context.Context) (iface.Key, error) {
}
out := keyOutput{JName: "self", Id: id.ID}
if err := out.valid(); err != nil {
return nil, err
}
return &out, nil
return &out, out.valid()
}
func (api *KeyAPI) Remove(ctx context.Context, name string) (iface.Key, error) {
panic("implement me")
var out struct{ Keys []keyOutput }
if err := api.core().request("key/rm", name).Exec(ctx, &out); err != nil {
return nil, err
}
if len(out.Keys) != 1 {
return nil, errors.New("got unexpected number of keys back")
}
return &out.Keys[0], out.Keys[0].valid()
}
func (api *KeyAPI) core() *HttpApi {