Skip to content

Commit

Permalink
clients/horizonclient: fix multi-parameter url for claimable balance …
Browse files Browse the repository at this point in the history
…query (#4248)

* Add cursor and limit parameters to the ClaimableBalanceRequest
* Close #4247
  • Loading branch information
iateadonut authored Mar 28, 2022
1 parent 463d2b9 commit 489c091
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
19 changes: 12 additions & 7 deletions clients/horizonclient/claimable_balance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
53 changes: 53 additions & 0 deletions clients/horizonclient/claimable_balance_request_test.go
Original file line number Diff line number Diff line change
@@ -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")

}
2 changes: 2 additions & 0 deletions clients/horizonclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 489c091

Please sign in to comment.