forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts_request.go
56 lines (44 loc) · 1.22 KB
/
accounts_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
package horizonclient
import (
"fmt"
"net/url"
"github.com/stellar/go/support/errors"
)
// BuildURL creates the endpoint to be queried based on the data in the AccountsRequest struct.
// Either "Signer" or "Asset" fields should be set when retrieving Accounts.
// At the moment, you can't use both filters at the same time.
func (r AccountsRequest) BuildURL() (endpoint string, err error) {
nParams := countParams(r.Signer, r.Asset)
if nParams <= 0 {
err = errors.New("invalid request: no parameters - Signer or Asset must be provided")
}
if nParams >= 2 {
err = errors.New("invalid request: too many parameters - Signer and Asset provided, provide a single filter")
}
if err != nil {
return endpoint, err
}
query := url.Values{}
switch {
case len(r.Signer) > 0:
query.Add("signer", r.Signer)
case len(r.Asset) > 0:
query.Add("asset", r.Asset)
}
endpoint = fmt.Sprintf(
"accounts?%s",
query.Encode(),
)
if pageParams := addQueryParams(cursor(r.Cursor), limit(r.Limit), r.Order); len(pageParams) > 0 {
endpoint = fmt.Sprintf(
"%s&%s",
endpoint,
pageParams,
)
}
_, err = url.Parse(endpoint)
if err != nil {
err = errors.Wrap(err, "failed to parse endpoint")
}
return endpoint, err
}