more rpc methods
This commit is contained in:
parent
6e929495fc
commit
3b2d6f82bc
|
@ -1,6 +1,7 @@
|
||||||
package jsonrpc
|
package jsonrpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -43,34 +44,167 @@ func decode(data interface{}, targetStruct interface{}) error {
|
||||||
return decoder.Decode(data)
|
return decoder.Decode(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Client) call(response interface{}, command string, params ...interface{}) error {
|
func (d *Client) callNoDecode(command string, params map[string]interface{}) (interface{}, error) {
|
||||||
r, err := d.conn.Call(command, params...)
|
r, err := d.conn.CallNamed(command, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Error != nil {
|
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) {
|
func (d *Client) Commands() (*CommandsResponse, error) {
|
||||||
response := &CommandsResponse{}
|
response := new(CommandsResponse)
|
||||||
return response, d.call(response, "commands")
|
return response, d.call(response, "commands", map[string]interface{}{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Client) Status() (*StatusResponse, error) {
|
func (d *Client) Status() (*StatusResponse, error) {
|
||||||
response := &StatusResponse{}
|
response := new(StatusResponse)
|
||||||
return response, d.call(response, "status")
|
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) {
|
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{}{
|
return response, d.call(response, "get", map[string]interface{}{
|
||||||
"uri": url,
|
"uri": url,
|
||||||
"file_name": filename,
|
"file_name": filename,
|
||||||
"timeout": timeout,
|
"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")
|
||||||
|
//}
|
||||||
|
|
|
@ -8,6 +8,59 @@ import (
|
||||||
lbryschema "github.com/lbryio/lbryschema.go/pb"
|
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) {
|
func getEnumVal(enum map[string]int32, data interface{}) (int32, error) {
|
||||||
s, ok := data.(string)
|
s, ok := data.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -67,6 +120,18 @@ func fixDecodeProto(src, dest reflect.Type, data interface{}) (interface{}, erro
|
||||||
case reflect.TypeOf(lbryschema.Source_SourceTypes(0)):
|
case reflect.TypeOf(lbryschema.Source_SourceTypes(0)):
|
||||||
val, err := getEnumVal(lbryschema.Source_SourceTypes_value, data)
|
val, err := getEnumVal(lbryschema.Source_SourceTypes_value, data)
|
||||||
return lbryschema.Source_SourceTypes(val), err
|
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
|
return data, nil
|
||||||
|
@ -74,6 +139,19 @@ func fixDecodeProto(src, dest reflect.Type, data interface{}) (interface{}, erro
|
||||||
|
|
||||||
type CommandsResponse []string
|
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 {
|
type StatusResponse struct {
|
||||||
BlockchainStatus struct {
|
BlockchainStatus struct {
|
||||||
BestBlockhash string `json:"best_blockhash"`
|
BestBlockhash string `json:"best_blockhash"`
|
||||||
|
@ -95,24 +173,43 @@ type StatusResponse struct {
|
||||||
} `json:"startup_status"`
|
} `json:"startup_status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetResponse struct {
|
type ClaimListResponse struct {
|
||||||
ClaimID string `json:"claim_id"`
|
Claims []Claim `json:"claims"`
|
||||||
Completed bool `json:"completed"`
|
LastTakeoverHeight int `json:"last_takeover_height"`
|
||||||
DownloadDirectory string `json:"download_directory"`
|
SupportsWithoutClaims []Support `json:"supports_without_claims"`
|
||||||
DownloadPath string `json:"download_path"`
|
}
|
||||||
FileName string `json:"file_name"`
|
|
||||||
Key string `json:"key"`
|
type ClaimShowResponse Claim
|
||||||
Message string `json:"message"`
|
|
||||||
Metadata *lbryschema.Claim `json:"metadata"`
|
type PeerListResponsePeer struct {
|
||||||
MimeType string `json:"mime_type"`
|
IP string
|
||||||
Name string `json:"name"`
|
Port uint
|
||||||
Outpoint string `json:"outpoint"`
|
IsAvailable bool
|
||||||
PointsPaid float64 `json:"points_paid"`
|
}
|
||||||
SdHash string `json:"sd_hash"`
|
type PeerListResponse []PeerListResponsePeer
|
||||||
Stopped bool `json:"stopped"`
|
|
||||||
StreamHash string `json:"stream_hash"`
|
type BlobGetResponse struct {
|
||||||
StreamName string `json:"stream_name"`
|
Blobs []struct {
|
||||||
SuggestedFileName string `json:"suggested_file_name"`
|
BlobHash string `json:"blob_hash,omitempty"`
|
||||||
TotalBytes uint64 `json:"total_bytes"`
|
BlobNum int `json:"blob_num"`
|
||||||
WrittenBytes uint64 `json:"written_bytes"`
|
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"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue