forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
claimable_balance_request.go
58 lines (48 loc) · 1.54 KB
/
claimable_balance_request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package horizonclient
import (
"fmt"
"net/http"
"net/url"
"github.com/stellar/go/support/errors"
)
// Creates the URL to either request a specific claimable balance (CB) by ID, or
// request all CBs, possibly filtered by asset, claimant, or sponsor.
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 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(
params, limit(cbr.Limit), cursor(cbr.Cursor),
)
endpoint = fmt.Sprintf("%s?%s", endpoint, queryParams)
}
_, err = url.Parse(endpoint)
if err != nil {
err = errors.Wrap(err, "failed to parse endpoint")
}
return endpoint, err
}
// HTTPRequest returns the http request for the claimable balances endpoint
func (cbr ClaimableBalanceRequest) HTTPRequest(horizonURL string) (*http.Request, error) {
endpoint, err := cbr.BuildURL()
if err != nil {
return nil, err
}
return http.NewRequest("GET", horizonURL+endpoint, nil)
}