From d01fefbfbfd1d7f626c15b7e8133f8dc01c17be7 Mon Sep 17 00:00:00 2001 From: Julien Riou Date: Fri, 8 Apr 2022 13:41:44 +0200 Subject: [PATCH] fix(payments): Improve paged response (#8) Signed-off-by: Julien Riou --- client.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index 7d920c4..b87ae26 100644 --- a/client.go +++ b/client.go @@ -87,7 +87,8 @@ func (f *FlexpoolClient) MinerBalance(coin string, address string) (float64, err type PaymentsResponse struct { Error string `json:"error"` Result struct { - Data []struct { + TotalPages int `json:"totalPages"` + Data []struct { Hash string `json:"hash"` Value float64 `json:"value"` Timestamp int64 `json:"timestamp"` @@ -98,7 +99,9 @@ type PaymentsResponse struct { // MinerPayments returns an ordered list of payments func (f *FlexpoolClient) MinerPayments(coin string, address string, limit int) (payments []*Payment, err error) { 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)) if err != nil { return nil, err @@ -107,6 +110,10 @@ func (f *FlexpoolClient) MinerPayments(coin string, address string, limit int) ( var response PaymentsResponse json.Unmarshal(body, &response) + if totalPages == 0 { + totalPages = response.Result.TotalPages + } + for _, result := range response.Result.Data { payment := NewPayment( result.Hash, @@ -115,18 +122,24 @@ func (f *FlexpoolClient) MinerPayments(coin string, address string, limit int) ( ) payments = append(payments, payment) if len(payments) >= limit { - // sort by timestamp - sort.Slice(payments, func(p1, p2 int) bool { - return payments[p1].Timestamp > payments[p2].Timestamp - }) - return payments, nil + break } } - page = page + 1 - if page > MaxIterations { - return nil, fmt.Errorf("Max iterations of %d reached", MaxIterations) + page++ + if page >= totalPages { + 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