Add initial support for Keybase's kv store api

This commit is contained in:
Sam
2020-01-17 13:55:21 -05:00
parent ae285fd0ce
commit 77e35028fa
3 changed files with 90 additions and 0 deletions

View File

@ -87,6 +87,14 @@ func (k *Keybase) NewTeam(name string) Team {
} }
} }
// NewKV returns a new KV instance
func (k *Keybase) NewKV(team string) KV {
return KV{
keybase: k,
Team: team,
}
}
// NewWallet returns a new Wallet instance // NewWallet returns a new Wallet instance
func (k *Keybase) NewWallet() Wallet { func (k *Keybase) NewWallet() Wallet {
return Wallet{ return Wallet{

45
kvstore.go Normal file
View File

@ -0,0 +1,45 @@
package keybase
import (
"encoding/json"
"errors"
)
// kvAPIOut sends a JSON request to the kvstore API and returns its response.
func kvAPIOut(k *Keybase, kv KVAPI) (KVAPI, error) {
jsonBytes, _ := json.Marshal(kv)
cmdOut, err := k.Exec("kvstore", "api", "-m", string(jsonBytes))
if err != nil {
return KVAPI{}, err
}
var r KVAPI
if err := json.Unmarshal(cmdOut, &r); err != nil {
return KVAPI{}, err
}
if r.Error != nil {
return KVAPI{}, errors.New(r.Error.Message)
}
return r, nil
}
// Namespaces returns all namespaces for a team
func (kv KV) Namespaces() (KVAPI, error) {
m := KVAPI{
Params: &kvParams{},
}
m.Params.Options = kvOptions{
Team: kv.Team,
}
m.Method = "list"
r, err := kvAPIOut(kv.keybase, m)
if err != nil {
return r, err
}
return r, nil
}

View File

@ -646,6 +646,32 @@ type teamInfo struct {
Implicit implicit `json:"implicit,omitempty"` Implicit implicit `json:"implicit,omitempty"`
} }
// KVAPI holds information sent and received to/from the kvstore api
type KVAPI struct {
Method string `json:"method,omitempty"`
Params *kvParams `json:"params,omitempty"`
Result *kvResult `json:"result,omitempty"`
Error *Error `json:"error"`
keybase Keybase
}
type kvOptions struct {
Team string `json:"team,omitempty"`
Namespace string `json:"namespace,omitempty"`
EntryKey string `json:"entryKey,omitempty"`
Revision int `json:"revision,omitempty"`
EntryValue string `json:"entryValue,omitempty"`
}
type kvParams struct {
Options kvOptions `json:"options,omitempty"`
}
type kvResult struct {
TeamName string `json:"teamName"`
Namespaces []string `json:"namespaces"`
}
// UserAPI holds information received from the user/lookup api // UserAPI holds information received from the user/lookup api
type UserAPI struct { type UserAPI struct {
Status uStatus `json:"status"` Status uStatus `json:"status"`
@ -848,6 +874,16 @@ type wallet interface {
TxDetail(txid string) (WalletAPI, error) TxDetail(txid string) (WalletAPI, error)
} }
// KV holds basic information about a KVStore
type KV struct {
keybase *Keybase
Team string
}
type kvInterface interface {
Namespaces() (KVAPI, error)
}
type keybase interface { type keybase interface {
AdvertiseCommand(advertisement BotAdvertisement) (ChatAPI, error) AdvertiseCommand(advertisement BotAdvertisement) (ChatAPI, error)
AdvertiseCommands(advertisements []BotAdvertisement) (ChatAPI, error) AdvertiseCommands(advertisements []BotAdvertisement) (ChatAPI, error)
@ -856,6 +892,7 @@ type keybase interface {
CreateTeam(name string) (TeamAPI, error) CreateTeam(name string) (TeamAPI, error)
NewChat(channel Channel) Chat NewChat(channel Channel) Chat
NewTeam(name string) Team NewTeam(name string) Team
NewKV(team string) KV
NewWallet() Wallet NewWallet() Wallet
Run(handler func(ChatAPI), options ...RunOptions) Run(handler func(ChatAPI), options ...RunOptions)
status() status status() status