github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/request/request.go (about) 1 package request 2 3 import ( 4 "net/http" 5 "strings" 6 7 "github.com/gorilla/mux" 8 9 "github.com/koko1123/flow-go-1/engine/access/rest/middleware" 10 "github.com/koko1123/flow-go-1/model/flow" 11 ) 12 13 // Request a convenience wrapper around the http request to make it easy to read request query params 14 type Request struct { 15 *http.Request 16 ExpandFields map[string]bool 17 selectFields []string 18 Chain flow.Chain 19 } 20 21 func (rd *Request) GetScriptRequest() (GetScript, error) { 22 var req GetScript 23 err := req.Build(rd) 24 return req, err 25 } 26 27 func (rd *Request) GetBlockRequest() (GetBlock, error) { 28 var req GetBlock 29 err := req.Build(rd) 30 return req, err 31 } 32 33 func (rd *Request) GetBlockByIDsRequest() (GetBlockByIDs, error) { 34 var req GetBlockByIDs 35 err := req.Build(rd) 36 return req, err 37 } 38 39 func (rd *Request) GetBlockPayloadRequest() (GetBlockPayload, error) { 40 var req GetBlockPayload 41 err := req.Build(rd) 42 return req, err 43 } 44 45 func (rd *Request) GetCollectionRequest() (GetCollection, error) { 46 var req GetCollection 47 err := req.Build(rd) 48 return req, err 49 } 50 51 func (rd *Request) GetAccountRequest() (GetAccount, error) { 52 var req GetAccount 53 err := req.Build(rd) 54 return req, err 55 } 56 57 func (rd *Request) GetExecutionResultByBlockIDsRequest() (GetExecutionResultByBlockIDs, error) { 58 var req GetExecutionResultByBlockIDs 59 err := req.Build(rd) 60 return req, err 61 } 62 63 func (rd *Request) GetExecutionResultRequest() (GetExecutionResult, error) { 64 var req GetExecutionResult 65 err := req.Build(rd) 66 return req, err 67 } 68 69 func (rd *Request) GetTransactionRequest() (GetTransaction, error) { 70 var req GetTransaction 71 err := req.Build(rd) 72 return req, err 73 } 74 75 func (rd *Request) GetTransactionResultRequest() (GetTransactionResult, error) { 76 var req GetTransactionResult 77 err := req.Build(rd) 78 return req, err 79 } 80 81 func (rd *Request) GetEventsRequest() (GetEvents, error) { 82 var req GetEvents 83 err := req.Build(rd) 84 return req, err 85 } 86 87 func (rd *Request) CreateTransactionRequest() (CreateTransaction, error) { 88 var req CreateTransaction 89 err := req.Build(rd) 90 return req, err 91 } 92 93 func (rd *Request) Expands(field string) bool { 94 return rd.ExpandFields[field] 95 } 96 97 func (rd *Request) Selects() []string { 98 return rd.selectFields 99 } 100 101 func (rd *Request) GetVar(name string) string { 102 vars := mux.Vars(rd.Request) 103 return vars[name] 104 } 105 106 func (rd *Request) GetVars(name string) []string { 107 vars := mux.Vars(rd.Request) 108 return toStringArray(vars[name]) 109 } 110 111 func (rd *Request) GetQueryParam(name string) string { 112 return rd.Request.URL.Query().Get(name) 113 } 114 115 func (rd *Request) GetQueryParams(name string) []string { 116 param := rd.Request.URL.Query().Get(name) 117 return toStringArray(param) 118 } 119 120 // Decorate takes http request and applies functions to produce our custom 121 // request object decorated with values we need 122 func Decorate(r *http.Request, chain flow.Chain) *Request { 123 decoratedReq := &Request{ 124 Request: r, 125 Chain: chain, 126 } 127 128 if expandFields, found := middleware.GetFieldsToExpand(r); found { 129 decoratedReq.ExpandFields = sliceToMap(expandFields) 130 } 131 132 if selectFields, found := middleware.GetFieldsToSelect(r); found { 133 decoratedReq.selectFields = selectFields 134 } 135 136 return decoratedReq 137 } 138 139 func sliceToMap(values []string) map[string]bool { 140 valueMap := make(map[string]bool, len(values)) 141 for _, v := range values { 142 valueMap[v] = true 143 } 144 return valueMap 145 } 146 147 func toStringArray(in string) []string { 148 // currently, the swagger generated Go REST client is incorrectly doing a `fmt.Sprintf("%v", id)` for the id slice 149 // resulting in the client sending the ids in the format [id1 id2 id3...]. This is a temporary workaround to 150 // accommodate the client for now by doing a strings.Fields if commas are not present. 151 // Issue to to fix the client: https://github.com/onflow/flow/issues/698 152 in = strings.TrimSuffix(in, "]") 153 in = strings.TrimPrefix(in, "[") 154 var out []string 155 156 if len(in) == 0 { 157 return []string{} 158 } 159 160 if strings.Contains(in, ",") { 161 out = strings.Split(in, ",") 162 } else { 163 out = strings.Fields(in) 164 } 165 166 return out 167 }