ultimate needed SDK calls update

removed useless pagination

add type to claim signature
This commit is contained in:
Niko Storni 2019-01-22 18:48:04 +01:00
parent 2437a06505
commit ec0eec8ac4
No known key found for this signature in database
GPG key ID: F37FE63398800368
3 changed files with 161 additions and 303 deletions

View file

@ -42,7 +42,7 @@ func NewClient(address string) *Client {
func NewClientAndWait(address string) *Client {
d := NewClient(address)
for {
_, err := d.WalletBalance()
_, err := d.AccountBalance(nil)
if err == nil {
return d
}
@ -135,259 +135,6 @@ func (d *Client) SetRPCTimeout(timeout time.Duration) {
})
}
func (d *Client) Commands() (*CommandsResponse, error) {
response := new(CommandsResponse)
return response, d.call(response, "commands", map[string]interface{}{})
}
func (d *Client) WalletBalance() (*WalletBalanceResponse, error) {
rawResponse, err := d.callNoDecode("wallet_balance", map[string]interface{}{})
if err != nil {
return nil, err
}
dec, err := decodeNumber(rawResponse)
if err != nil {
return nil, err
}
response := WalletBalanceResponse(dec)
return &response, nil
}
func (d *Client) WalletList() (*WalletListResponse, error) {
response := new(WalletListResponse)
return response, d.call(response, "wallet_list", 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 := new(GetResponse)
return response, d.call(response, "get", map[string]interface{}{
"uri": url,
"file_name": filename,
"timeout": timeout,
})
}
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.Err("invalid peer_list response")
}
peers := []PeerListResponsePeer{}
for _, peer := range castResponse {
t, ok := peer.(map[string]interface{})
if !ok {
return nil, errors.Err("invalid peer_list response")
}
if len(t) != 3 {
return nil, errors.Err("invalid triplet in peer_list response")
}
ip, ok := t["host"].(string)
if !ok {
return nil, errors.Err("invalid ip in peer_list response")
}
port, ok := t["port"].(json.Number)
if !ok {
return nil, errors.Err("invalid port in peer_list response")
}
nodeid, ok := t["node_id"].(string)
if !ok {
return nil, errors.Err("invalid nodeid in peer_list response")
}
portNum, err := port.Int64()
if err != nil {
return nil, errors.Wrap(err, 0)
} else if portNum < 0 {
return nil, errors.Err("invalid port in peer_list response")
}
peers = append(peers, PeerListResponsePeer{
IP: ip,
Port: uint(portNum),
NodeId: nodeid,
})
}
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) StreamAvailability(url string, search_timeout *uint64, blob_timeout *uint64) (*StreamAvailabilityResponse, error) {
response := new(StreamAvailabilityResponse)
return response, d.call(response, "stream_availability", map[string]interface{}{
"uri": url,
"search_timeout": search_timeout,
"blob_timeout": blob_timeout,
})
}
func (d *Client) StreamCostEstimate(url string, size *uint64) (*StreamCostEstimateResponse, error) {
rawResponse, err := d.callNoDecode("stream_cost_estimate", map[string]interface{}{
"uri": url,
"size": size,
})
if err != nil {
return nil, err
}
dec, err := decodeNumber(rawResponse)
if err != nil {
return nil, err
}
response := StreamCostEstimateResponse(dec)
return &response, nil
}
type FileListOptions struct {
SDHash *string
StreamHash *string
FileName *string
ClaimID *string
Outpoint *string
RowID *string
Name *string
}
func (d *Client) FileList(options FileListOptions) (*FileListResponse, error) {
response := new(FileListResponse)
return response, d.call(response, "file_list", map[string]interface{}{
"sd_hash": options.SDHash,
"stream_hash": options.StreamHash,
"file_name": options.FileName,
"claim_id": options.ClaimID,
"outpoint": options.Outpoint,
"rowid": options.RowID,
"name": options.Name,
})
}
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) BlobAnnounce(blobHash, sdHash, streamHash *string) (*BlobAnnounceResponse, error) {
response := new(BlobAnnounceResponse)
return response, d.call(response, "blob_announce", map[string]interface{}{
"blob_hash": blobHash,
"stream_hash": streamHash,
"sd_hash": sdHash,
})
}
func (d *Client) WalletPrefillAddresses(numAddresses int, amount decimal.Decimal, broadcast bool) (*WalletPrefillAddressesResponse, error) {
if numAddresses < 1 {
return nil, errors.Err("must create at least 1 address")
}
response := new(WalletPrefillAddressesResponse)
return response, d.call(response, "wallet_prefill_addresses", map[string]interface{}{
"num_addresses": numAddresses,
"amount": amount,
"no_broadcast": !broadcast,
})
}
func (d *Client) WalletNewAddress() (*WalletNewAddressResponse, error) {
rawResponse, err := d.callNoDecode("wallet_new_address", map[string]interface{}{})
if err != nil {
return nil, err
}
address, ok := rawResponse.(string)
if !ok {
return nil, errors.Err("unexpected response")
}
response := WalletNewAddressResponse(address)
return &response, nil
}
func (d *Client) WalletUnusedAddress() (*WalletUnusedAddressResponse, error) {
rawResponse, err := d.callNoDecode("wallet_unused_address", map[string]interface{}{})
if err != nil {
return nil, err
}
address, ok := rawResponse.(string)
if !ok {
return nil, errors.Err("unexpected response")
}
response := WalletUnusedAddressResponse(address)
return &response, nil
}
func (d *Client) NumClaimsInChannel(url string) (uint64, error) {
response := new(NumClaimsInChannelResponse)
err := d.call(response, "claim_list_by_channel", map[string]interface{}{
"uri": url,
})
if err != nil {
return 0, err
} else if response == nil {
return 0, errors.Err("no response")
}
channel, ok := (*response)[url]
if !ok {
return 0, errors.Err("url not in response")
}
if channel.Error != "" {
if strings.Contains(channel.Error, "cannot be resolved") {
return 0, nil
}
return 0, errors.Err(channel.Error)
}
return channel.ClaimsInChannel, nil
}
//============================================
// NEW SDK
//============================================
@ -421,6 +168,9 @@ func (d *Client) AddressUnused(account *string) (*AddressUnusedResponse, error)
}
func (d *Client) ChannelList(account *string, page uint64, pageSize uint64) (*ChannelListResponse, error) {
if page == 0 {
return nil, errors.Err("pages start from 1")
}
response := new(ChannelListResponse)
return response, d.call(response, "channel_list", map[string]interface{}{
"account_id": account,
@ -511,6 +261,9 @@ func (d *Client) ClaimList(name string) (*ClaimListResponse, error) {
}
func (d *Client) ClaimListMine(account *string, page uint64, pageSize uint64) (*ClaimListMineResponse, error) {
if page == 0 {
return nil, errors.Err("pages start from 1")
}
response := new(ClaimListMineResponse)
err := d.call(response, "claim_list_mine", map[string]interface{}{
"account_id": account,
@ -531,11 +284,59 @@ func (d *Client) Status() (*StatusResponse, error) {
return response, d.call(response, "status", map[string]interface{}{})
}
func (d *Client) UTXOList(account *string, page uint64, pageSize uint64) (*UTXOListResponse, error) {
func (d *Client) UTXOList(account *string) (*UTXOListResponse, error) {
response := new(UTXOListResponse)
return response, d.call(response, "utxo_list", map[string]interface{}{
"account_id": account,
"page": page,
"page_size": pageSize,
})
}
func (d *Client) Version() (*VersionResponse, error) {
response := new(VersionResponse)
return response, d.call(response, "version", map[string]interface{}{})
}
func (d *Client) Commands() (*CommandsResponse, error) {
response := new(CommandsResponse)
return response, d.call(response, "commands", map[string]interface{}{})
}
func (d *Client) Resolve(uri string) (*ResolveResponse, error) {
response := new(ResolveResponse)
return response, d.call(response, "resolve", map[string]interface{}{
"uri": uri,
})
}
func (d *Client) NumClaimsInChannel(uri string) (uint64, error) {
response := new(NumClaimsInChannelResponse)
err := d.call(response, "claim_list_by_channel", map[string]interface{}{
"uri": uri,
})
if err != nil {
return 0, err
} else if response == nil {
return 0, errors.Err("no response")
}
channel, ok := (*response)[uri]
if !ok {
return 0, errors.Err("url not in response")
}
if channel.Error != nil {
if strings.Contains(*channel.Error, "cannot be resolved") {
return 0, nil
}
return 0, errors.Err(*channel.Error)
}
return *channel.ClaimsInChannel, nil
}
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,
})
}

View file

@ -1,6 +1,7 @@
package jsonrpc
import (
"encoding/json"
"fmt"
"strconv"
"testing"
@ -152,7 +153,7 @@ func TestClient_ClaimList(t *testing.T) {
func TestClient_ClaimListMine(t *testing.T) {
d := NewClient("")
got, err := d.ClaimListMine(nil, 0, 50)
got, err := d.ClaimListMine(nil, 1, 50)
if err != nil {
t.Error(err)
}
@ -170,7 +171,56 @@ func TestClient_Status(t *testing.T) {
func TestClient_UTXOList(t *testing.T) {
d := NewClient("")
got, err := d.UTXOList(nil, 0, 50)
got, err := d.UTXOList(nil)
if err != nil {
t.Error(err)
}
log.Infof("%+v", *got)
}
func TestClient_Version(t *testing.T) {
d := NewClient("")
got, err := d.Version()
if err != nil {
t.Error(err)
}
log.Infof("%+v", *got)
}
func TestClient_Commands(t *testing.T) {
d := NewClient("")
got, err := d.Commands()
if err != nil {
t.Error(err)
}
log.Infof("%+v", *got)
}
func TestClient_Resolve(t *testing.T) {
d := NewClient("")
got, err := d.Resolve("test")
if err != nil {
t.Error(err)
}
b, err := json.Marshal(*got)
if err != nil {
t.Error(err)
}
log.Infof("%s", b)
}
func TestClient_NumClaimsInChannel(t *testing.T) {
d := NewClient("")
got, err := d.NumClaimsInChannel("@Test#0a32af305113435d1cdf4ec61452b9a6dcb74da8")
if err != nil {
t.Error(err)
}
log.Infof("%d", got)
}
func TestClient_ClaimShow(t *testing.T) {
d := NewClient("")
got, err := d.ClaimShow(util.PtrToString("4742f25e6d51b4b0483d5b8cd82e3ea121dacde9"), nil, nil)
if err != nil {
t.Error(err)
}

View file

@ -141,24 +141,8 @@ func fixDecodeProto(src, dest reflect.Type, data interface{}) (interface{}, erro
return data, nil
}
type CommandsResponse []string
type WalletBalanceResponse decimal.Decimal
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 ClaimShowResponse Claim
type PeerListResponsePeer struct {
IP string `json:"host"`
Port uint `json:"port"`
@ -206,14 +190,6 @@ type StreamAvailabilityResponse struct {
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"`
Error *string `json:"error,omitempty"`
}
type WalletListResponse []string
type BlobAnnounceResponse bool
@ -228,11 +204,6 @@ type WalletNewAddressResponse string
type WalletUnusedAddressResponse string
type NumClaimsInChannelResponse map[string]struct {
ClaimsInChannel uint64 `json:"claims_in_channel,omitempty"`
Error string `json:"error,omitempty"`
}
//============================================
// NEW SDK
//============================================
@ -342,6 +313,7 @@ type Claim struct {
SignatureIsValid *bool `json:"signature_is_valid,omitempty"`
Supports []Support `json:"supports"`
Txid string `json:"txid"`
Type string `json:"type"`
ValidAtHeight int `json:"valid_at_height"`
Value lbryschema.Claim `json:"value"`
}
@ -412,19 +384,54 @@ type StatusResponse struct {
} `json:"wallet"`
}
type UTXOListResponse struct {
UTXOs []struct {
Address string `json:"address"`
Amount string `json:"amount"`
Height int `json:"height"`
IsClaim bool `json:"is_claim"`
IsCoinbase bool `json:"is_coinbase"`
IsSupport bool `json:"is_support"`
IsUpdate bool `json:"is_update"`
Nout int `json:"nout"`
Txid string `json:"txid"`
} `json:"items"`
Page uint64 `json:"page"`
PageSize uint64 `json:"page_size"`
TotalPages uint64 `json:"total_pages"`
type UTXOListResponse []struct {
Address string `json:"address"`
Amount string `json:"amount"`
Height int `json:"height"`
IsClaim bool `json:"is_claim"`
IsCoinbase bool `json:"is_coinbase"`
IsSupport bool `json:"is_support"`
IsUpdate bool `json:"is_update"`
Nout int `json:"nout"`
Txid string `json:"txid"`
}
type VersionResponse struct {
Build string `json:"build"`
Desktop string `json:"desktop"`
Distro struct {
Codename string `json:"codename"`
ID string `json:"id"`
Like string `json:"like"`
Version string `json:"version"`
VersionParts struct {
BuildNumber string `json:"build_number"`
Major string `json:"major"`
Minor string `json:"minor"`
} `json:"version_parts"`
} `json:"distro"`
LbrynetVersion string `json:"lbrynet_version"`
LbryschemaVersion string `json:"lbryschema_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 CommandsResponse []string
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"`
Error *string `json:"error,omitempty"`
}
type NumClaimsInChannelResponse map[string]struct {
ClaimsInChannel *uint64 `json:"claims_in_channel,omitempty"`
Error *string `json:"error,omitempty"`
}
type ClaimShowResponse *Claim