From 489c091a42a5023bca921357fa6c0476a6194ce0 Mon Sep 17 00:00:00 2001 From: iateadonut Date: Tue, 29 Mar 2022 04:47:04 +0900 Subject: [PATCH] clients/horizonclient: fix multi-parameter url for claimable balance query (#4248) * Add cursor and limit parameters to the ClaimableBalanceRequest * Close #4247 --- .../claimable_balance_request.go | 19 ++++--- .../claimable_balance_request_test.go | 53 +++++++++++++++++++ clients/horizonclient/main.go | 2 + 3 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 clients/horizonclient/claimable_balance_request_test.go diff --git a/clients/horizonclient/claimable_balance_request.go b/clients/horizonclient/claimable_balance_request.go index eb80c13249..bbb6f96c6c 100644 --- a/clients/horizonclient/claimable_balance_request.go +++ b/clients/horizonclient/claimable_balance_request.go @@ -13,24 +13,29 @@ import ( func (cbr ClaimableBalanceRequest) BuildURL() (endpoint string, err error) { endpoint = "claimable_balances" + //max limit is 200 + if cbr.Limit > 200 { + return endpoint, errors.New("invalid request: limit " + fmt.Sprint(cbr.Limit) + " is greater than limit max of 200") + } + // Only one filter parameter is allowed, and you can't mix an ID query and // filters. nParams := countParams(cbr.Asset, cbr.Claimant, cbr.Sponsor, cbr.ID) - if nParams > 1 { + if cbr.ID != "" && nParams > 1 { return endpoint, errors.New("invalid request: too many parameters") } if cbr.ID != "" { endpoint = fmt.Sprintf("%s/%s", endpoint, cbr.ID) } else { + params := map[string]string{ + "claimant": cbr.Claimant, + "sponsor": cbr.Sponsor, + "asset": cbr.Asset, + } queryParams := addQueryParams( - map[string]string{ - "claimant": cbr.Claimant, - "sponsor": cbr.Sponsor, - "asset": cbr.Asset, - }, + params, limit(cbr.Limit), cursor(cbr.Cursor), ) - endpoint = fmt.Sprintf("%s?%s", endpoint, queryParams) } diff --git a/clients/horizonclient/claimable_balance_request_test.go b/clients/horizonclient/claimable_balance_request_test.go new file mode 100644 index 0000000000..3503e8caa5 --- /dev/null +++ b/clients/horizonclient/claimable_balance_request_test.go @@ -0,0 +1,53 @@ +package horizonclient + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestClaimableBalanceBuildUrl(t *testing.T) { + + cbr := ClaimableBalanceRequest{ + ID: "1235", + } + url, err := cbr.BuildURL() + assert.Equal(t, "claimable_balances/1235", url) + assert.NoError(t, err) + + //if the ID is included, you cannot include another parameter + cbr = ClaimableBalanceRequest{ + ID: "1235", + Claimant: "CLAIMANTADDRESS", + } + _, err = cbr.BuildURL() + assert.EqualError(t, err, "invalid request: too many parameters") + + //if you have two parameters, and neither of them are ID, it must use both in the URL + cbr = ClaimableBalanceRequest{ + Claimant: "CLAIMANTADDRESS", + Asset: "TEST:ISSUERADDRESS", + } + url, err = cbr.BuildURL() + assert.NoError(t, err) + assert.Equal(t, "claimable_balances?asset=TEST%3AISSUERADDRESS&claimant=CLAIMANTADDRESS", url) + + //check limit + cbr = ClaimableBalanceRequest{ + Claimant: "CLAIMANTADDRESS", + Asset: "TEST:ISSUERADDRESS", + Limit: 200, + } + url, err = cbr.BuildURL() + assert.NoError(t, err) + assert.Equal(t, "claimable_balances?asset=TEST%3AISSUERADDRESS&claimant=CLAIMANTADDRESS&limit=200", url) + + cbr = ClaimableBalanceRequest{ + Claimant: "CLAIMANTADDRESS", + Asset: "TEST:ISSUERADDRESS", + Limit: 201, + } + _, err = cbr.BuildURL() + assert.EqualError(t, err, "invalid request: limit 201 is greater than limit max of 200") + +} diff --git a/clients/horizonclient/main.go b/clients/horizonclient/main.go index 209b2a0fab..d85954e922 100644 --- a/clients/horizonclient/main.go +++ b/clients/horizonclient/main.go @@ -434,6 +434,8 @@ type ClaimableBalanceRequest struct { Asset string Sponsor string Claimant string + Cursor string + Limit uint } // ServerTimeRecord contains data for the current unix time of a horizon server instance, and the local time when it was recorded.