fix(payments): Improve paged response (#8)
Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
6445f3f292
commit
d01fefbfbf
1 changed files with 23 additions and 10 deletions
33
client.go
33
client.go
|
@ -87,7 +87,8 @@ func (f *FlexpoolClient) MinerBalance(coin string, address string) (float64, err
|
||||||
type PaymentsResponse struct {
|
type PaymentsResponse struct {
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
Result struct {
|
Result struct {
|
||||||
Data []struct {
|
TotalPages int `json:"totalPages"`
|
||||||
|
Data []struct {
|
||||||
Hash string `json:"hash"`
|
Hash string `json:"hash"`
|
||||||
Value float64 `json:"value"`
|
Value float64 `json:"value"`
|
||||||
Timestamp int64 `json:"timestamp"`
|
Timestamp int64 `json:"timestamp"`
|
||||||
|
@ -98,7 +99,9 @@ type PaymentsResponse struct {
|
||||||
// MinerPayments returns an ordered list of payments
|
// MinerPayments returns an ordered list of payments
|
||||||
func (f *FlexpoolClient) MinerPayments(coin string, address string, limit int) (payments []*Payment, err error) {
|
func (f *FlexpoolClient) MinerPayments(coin string, address string, limit int) (payments []*Payment, err error) {
|
||||||
page := 0
|
page := 0
|
||||||
for {
|
totalPages := 0
|
||||||
|
|
||||||
|
for page <= MaxIterations && len(payments) < limit {
|
||||||
body, err := f.request(fmt.Sprintf("%s/miner/payments/?coin=%s&address=%s&page=%d", FlexpoolAPIURL, coin, address, page))
|
body, err := f.request(fmt.Sprintf("%s/miner/payments/?coin=%s&address=%s&page=%d", FlexpoolAPIURL, coin, address, page))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -107,6 +110,10 @@ func (f *FlexpoolClient) MinerPayments(coin string, address string, limit int) (
|
||||||
var response PaymentsResponse
|
var response PaymentsResponse
|
||||||
json.Unmarshal(body, &response)
|
json.Unmarshal(body, &response)
|
||||||
|
|
||||||
|
if totalPages == 0 {
|
||||||
|
totalPages = response.Result.TotalPages
|
||||||
|
}
|
||||||
|
|
||||||
for _, result := range response.Result.Data {
|
for _, result := range response.Result.Data {
|
||||||
payment := NewPayment(
|
payment := NewPayment(
|
||||||
result.Hash,
|
result.Hash,
|
||||||
|
@ -115,18 +122,24 @@ func (f *FlexpoolClient) MinerPayments(coin string, address string, limit int) (
|
||||||
)
|
)
|
||||||
payments = append(payments, payment)
|
payments = append(payments, payment)
|
||||||
if len(payments) >= limit {
|
if len(payments) >= limit {
|
||||||
// sort by timestamp
|
break
|
||||||
sort.Slice(payments, func(p1, p2 int) bool {
|
|
||||||
return payments[p1].Timestamp > payments[p2].Timestamp
|
|
||||||
})
|
|
||||||
return payments, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
page = page + 1
|
page++
|
||||||
if page > MaxIterations {
|
if page >= totalPages {
|
||||||
return nil, fmt.Errorf("Max iterations of %d reached", MaxIterations)
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if page > MaxIterations {
|
||||||
|
return nil, fmt.Errorf("Max iterations of %d reached", MaxIterations)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort by timestamp
|
||||||
|
sort.Slice(payments, func(p1, p2 int) bool {
|
||||||
|
return payments[p1].Timestamp > payments[p2].Timestamp
|
||||||
|
})
|
||||||
|
return payments, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LastMinerPayment return the last payment of a miner
|
// LastMinerPayment return the last payment of a miner
|
||||||
|
|
Reference in a new issue