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

Commit

Permalink
Implement Key API
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Jan 15, 2019
1 parent 66593af commit 281b2bf
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit 281b2bf

Please sign in to comment.