forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
responses.go
215 lines (184 loc) · 5.59 KB
/
responses.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
212
213
214
215
// Copyright (C) 2019 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
// Package handlers handles and helps specify the algod/api
//
// Currently, server implementation annotations serve
// as the API ground truth. From that, we use go-swagger
// to generate a swagger spec.
//
// IF YOU MODIFY THIS PACKAGE: IMPORTANT
// MAKE SURE YOU REGENERATE THE SWAGGER SPEC (using go:generate)
// MAKE SURE IT VALIDATES
package handlers
import (
"encoding/json"
"io"
"net/http"
"github.com/vincentbdb/go-algorand/daemon/algod/api/spec/v1"
"github.com/vincentbdb/go-algorand/logging"
)
// Response is a generic interface wrapping any data returned by the server.
// We wrap every type in a Response type so that we can swagger annotate them.
//
// Each response must have a Body (a payload). We
// write an interface for this because it better mirrors the
// go-swagger annotation style (which requires swagger colon responses
// to have an embedded body struct of the actual data to be sent. of
// course, they can also have headers and the sort.)
// Anything implementing the Response interface will naturally be
// able to be annotated by swagger:response. This also allows package
// functions to naturally unwrap Response types and send the underlying
// Body through another interface (e.g. an http.ResponseWriter)
type Response interface {
getBody() interface{}
}
func writeJSON(obj interface{}, w io.Writer) error {
enc := json.NewEncoder(w)
return enc.Encode(obj)
}
// SendJSON is like writeJSON, but it writes to the log instead of returning an error.
// The caller must ensure that no writes to w happen after this function is called.
// Unwraps a Response object and converts it to an HTTP Response.
func SendJSON(obj Response, w http.ResponseWriter, log logging.Logger) {
w.Header().Set("Content-Type", "application/json")
err := writeJSON(obj.getBody(), w)
if err != nil {
log.Warnf("algod failed to write an object to the response stream: %v", err)
}
}
// StatusResponse contains the node's status information
//
// swagger:response StatusResponse
type StatusResponse struct {
// in: body
Body *v1.NodeStatus
}
func (sr StatusResponse) getBody() interface{} {
return sr.Body
}
// TransactionIDResponse contains a transaction information
//
// swagger:response TransactionIDResponse
type TransactionIDResponse struct {
// in: body
Body *v1.TransactionID
}
func (r TransactionIDResponse) getBody() interface{} {
return r.Body
}
// AccountInformationResponse contains an account information
//
// swagger:response AccountInformationResponse
type AccountInformationResponse struct {
// in: body
Body *v1.Account
}
func (r AccountInformationResponse) getBody() interface{} {
return r.Body
}
// TransactionResponse contains a transaction information
//
// swagger:response TransactionResponse
type TransactionResponse struct {
// in: body
Body *v1.Transaction
}
func (r TransactionResponse) getBody() interface{} {
return r.Body
}
// TransactionsResponse contains a list of transactions
//
// swagger:response TransactionsResponse
type TransactionsResponse struct {
// in: body
Body *v1.TransactionList
}
func (r TransactionsResponse) getBody() interface{} {
return r.Body
}
// AssetsResponse contains a list of assets
//
// swagger:response AssetsResponse
type AssetsResponse struct {
// in: body
Body *v1.AssetList
}
func (r AssetsResponse) getBody() interface{} {
return r.Body
}
// AssetInformationResponse contains asset information
//
// swagger:response AssetInformationResponse
type AssetInformationResponse struct {
// in: body
Body *v1.AssetParams
}
func (r AssetInformationResponse) getBody() interface{} {
return r.Body
}
// TransactionFeeResponse contains a suggested fee
//
// swagger:response TransactionFeeResponse
type TransactionFeeResponse struct {
// in: body
Body *v1.TransactionFee
}
func (r TransactionFeeResponse) getBody() interface{} {
return r.Body
}
// TransactionParamsResponse contains the parameters for
// constructing a new transaction.
//
// swagger:response TransactionParamsResponse
type TransactionParamsResponse struct {
// in: body
Body *v1.TransactionParams
}
func (r TransactionParamsResponse) getBody() interface{} {
return r.Body
}
// BlockResponse contains block information
//
// swagger:response BlockResponse
type BlockResponse struct {
// in: body
Body *v1.Block
}
func (r BlockResponse) getBody() interface{} {
return r.Body
}
// SupplyResponse contains the ledger supply information
//
// swagger:response SupplyResponse
type SupplyResponse struct {
// in: body
Body *v1.Supply
}
func (r SupplyResponse) getBody() interface{} {
return r.Body
}
/* Errors */
// PendingTransactionsResponse contains a (potentially truncated) list of transactions and
// the total number of transactions currently in the pool.
//
// swagger:response PendingTransactionsResponse
type PendingTransactionsResponse struct {
// in: body
Body *v1.PendingTransactions
}
func (r PendingTransactionsResponse) getBody() interface{} {
return r.Body
}