/
endpoints.go
211 lines (190 loc) 路 6.33 KB
/
endpoints.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package wallet
import (
"context"
"os"
"time"
"github.com/go-kit/kit/endpoint"
"github.com/ilyakaznacheev/tiny-wallet/pkg/currency"
)
const (
urlAPIDoc = "https://github.com/ilyakaznacheev/tiny-wallet/blob/master/api/api.md"
urlAPIMain = "https://github.com/ilyakaznacheev/tiny-wallet"
)
// Endpoints is a set of service API endpoints
type Endpoints struct {
// GetAllPaymentsEndpoint returns all payments in the system
GetAllPaymentsEndpoint endpoint.Endpoint
// GetAllAccountsEndpoint returns all accounts in the system
GetAllAccountsEndpoint endpoint.Endpoint
// PostPayment processes a new payment
PostPayment endpoint.Endpoint
// PostAccount creates a new account
PostAccount endpoint.Endpoint
// RedirectMain redirects the user from the main page
RedirectMain endpoint.Endpoint
// RedirectAPI redirects the user from the API page
RedirectAPI endpoint.Endpoint
}
// MakeServerEndpoints creates server handlers for each endpoint
func MakeServerEndpoints(s Service) Endpoints {
return Endpoints{
GetAllPaymentsEndpoint: makeGetAllPaymentsEndpoint(s),
GetAllAccountsEndpoint: makeGetAllAccountsEndpoint(s),
PostPayment: makePostPaymentEndpoint(s),
PostAccount: makePostAccountEndpoint(s),
RedirectAPI: makeRedirectAPIEndpoint(s),
RedirectMain: makeRedirectMainEndpoint(s),
}
}
// makeGetAllPaymentsEndpoint creates a GetAllPayments endpoint handler
func makeGetAllPaymentsEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
// call service logic
payments, err := s.GetAllPayments(ctx)
if err != nil {
return nil, err
}
// convert results into the response format
res := GetAllPaymentsResponse{
Payments: make([]Payment, 0, len(payments)),
}
for _, p := range payments {
res.Payments = append(res.Payments, Payment{
AccFromID: p.AccFromID,
AccToID: p.AccToID,
DateTime: p.DateTime,
Amount: currency.ConvertToExternal(p.Amount, p.Currency),
Currency: p.Currency,
})
}
return res, nil
}
}
// makeGetAllAccountsEndpoint creates a GetAllAccounts endpoint handler
func makeGetAllAccountsEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
// call service logic
accounts, err := s.GetAllAccounts(ctx)
if err != nil {
return nil, err
}
// convert results into the response format
res := GetAllAccountsResponse{
Accounts: make([]Account, 0, len(accounts)),
}
for _, a := range accounts {
res.Accounts = append(res.Accounts, Account{
ID: a.ID,
Balance: currency.ConvertToExternal(a.Balance, a.Currency),
Currency: a.Currency,
})
}
return res, nil
}
}
// makePostPaymentEndpoint creates a PostPayment endpoint handler
func makePostPaymentEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(PostPaymentRequest)
// call service logic
res, err := s.PostPayment(ctx, req.AccountFromID, req.AccountToID, req.Amount)
if err != nil {
return nil, err
}
// convert results into the response format
payment := Payment{
AccFromID: res.AccFromID,
AccToID: res.AccToID,
DateTime: res.DateTime,
Amount: currency.ConvertToExternal(res.Amount, res.Currency),
Currency: res.Currency,
}
return &payment, nil
}
}
// makePostAccountEndpoint creates a PostAccount endpoint handler
func makePostAccountEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(PostAccountRequest)
// call service logic
res, err := s.PostAccount(ctx, req.ID, req.Balance, req.Currency)
if err != nil {
return nil, err
}
// convert results into the response format
account := Account{
ID: res.ID,
Balance: currency.ConvertToExternal(res.Balance, res.Currency),
Currency: res.Currency,
}
return &account, nil
}
}
// makeRedirectAPIEndpoint redirects to api documentation page
func makeRedirectAPIEndpoint(s Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (response interface{}, err error) {
// redirect to API documentation
redirectURL := urlAPIDoc
return &redirectURL, nil
}
}
// makeRedirectMainEndpoint redirects to main project page or preconfigured redirect link
func makeRedirectMainEndpoint(s Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (response interface{}, err error) {
// redirect to main project page or specified page
redirectURL := urlAPIMain
if redirectEnv := os.Getenv("REDIRECT_MAIN"); redirectEnv != "" {
redirectURL = redirectEnv
}
return &redirectURL, nil
}
}
// API data structures
type (
// PostPaymentRequest is a request structure for the PostPayment endpoint.
//
// It is used to structure REST request data.
PostPaymentRequest struct {
AccountFromID string `json:"account-from"`
AccountToID string `json:"account-to"`
Amount float64 `json:"amount"`
}
// PostAccountRequest is a request structure for the PostAccount endpoint.
//
// It is used to structure REST request data.
PostAccountRequest struct {
ID string `json:"id"`
Balance float64 `json:"balance"`
Currency string `json:"currency"`
}
// GetAllPaymentsResponse is a request structure for the GetAllPayments endpoint
//
// It is used to structure REST response data.
GetAllPaymentsResponse struct {
Payments []Payment `json:"payments"`
}
// GetAllAccountsResponse is a request structure for the GetAllAccounts endpoint.
//
// It is used to structure REST response data.
GetAllAccountsResponse struct {
Accounts []Account `json:"accounts"`
}
// Account is a financial account.
//
// It is used to structure REST response data.
Account struct {
ID string `json:"id"`
Balance float64 `json:"balance"`
Currency currency.Currency `json:"currency"`
}
// Payment is a financial transaction between accounts.
//
// It is used to structure REST response data.
Payment struct {
AccFromID string `json:"account-from"`
AccToID string `json:"account-to"`
DateTime time.Time `json:"time,omitempty"`
Amount float64 `json:"amount"`
Currency currency.Currency `json:"currency"`
}
)