Update to ALMOST support 0.35 with new metadata

This commit is contained in:
Niko Storni 2019-04-03 00:44:30 +02:00
parent 41b4a3684a
commit 6f0d34f863
No known key found for this signature in database
GPG key ID: F37FE63398800368
4 changed files with 209 additions and 164 deletions

View file

@ -3,7 +3,7 @@ package claim
import ( import (
"bytes" "bytes"
types "github.com/lbryio/types/v1/go" types "github.com/lbryio/types/v2/go"
"github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"

View file

@ -10,9 +10,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/fatih/structs"
"github.com/lbryio/lbry.go/extras/errors" "github.com/lbryio/lbry.go/extras/errors"
"github.com/fatih/structs"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -180,55 +181,137 @@ func (d *Client) ChannelList(account *string, page uint64, pageSize uint64) (*Ch
}) })
} }
type Metadata struct { type streamType string
Fee *Fee `json:"fee,omitempty"`
var (
StreamTypeVideo = streamType("video")
StreamTypeAudio = streamType("audio")
StreamTypeImage = streamType("image")
)
type Locations struct {
Country *string `json:"country,omitempty"`
State *string `json:"state,omitempty"`
City *string `json:"city,omitempty"`
PostalCode *string `json:"code,omitempty"`
Latitude *string `json:"latitude,omitempty"`
Longitude *string `json:"longitude,omitempty"`
}
type ClaimCreateOptions struct {
Title string `json:"title"` Title string `json:"title"`
Description string `json:"description"` Description string `json:"description"`
Author string `json:"author"` Tags []string `json:"tags,omitempty"`
Language string `json:"language"` Languages []string `json:"language"`
License string `json:"license"` Locations *Locations `json:"locations,omitempty"`
LicenseURL *string `json:"license_url,omitempty"` ThumbnailURL *string `json:"thumbnail_url,omitempty"`
Thumbnail *string `json:"thumbnail,omitempty"`
Preview *string `json:"preview,omitempty"`
NSFW bool `json:"nsfw"`
}
type PublishOptions struct {
*Metadata `json:"metadata"`
ChannelName *string `json:"channel_name,omitempty"`
ChannelID *string `json:"channel_id,omitempty"`
ChannelAccountID *string `json:"channel_account_id,omitempty"`
AccountID *string `json:"account_id,omitempty"` AccountID *string `json:"account_id,omitempty"`
ClaimAddress *string `json:"claim_address,omitempty"` ClaimAddress *string `json:"claim_address,omitempty"`
ChangeAddress *string `json:"change_address,omitempty"` ChangeAddress *string `json:"change_address,omitempty"`
Preview *bool `json:"preview,omitempty"`
} }
func (d *Client) Publish(name, filePath string, bid float64, options PublishOptions) (*PublishResponse, error) { type ChannelCreateOptions struct {
*ClaimCreateOptions `json:",flatten"`
ContactEmail *string `json:"contact_email,omitempty"`
HomepageURL *string `json:"homepage_url,omitempty"`
CoverURL *string `json:"cover_url,omitempty"`
}
func (d *Client) ChannelCreate(name string, bid float64, options *ChannelCreateOptions) (*PublishResponse, error) {
response := new(PublishResponse) response := new(PublishResponse)
args := struct { args := struct {
Name string `json:"name"` Name string `json:"name"`
FilePath string `json:"file_path,omitempty"`
Bid string `json:"bid"` Bid string `json:"bid"`
*PublishOptions `json:",flatten"` FilePath string `json:"file_path,omitempty"`
*ChannelCreateOptions `json:",flatten"`
}{
Name: name,
Bid: fmt.Sprintf("%.6f", bid),
ChannelCreateOptions: options,
}
structs.DefaultTagName = "json"
return response, d.call(response, "channel_create", structs.Map(args))
}
type StreamCreateOptions struct {
*ClaimCreateOptions `json:",flatten"`
Fee *Fee `json:"fee,omitempty"`
Author *string `json:"author"`
License *string `json:"license"`
LicenseURL *string `json:"license_url,omitempty"`
StreamType *streamType `json:"stream_type,omitempty"`
ReleaseTime *int `json:"release_time,omitempty"`
Duration *int `json:"duration,omitempty"`
ImageWidth *int `json:"image_width,omitempty"`
ImageHeigth *int `json:"image_heigth,omitempty"`
VideoWidth *int `json:"video_width,omitempty"`
VideoHeight *int `json:"video_height,omitempty"`
Preview *string `json:"preview,omitempty"`
AllowDuplicateName *bool `json:"allow_duplicate_name,omitempty"`
ChannelName *string `json:"channel_name,omitempty"`
ChannelID *string `json:"channel_id,omitempty"`
ChannelAccountID *string `json:"channel_account_id,omitempty"`
}
func (d *Client) StreamCreate(name, filePath string, bid float64, options StreamCreateOptions) (*PublishResponse, error) {
response := new(PublishResponse)
args := struct {
Name string `json:"name"`
Bid string `json:"bid"`
FilePath string `json:"file_path,omitempty"`
*StreamCreateOptions `json:",flatten"`
}{ }{
Name: name, Name: name,
FilePath: filePath, FilePath: filePath,
Bid: fmt.Sprintf("%.6f", bid), Bid: fmt.Sprintf("%.6f", bid),
PublishOptions: &options, StreamCreateOptions: &options,
} }
structs.DefaultTagName = "json" structs.DefaultTagName = "json"
return response, d.call(response, "publish", structs.Map(args)) return response, d.call(response, "stream_create", structs.Map(args))
} }
func (d *Client) ChannelNew(name string, amount float64, accountID *string) (*ChannelNewResponse, error) { func (d *Client) StreamAbandon(txID string, nOut uint64, accountID *string, blocking bool) (*ClaimAbandonResponse, error) {
response := new(ChannelNewResponse) response := new(ClaimAbandonResponse)
return response, d.call(response, "channel_new", map[string]interface{}{ err := d.call(response, "claim_abandon", map[string]interface{}{
"channel_name": name, "txid": txID,
"amount": fmt.Sprintf("%.6f", amount), "nout": nOut,
"account_id": accountID, "account_id": accountID,
}) })
if err != nil {
return nil, err
} else if response == nil {
return nil, errors.Err("no response")
}
return response, nil
} }
func (d *Client) ClaimAbandon(txID string, nOut uint64, accountID *string, blocking bool) (*ClaimAbandonResponse, error) { type StreamUpdateOptions struct {
ClearTags *bool `json:"clear_tags,omitempty"`
ClearLanguages *bool `json:"clear_languages,omitempty"`
ClearLocations *bool `json:"clear_locations,omitempty"`
Name *string `json:"name"`
FilePath *string `json:"file_path,omitempty"`
Bid *string `json:"bid"`
*StreamCreateOptions `json:",flatten"`
}
func (d *Client) StreamUpdate(claimID string, options StreamUpdateOptions) (*PublishResponse, error) {
response := new(PublishResponse)
args := struct {
ClaimID string `json:"claim_id"`
FilePath string `json:"file_path,omitempty"`
Bid string `json:"bid"`
*StreamUpdateOptions `json:",flatten"`
}{
ClaimID: claimID,
StreamUpdateOptions: &options,
}
structs.DefaultTagName = "json"
return response, d.call(response, "stream_create", structs.Map(args))
}
func (d *Client) ChannelAbandon(txID string, nOut uint64, accountID *string, blocking bool) (*ClaimAbandonResponse, error) {
response := new(ClaimAbandonResponse) response := new(ClaimAbandonResponse)
err := d.call(response, "claim_abandon", map[string]interface{}{ err := d.call(response, "claim_abandon", map[string]interface{}{
"txid": txID, "txid": txID,
@ -251,19 +334,12 @@ func (d *Client) AddressList(account *string) (*AddressListResponse, error) {
}) })
} }
func (d *Client) ClaimList(name string) (*ClaimListResponse, error) { func (d *Client) ClaimList(account *string, page uint64, pageSize uint64) (*ClaimListMineResponse, error) {
response := new(ClaimListResponse)
return response, d.call(response, "claim_list", map[string]interface{}{
"name": name,
})
}
func (d *Client) ClaimListMine(account *string, page uint64, pageSize uint64) (*ClaimListMineResponse, error) {
if page == 0 { if page == 0 {
return nil, errors.Err("pages start from 1") return nil, errors.Err("pages start from 1")
} }
response := new(ClaimListMineResponse) response := new(ClaimListMineResponse)
err := d.call(response, "claim_list_mine", map[string]interface{}{ err := d.call(response, "claim_list", map[string]interface{}{
"account_id": account, "account_id": account,
"page": page, "page": page,
"page_size": pageSize, "page_size": pageSize,
@ -301,10 +377,12 @@ func (d *Client) Resolve(urls string) (*ResolveResponse, error) {
}) })
} }
func (d *Client) NumClaimsInChannel(uri string) (uint64, error) { /*
// use resolve?
func (d *Client) NumClaimsInChannel(channelClaimID string) (uint64, error) {
response := new(NumClaimsInChannelResponse) response := new(NumClaimsInChannelResponse)
err := d.call(response, "claim_list_by_channel", map[string]interface{}{ err := d.call(response, "claim_search", map[string]interface{}{
"uri": uri, "channel_id": channelClaimID,
}) })
if err != nil { if err != nil {
return 0, err return 0, err
@ -324,12 +402,13 @@ func (d *Client) NumClaimsInChannel(uri string) (uint64, error) {
} }
return *channel.ClaimsInChannel, nil return *channel.ClaimsInChannel, nil
} }
*/
func (d *Client) ClaimShow(claimID *string, txid *string, nout *uint) (*ClaimShowResponse, error) { func (d *Client) ClaimSearch(claimName, claimID, txid *string, nout *uint) (*ClaimSearchResponse, error) {
response := new(ClaimShowResponse) response := new(ClaimSearchResponse)
return response, d.call(response, "claim_show", map[string]interface{}{ return response, d.call(response, "claim_search", map[string]interface{}{
"claim_id": claimID, "claim_id": claimID,
"txid": txid, "txid": txid,
"nout": nout, "nout": nout,
"name": claimName,
}) })
} }

View file

@ -7,8 +7,9 @@ import (
"testing" "testing"
"time" "time"
"github.com/lbryio/lbry.go/extras/util"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
"github.com/lbryio/lbry.go/extras/util"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -55,29 +56,46 @@ func TestClient_Publish(t *testing.T) {
t.Error(err) t.Error(err)
} }
address := string(*addressResponse) address := string(*addressResponse)
got, err := d.Publish("test", "/home/niko/work/allClaims.txt", 14.37, PublishOptions{ got, err := d.StreamCreate("test"+string(time.Now().Unix()), "/home/niko/work/allClaims.txt", 14.37, StreamCreateOptions{
Metadata: &Metadata{ ClaimCreateOptions: &ClaimCreateOptions{
Title: "This is a Test Title" + time.Now().String(),
Description: "My Special Description",
Tags: []string{"nsfw", "test"},
Languages: []string{"en-US", "fr-CH"},
Locations: &Locations{
Country: util.PtrToString("CH"),
State: util.PtrToString("Ticino"),
City: util.PtrToString("Lugano"),
PostalCode: util.PtrToString("6900"),
Latitude: nil,
Longitude: nil,
},
ThumbnailURL: util.PtrToString("https://scrn.storni.info/2019-01-18_16-37-39-098537783.png"),
AccountID: nil,
ClaimAddress: &address,
ChangeAddress: &address,
Preview: nil,
},
Fee: &Fee{ Fee: &Fee{
Currency: "LBC", Currency: "LBC",
Amount: decimal.NewFromFloat(1.0), Amount: decimal.NewFromFloat(1.0),
Address: &address, Address: &address,
}, },
Title: "This is a Test Title", Author: util.PtrToString("Niko"),
Description: "My Special Description", License: util.PtrToString("FREE"),
Author: "Niko",
Language: "en",
License: "FREEEEE",
LicenseURL: nil, LicenseURL: nil,
Thumbnail: util.PtrToString("https://scrn.storni.info/2019-01-18_16-37-39-098537783.png"), StreamType: &StreamTypeImage,
ReleaseTime: nil,
Duration: nil,
ImageWidth: nil,
ImageHeigth: nil,
VideoWidth: nil,
VideoHeight: nil,
Preview: nil, Preview: nil,
NSFW: false, AllowDuplicateName: nil,
},
ChannelName: nil, ChannelName: nil,
ChannelID: util.PtrToString("bda0520bff61e4a70c966d7298e6b89107cf8bed"), ChannelID: util.PtrToString("bda0520bff61e4a70c966d7298e6b89107cf8bed"),
ChannelAccountID: nil, ChannelAccountID: nil,
AccountID: nil,
ClaimAddress: &address,
ChangeAddress: &address,
}) })
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -85,25 +103,25 @@ func TestClient_Publish(t *testing.T) {
log.Infof("%+v", *got) log.Infof("%+v", *got)
} }
func TestClient_ChannelNew(t *testing.T) { func TestClient_ChannelCreate(t *testing.T) {
d := NewClient("") d := NewClient("")
got, err := d.ChannelNew("@Test", 13.37, nil) got, err := d.ChannelCreate("@Test", 13.37, nil)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
log.Infof("%+v", *got) log.Infof("%+v", *got)
} }
func TestClient_ClaimAbandon(t *testing.T) { func TestClient_ChannelAbandon(t *testing.T) {
d := NewClient("") d := NewClient("")
channelResponse, err := d.ChannelNew("@TestToDelete", 13.37, nil) channelResponse, err := d.ChannelCreate("@TestToDelete", 13.37, nil)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
txID := channelResponse.Output.Txid txID := channelResponse.Output.Txid
nout := channelResponse.Output.Nout nout := channelResponse.Output.Nout
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
got, err := d.ClaimAbandon(txID, nout, nil, false) got, err := d.ChannelAbandon(txID, nout, nil, false)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -121,16 +139,16 @@ func TestClient_AddressList(t *testing.T) {
func TestClient_ClaimList(t *testing.T) { func TestClient_ClaimList(t *testing.T) {
d := NewClient("") d := NewClient("")
got, err := d.ClaimList("test") got, err := d.ClaimList(nil, 1, 10)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
log.Infof("%+v", *got) log.Infof("%+v", *got)
} }
func TestClient_ClaimListMine(t *testing.T) { func TestClient_ClaimSearch(t *testing.T) {
d := NewClient("") d := NewClient("")
got, err := d.ClaimListMine(nil, 1, 50) got, err := d.ClaimSearch(nil, util.PtrToString("4742f25e6d51b4b0483d5b8cd82e3ea121dacde9"), nil, nil)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -177,24 +195,6 @@ func TestClient_Resolve(t *testing.T) {
log.Infof("%s", b) log.Infof("%s", b)
} }
func TestClient_NumClaimsInChannel(t *testing.T) {
d := NewClient("")
got, err := d.NumClaimsInChannel("@Test#bda0520bff61e4a70c966d7298e6b89107cf8bed")
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)
}
log.Infof("%+v", *got)
}
func TestClient_AccountFund(t *testing.T) { func TestClient_AccountFund(t *testing.T) {
d := NewClient("") d := NewClient("")
accounts, err := d.AccountList() accounts, err := d.AccountList()

View file

@ -5,7 +5,7 @@ import (
"reflect" "reflect"
"github.com/lbryio/lbry.go/extras/errors" "github.com/lbryio/lbry.go/extras/errors"
lbryschema "github.com/lbryio/types/v1/go" lbryschema "github.com/lbryio/types/v2/go"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
) )
@ -93,49 +93,13 @@ func fixDecodeProto(src, dest reflect.Type, data interface{}) (interface{}, erro
return d, nil return d, nil
} }
case reflect.TypeOf(lbryschema.Metadata_Version(0)):
val, err := getEnumVal(lbryschema.Metadata_Version_value, data)
return lbryschema.Metadata_Version(val), err
case reflect.TypeOf(lbryschema.Metadata_Language(0)):
val, err := getEnumVal(lbryschema.Metadata_Language_value, data)
return lbryschema.Metadata_Language(val), err
case reflect.TypeOf(lbryschema.Stream_Version(0)):
val, err := getEnumVal(lbryschema.Stream_Version_value, data)
return lbryschema.Stream_Version(val), err
case reflect.TypeOf(lbryschema.Claim_Version(0)):
val, err := getEnumVal(lbryschema.Claim_Version_value, data)
return lbryschema.Claim_Version(val), err
case reflect.TypeOf(lbryschema.Claim_ClaimType(0)):
val, err := getEnumVal(lbryschema.Claim_ClaimType_value, data)
return lbryschema.Claim_ClaimType(val), err
case reflect.TypeOf(lbryschema.Fee_Version(0)):
val, err := getEnumVal(lbryschema.Fee_Version_value, data)
return lbryschema.Fee_Version(val), err
case reflect.TypeOf(lbryschema.Fee_Currency(0)): case reflect.TypeOf(lbryschema.Fee_Currency(0)):
val, err := getEnumVal(lbryschema.Fee_Currency_value, data) val, err := getEnumVal(lbryschema.Fee_Currency_value, data)
return lbryschema.Fee_Currency(val), err return lbryschema.Fee_Currency(val), err
case reflect.TypeOf(lbryschema.Source_Version(0)): case reflect.TypeOf(lbryschema.Claim_Type_name):
val, err := getEnumVal(lbryschema.Source_Version_value, data) val, err := getEnumVal(lbryschema.Claim_Type_value, data)
return lbryschema.Source_Version(val), err return lbryschema.Claim_Type_name[val], err
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 return data, nil
@ -308,21 +272,22 @@ type Claim struct {
Height int `json:"height"` Height int `json:"height"`
Hex string `json:"hex"` Hex string `json:"hex"`
Name string `json:"name"` Name string `json:"name"`
NormalizedName string `json:"normalized_name"`
Nout uint64 `json:"nout"` Nout uint64 `json:"nout"`
PermanentUrl string `json:"permanent_url"` PermanentUrl string `json:"permanent_url"`
SignatureIsValid *bool `json:"signature_is_valid,omitempty"` SignatureIsValid *bool `json:"signature_is_valid,omitempty"`
Supports []Support `json:"supports"` Supports []Support `json:"supports"`
Txid string `json:"txid"` Txid string `json:"txid"`
Type string `json:"type"` //Type string `json:"type"`
ValidAtHeight int `json:"valid_at_height"` ValidAtHeight int `json:"valid_at_height"`
Value lbryschema.Claim `json:"value"` Value lbryschema.Claim `json:"value"`
} }
type ClaimListResponse struct { type ClaimListResponse []Claim /* {
Claims []Claim `json:"claims"` Claims []Claim `json:"claims"`
LastTakeoverHeight int `json:"last_takeover_height"` LastTakeoverHeight int `json:"last_takeover_height"`
SupportsWithoutClaims []Support `json:"supports_without_claims"` SupportsWithoutClaims []Support `json:"supports_without_claims"`
} }*/
type ClaimListMineResponse struct { type ClaimListMineResponse struct {
Claims []Claim `json:"items"` Claims []Claim `json:"items"`
@ -330,6 +295,7 @@ type ClaimListMineResponse struct {
PageSize uint64 `json:"page_size"` PageSize uint64 `json:"page_size"`
TotalPages uint64 `json:"total_pages"` TotalPages uint64 `json:"total_pages"`
} }
type ClaimSearchResponse ClaimListMineResponse
type StatusResponse struct { type StatusResponse struct {
BlobManager struct { BlobManager struct {