-
Notifications
You must be signed in to change notification settings - Fork 175
/
request.go
167 lines (136 loc) · 3.9 KB
/
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
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
package request
import (
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/onflow/flow-go/engine/access/rest/middleware"
"github.com/onflow/flow-go/model/flow"
)
// Request a convenience wrapper around the http request to make it easy to read request query params
type Request struct {
*http.Request
ExpandFields map[string]bool
selectFields []string
Chain flow.Chain
}
func (rd *Request) GetScriptRequest() (GetScript, error) {
var req GetScript
err := req.Build(rd)
return req, err
}
func (rd *Request) GetBlockRequest() (GetBlock, error) {
var req GetBlock
err := req.Build(rd)
return req, err
}
func (rd *Request) GetBlockByIDsRequest() (GetBlockByIDs, error) {
var req GetBlockByIDs
err := req.Build(rd)
return req, err
}
func (rd *Request) GetBlockPayloadRequest() (GetBlockPayload, error) {
var req GetBlockPayload
err := req.Build(rd)
return req, err
}
func (rd *Request) GetCollectionRequest() (GetCollection, error) {
var req GetCollection
err := req.Build(rd)
return req, err
}
func (rd *Request) GetAccountRequest() (GetAccount, error) {
var req GetAccount
err := req.Build(rd)
return req, err
}
func (rd *Request) GetExecutionResultByBlockIDsRequest() (GetExecutionResultByBlockIDs, error) {
var req GetExecutionResultByBlockIDs
err := req.Build(rd)
return req, err
}
func (rd *Request) GetExecutionResultRequest() (GetExecutionResult, error) {
var req GetExecutionResult
err := req.Build(rd)
return req, err
}
func (rd *Request) GetTransactionRequest() (GetTransaction, error) {
var req GetTransaction
err := req.Build(rd)
return req, err
}
func (rd *Request) GetTransactionResultRequest() (GetTransactionResult, error) {
var req GetTransactionResult
err := req.Build(rd)
return req, err
}
func (rd *Request) GetEventsRequest() (GetEvents, error) {
var req GetEvents
err := req.Build(rd)
return req, err
}
func (rd *Request) CreateTransactionRequest() (CreateTransaction, error) {
var req CreateTransaction
err := req.Build(rd)
return req, err
}
func (rd *Request) Expands(field string) bool {
return rd.ExpandFields[field]
}
func (rd *Request) Selects() []string {
return rd.selectFields
}
func (rd *Request) GetVar(name string) string {
vars := mux.Vars(rd.Request)
return vars[name]
}
func (rd *Request) GetVars(name string) []string {
vars := mux.Vars(rd.Request)
return toStringArray(vars[name])
}
func (rd *Request) GetQueryParam(name string) string {
return rd.Request.URL.Query().Get(name)
}
func (rd *Request) GetQueryParams(name string) []string {
param := rd.Request.URL.Query().Get(name)
return toStringArray(param)
}
// Decorate takes http request and applies functions to produce our custom
// request object decorated with values we need
func Decorate(r *http.Request, chain flow.Chain) *Request {
decoratedReq := &Request{
Request: r,
Chain: chain,
}
if expandFields, found := middleware.GetFieldsToExpand(r); found {
decoratedReq.ExpandFields = sliceToMap(expandFields)
}
if selectFields, found := middleware.GetFieldsToSelect(r); found {
decoratedReq.selectFields = selectFields
}
return decoratedReq
}
func sliceToMap(values []string) map[string]bool {
valueMap := make(map[string]bool, len(values))
for _, v := range values {
valueMap[v] = true
}
return valueMap
}
func toStringArray(in string) []string {
// currently, the swagger generated Go REST client is incorrectly doing a `fmt.Sprintf("%v", id)` for the id slice
// resulting in the client sending the ids in the format [id1 id2 id3...]. This is a temporary workaround to
// accommodate the client for now by doing a strings.Fields if commas are not present.
// Issue to to fix the client: https://github.com/onflow/flow/issues/698
in = strings.TrimSuffix(in, "]")
in = strings.TrimPrefix(in, "[")
var out []string
if len(in) == 0 {
return []string{}
}
if strings.Contains(in, ",") {
out = strings.Split(in, ",")
} else {
out = strings.Fields(in)
}
return out
}