-
Notifications
You must be signed in to change notification settings - Fork 501
/
account_request.go
59 lines (47 loc) · 1.33 KB
/
account_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
59
package horizonclient
import (
"fmt"
"net/http"
"net/url"
"github.com/stellar/go/support/errors"
)
// BuildURL creates the endpoint to be queried based on the data in the AccountRequest struct.
// If only AccountID is present, then the endpoint for account details is returned.
// If both AccounId and DataKey are present, then the endpoint for getting account data is returned
func (ar AccountRequest) BuildURL() (endpoint string, err error) {
nParams := countParams(ar.DataKey, ar.AccountID)
if nParams >= 1 && ar.AccountID == "" {
err = errors.New("invalid request: too few parameters")
}
if nParams <= 0 {
err = errors.New("invalid request: no parameters")
}
if err != nil {
return endpoint, err
}
if ar.DataKey != "" && ar.AccountID != "" {
endpoint = fmt.Sprintf(
"accounts/%s/data/%s",
ar.AccountID,
ar.DataKey,
)
} else if ar.AccountID != "" {
endpoint = fmt.Sprintf(
"accounts/%s",
ar.AccountID,
)
}
_, 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 account endpoint
func (ar AccountRequest) HTTPRequest(horizonURL string) (*http.Request, error) {
endpoint, err := ar.BuildURL()
if err != nil {
return nil, err
}
return http.NewRequest("GET", horizonURL+endpoint, nil)
}