more rpc methods

This commit is contained in:
Alex Grintsvayg 2017-09-12 16:38:18 -04:00
parent 6e929495fc
commit 3b2d6f82bc
3 changed files with 262 additions and 32 deletions

View file

@ -1,6 +1,7 @@
package jsonrpc
import (
"encoding/json"
"errors"
"strconv"
@ -43,34 +44,167 @@ func decode(data interface{}, targetStruct interface{}) error {
return decoder.Decode(data)
}
func (d *Client) call(response interface{}, command string, params ...interface{}) error {
r, err := d.conn.Call(command, params...)
func (d *Client) callNoDecode(command string, params map[string]interface{}) (interface{}, error) {
r, err := d.conn.CallNamed(command, params)
if err != nil {
return err
return nil, err
}
if r.Error != nil {
return errors.New("Error in daemon: " + r.Error.Message)
return nil, errors.New("Error in daemon: " + r.Error.Message)
}
return decode(r.Result, response)
return r.Result, nil
}
func (d *Client) call(response interface{}, command string, params map[string]interface{}) error {
result, err := d.callNoDecode(command, params)
if err != nil {
return err
}
return decode(result, response)
}
func (d *Client) Commands() (*CommandsResponse, error) {
response := &CommandsResponse{}
return response, d.call(response, "commands")
response := new(CommandsResponse)
return response, d.call(response, "commands", map[string]interface{}{})
}
func (d *Client) Status() (*StatusResponse, error) {
response := &StatusResponse{}
return response, d.call(response, "status")
response := new(StatusResponse)
return response, d.call(response, "status", map[string]interface{}{})
}
func (d *Client) WalletBalance() (*WalletBalanceResponse, error) {
response := new(WalletBalanceResponse)
return response, d.call(response, "wallet_balance", map[string]interface{}{})
}
func (d *Client) Version() (*VersionResponse, error) {
response := new(VersionResponse)
return response, d.call(response, "version", map[string]interface{}{})
}
func (d *Client) Get(url string, filename *string, timeout *uint) (*GetResponse, error) {
response := &GetResponse{}
response := new(GetResponse)
return response, d.call(response, "get", map[string]interface{}{
"uri": url,
"file_name": filename,
"timeout": timeout,
})
}
func (d *Client) ClaimList(name string) (*ClaimListResponse, error) {
response := new(ClaimListResponse)
return response, d.call(response, "claim_list", map[string]interface{}{
"name": name,
})
}
func (d *Client) ClaimShow(claimID *string, txid *string, nout *uint) (*ClaimShowResponse, error) {
response := new(ClaimShowResponse)
return response, d.call(response, "claim_show", map[string]interface{}{
"claim_id": claimID,
"txid": txid,
"nout": nout,
})
}
func (d *Client) PeerList(blobHash string, timeout *uint) (*PeerListResponse, error) {
rawResponse, err := d.callNoDecode("peer_list", map[string]interface{}{
"blob_hash": blobHash,
"timeout": timeout,
})
if err != nil {
return nil, err
}
castResponse, ok := rawResponse.([]interface{})
if !ok {
return nil, errors.New("invalid peer_list response")
}
peers := []PeerListResponsePeer{}
for _, peer := range castResponse {
t, ok := peer.([]interface{})
if !ok {
return nil, errors.New("invalid peer_list response")
}
if len(t) != 3 {
return nil, errors.New("invalid triplet in peer_list response")
}
ip, ok := t[0].(string)
if !ok {
return nil, errors.New("invalid ip in peer_list response")
}
port, ok := t[1].(json.Number)
if !ok {
return nil, errors.New("invalid port in peer_list response")
}
available, ok := t[2].(bool)
if !ok {
return nil, errors.New("invalid is_available in peer_list response")
}
portNum, err := port.Int64()
if err != nil {
return nil, err
} else if portNum < 0 {
return nil, errors.New("invalid port in peer_list response")
}
peers = append(peers, PeerListResponsePeer{
IP: ip,
Port: uint(portNum),
IsAvailable: available,
})
}
response := PeerListResponse(peers)
return &response, nil
}
func (d *Client) BlobGet(blobHash string, encoding *string, timeout *uint) (*BlobGetResponse, error) {
rawResponse, err := d.callNoDecode("blob_get", map[string]interface{}{
"blob_hash": blobHash,
"timeout": timeout,
"encoding": encoding,
})
if err != nil {
return nil, err
}
if _, ok := rawResponse.(string); ok {
return nil, nil // blob was downloaded, nothing to return
}
response := new(BlobGetResponse)
return response, decode(rawResponse, response)
}
func (d *Client) StreamCostEstimate(url string, size *uint64) (*StreamCostEstimateResponse, error) {
response := new(StreamCostEstimateResponse)
return response, d.call(response, "stream_cost_estimate", map[string]interface{}{
"uri": url,
"size": size,
})
}
func (d *Client) FileList() (*FileListResponse, error) {
response := new(FileListResponse)
return response, d.call(response, "file_list", map[string]interface{}{})
}
func (d *Client) Resolve(url string) (*ResolveResponse, error) {
response := new(ResolveResponse)
return response, d.call(response, "resolve", map[string]interface{}{
"uri": url,
})
}
//func (d *Client) Publish() (*PublishResponse, error) {
// response := new(PublishResponse)
// return response, d.call(response, "publish")
//}

View file

@ -8,6 +8,59 @@ import (
lbryschema "github.com/lbryio/lbryschema.go/pb"
)
type Support struct {
Amount float64 `json:"amount"`
Nout int `json:"nout"`
Txid string `json:"txid"`
}
type Claim struct {
Address string `json:"address"`
Amount float64 `json:"amount"`
ClaimID string `json:"claim_id"`
ClaimSequence int `json:"claim_sequence"`
DecodedClaim bool `json:"decoded_claim"`
Depth int `json:"depth"`
EffectiveAmount float64 `json:"effective_amount"`
Height int `json:"height"`
Hex string `json:"hex"`
Name string `json:"name"`
Nout int `json:"nout"`
Supports []Support `json:"supports"`
Txid string `json:"txid"`
ValidAtHeight int `json:"valid_at_height"`
Value lbryschema.Claim `json:"value"`
Error *string `json:"error,omitempty"`
ChannelName *string `json:"channel_name,omitempty"`
HasSignature *bool `json:"has_signature,omitempty"`
SignatureIsValid *bool `json:"signature_is_valid,omitempty"`
}
type File struct {
ClaimID string `json:"claim_id"`
Completed bool `json:"completed"`
DownloadDirectory string `json:"download_directory"`
DownloadPath string `json:"download_path"`
FileName string `json:"file_name"`
Key string `json:"key"`
Message string `json:"message"`
Metadata *lbryschema.Claim `json:"metadata"`
MimeType string `json:"mime_type"`
Name string `json:"name"`
Outpoint string `json:"outpoint"`
PointsPaid float64 `json:"points_paid"`
SdHash string `json:"sd_hash"`
Stopped bool `json:"stopped"`
StreamHash string `json:"stream_hash"`
StreamName string `json:"stream_name"`
SuggestedFileName string `json:"suggested_file_name"`
TotalBytes uint64 `json:"total_bytes"`
WrittenBytes uint64 `json:"written_bytes"`
ChannelName *string `json:"channel_name,omitempty"`
HasSignature *bool `json:"has_signature,omitempty"`
SignatureIsValid *bool `json:"signature_is_valid,omitempty"`
}
func getEnumVal(enum map[string]int32, data interface{}) (int32, error) {
s, ok := data.(string)
if !ok {
@ -67,6 +120,18 @@ func fixDecodeProto(src, dest reflect.Type, data interface{}) (interface{}, erro
case reflect.TypeOf(lbryschema.Source_SourceTypes(0)):
val, err := getEnumVal(lbryschema.Source_SourceTypes_value, data)
return lbryschema.Source_SourceTypes(val), err
case reflect.TypeOf(lbryschema.KeyType(0)):
val, err := getEnumVal(lbryschema.KeyType_value, data)
return lbryschema.KeyType(val), err
case reflect.TypeOf(lbryschema.Signature_Version(0)):
val, err := getEnumVal(lbryschema.Signature_Version_value, data)
return lbryschema.Signature_Version(val), err
case reflect.TypeOf(lbryschema.Certificate_Version(0)):
val, err := getEnumVal(lbryschema.Certificate_Version_value, data)
return lbryschema.Certificate_Version(val), err
}
return data, nil
@ -74,6 +139,19 @@ func fixDecodeProto(src, dest reflect.Type, data interface{}) (interface{}, erro
type CommandsResponse []string
type WalletBalanceResponse float64
type VersionResponse struct {
Build string `json:"build"`
LbrynetVersion string `json:"lbrynet_version"`
LbryschemaVersion string `json:"lbryschema_version"`
LbryumVersion string `json:"lbryum_version"`
OsRelease string `json:"os_release"`
OsSystem string `json:"os_system"`
Platform string `json:"platform"`
Processor string `json:"processor"`
PythonVersion string `json:"python_version"`
}
type StatusResponse struct {
BlockchainStatus struct {
BestBlockhash string `json:"best_blockhash"`
@ -95,24 +173,43 @@ type StatusResponse struct {
} `json:"startup_status"`
}
type GetResponse struct {
ClaimID string `json:"claim_id"`
Completed bool `json:"completed"`
DownloadDirectory string `json:"download_directory"`
DownloadPath string `json:"download_path"`
FileName string `json:"file_name"`
Key string `json:"key"`
Message string `json:"message"`
Metadata *lbryschema.Claim `json:"metadata"`
MimeType string `json:"mime_type"`
Name string `json:"name"`
Outpoint string `json:"outpoint"`
PointsPaid float64 `json:"points_paid"`
SdHash string `json:"sd_hash"`
Stopped bool `json:"stopped"`
StreamHash string `json:"stream_hash"`
StreamName string `json:"stream_name"`
SuggestedFileName string `json:"suggested_file_name"`
TotalBytes uint64 `json:"total_bytes"`
WrittenBytes uint64 `json:"written_bytes"`
type ClaimListResponse struct {
Claims []Claim `json:"claims"`
LastTakeoverHeight int `json:"last_takeover_height"`
SupportsWithoutClaims []Support `json:"supports_without_claims"`
}
type ClaimShowResponse Claim
type PeerListResponsePeer struct {
IP string
Port uint
IsAvailable bool
}
type PeerListResponse []PeerListResponsePeer
type BlobGetResponse struct {
Blobs []struct {
BlobHash string `json:"blob_hash,omitempty"`
BlobNum int `json:"blob_num"`
IV string `json:"iv"`
Length int `json:"length"`
} `json:"blobs"`
Key string `json:"key"`
StreamHash string `json:"stream_hash"`
StreamName string `json:"stream_name"`
StreamType string `json:"stream_type"`
SuggestedFileName string `json:"suggested_file_name"`
}
type StreamCostEstimateResponse *float64
type GetResponse File
type FileListResponse []File
type ResolveResponse map[string]ResolveResponseItem
type ResolveResponseItem struct {
Certificate *Claim `json:"certificate,omitempty"`
Claim *Claim `json:"claim,omitempty"`
ClaimsInChannel *uint64 `json:"claims_in_channel,omitempty"`
}

View file

@ -16,10 +16,9 @@ func main() {
conn := jsonrpc.NewClient("")
response, err := conn.Get("one", nil, nil)
response, err := conn.Resolve("one")
if err != nil {
panic(err)
}
spew.Dump(response)
}