/
account_data.go
68 lines (59 loc) · 1.64 KB
/
account_data.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
60
61
62
63
64
65
66
67
68
package actions
import (
"io"
"net/http"
"github.com/TosinShada/stellar-core/services/horizon/internal/context"
"github.com/TosinShada/stellar-core/services/horizon/internal/db2/history"
)
// AccountDataQuery query struct for account data end-point
type AccountDataQuery struct {
AccountID string `schema:"account_id" valid:"accountID"`
Key string `schema:"key" valid:"length(1|64)"`
}
type accountDataResponse struct {
Value string `json:"value"`
Sponsor string `json:"sponsor,omitempty"`
}
func (adr accountDataResponse) Equals(other StreamableObjectResponse) bool {
other, ok := other.(accountDataResponse)
if !ok {
return false
}
return adr == other
}
type GetAccountDataHandler struct{}
func (handler GetAccountDataHandler) GetResource(w HeaderWriter, r *http.Request) (StreamableObjectResponse, error) {
data, err := loadAccountData(r)
if err != nil {
return nil, err
}
response := accountDataResponse{Value: data.Value.Base64()}
if data.Sponsor.Valid {
response.Sponsor = data.Sponsor.String
}
return response, nil
}
func (handler GetAccountDataHandler) WriteRawResponse(w io.Writer, r *http.Request) error {
data, err := loadAccountData(r)
if err != nil {
return err
}
_, err = w.Write(data.Value)
return err
}
func loadAccountData(r *http.Request) (history.Data, error) {
qp := AccountDataQuery{}
err := getParams(&qp, r)
if err != nil {
return history.Data{}, err
}
historyQ, err := context.HistoryQFromRequest(r)
if err != nil {
return history.Data{}, err
}
data, err := historyQ.GetAccountDataByName(r.Context(), qp.AccountID, qp.Key)
if err != nil {
return history.Data{}, err
}
return data, nil
}