-
Notifications
You must be signed in to change notification settings - Fork 497
/
accounts.go
142 lines (118 loc) · 3.56 KB
/
accounts.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package actions
import (
"errors"
"net/http"
"os"
"strconv"
"github.com/stellar/go/support/log"
"github.com/stellar/go/xdr"
"github.com/stellar/go/exp/lighthorizon/adapters"
"github.com/stellar/go/exp/lighthorizon/services"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/protocols/horizon/operations"
"github.com/stellar/go/support/render/hal"
supportProblem "github.com/stellar/go/support/render/problem"
"github.com/stellar/go/toid"
)
const (
urlAccountId = "account_id"
)
func accountRequestParams(w http.ResponseWriter, r *http.Request) (string, pagination, error) {
var accountId string
var accountErr bool
if accountId, accountErr = getURLParam(r, urlAccountId); !accountErr {
return "", pagination{}, errors.New("unable to find account_id in url path")
}
paginate, err := paging(r)
if err != nil {
return "", pagination{}, err
}
if paginate.Cursor < 1 {
paginate.Cursor = toid.New(1, 1, 1).ToInt64()
}
if paginate.Limit == 0 {
paginate.Limit = 10
}
return accountId, paginate, nil
}
func NewTXByAccountHandler(lightHorizon services.LightHorizon) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var accountId string
var paginate pagination
var err error
if accountId, paginate, err = accountRequestParams(w, r); err != nil {
errorMsg := supportProblem.MakeInvalidFieldProblem("account_id", err)
sendErrorResponse(r.Context(), w, *errorMsg)
return
}
page := hal.Page{
Cursor: strconv.FormatInt(paginate.Cursor, 10),
Order: string(paginate.Order),
Limit: uint64(paginate.Limit),
}
page.Init()
page.FullURL = r.URL
txns, err := lightHorizon.Transactions.GetTransactionsByAccount(ctx, paginate.Cursor, paginate.Limit, accountId)
if err != nil {
log.Error(err)
if os.IsNotExist(err) {
sendErrorResponse(r.Context(), w, supportProblem.NotFound)
} else if err != nil {
sendErrorResponse(r.Context(), w, supportProblem.ServerError)
}
return
}
encoder := xdr.NewEncodingBuffer()
for _, txn := range txns {
var response hProtocol.Transaction
response, err = adapters.PopulateTransaction(r.URL, &txn, encoder)
if err != nil {
log.Error(err)
sendErrorResponse(r.Context(), w, supportProblem.ServerError)
return
}
page.Add(response)
}
page.PopulateLinks()
sendPageResponse(r.Context(), w, page)
}
}
func NewOpsByAccountHandler(lightHorizon services.LightHorizon) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var accountId string
var paginate pagination
var err error
if accountId, paginate, err = accountRequestParams(w, r); err != nil {
errorMsg := supportProblem.MakeInvalidFieldProblem("account_id", err)
sendErrorResponse(r.Context(), w, *errorMsg)
return
}
page := hal.Page{
Cursor: strconv.FormatInt(paginate.Cursor, 10),
Order: string(paginate.Order),
Limit: uint64(paginate.Limit),
}
page.Init()
page.FullURL = r.URL
ops, err := lightHorizon.Operations.GetOperationsByAccount(ctx, paginate.Cursor, paginate.Limit, accountId)
if err != nil {
log.Error(err)
sendErrorResponse(r.Context(), w, supportProblem.ServerError)
return
}
for _, op := range ops {
var response operations.Operation
response, err = adapters.PopulateOperation(r, &op)
if err != nil {
log.Error(err)
sendErrorResponse(r.Context(), w, supportProblem.ServerError)
return
}
page.Add(response)
}
page.PopulateLinks()
sendPageResponse(r.Context(), w, page)
}
}