MCPcopy Create free account
hub / github.com/CPChain/chain / parseBatchRequest

Function parseBatchRequest

api/rpc/json.go:220–271  ·  view source on GitHub ↗

parseBatchRequest will parse a batch request into a collection of requests from the given RawMessage, an indication if the request was a batch or an error when the request could not be read.

(incomingMsg json.RawMessage)

Source from the content-addressed store, hash-verified

218// parseBatchRequest will parse a batch request into a collection of requests from the given RawMessage, an indication
219// if the request was a batch or an error when the request could not be read.
220func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) {
221 var in []jsonRequest
222 if err := json.Unmarshal(incomingMsg, &in); err != nil {
223 return nil, false, &invalidMessageError{err.Error()}
224 }
225
226 requests := make([]rpcRequest, len(in))
227 for i, r := range in {
228 if err := checkReqId(r.Id); err != nil {
229 return nil, false, &invalidMessageError{err.Error()}
230 }
231
232 id := &in[i].Id
233
234 // subscribe are special, they will always use `subscriptionMethod` as first param in the payload
235 if strings.HasSuffix(r.Method, subscribeMethodSuffix) {
236 requests[i] = rpcRequest{id: id, isPubSub: true}
237 if len(r.Payload) > 0 {
238 // first param must be subscription name
239 var subscribeMethod [1]string
240 if err := json.Unmarshal(r.Payload, &subscribeMethod); err != nil {
241 log.Debug(fmt.Sprintf("Unable to parse subscription method: %v\n", err))
242 return nil, false, &invalidRequestError{"Unable to parse subscription request"}
243 }
244
245 requests[i].service, requests[i].method = strings.TrimSuffix(r.Method, subscribeMethodSuffix), subscribeMethod[0]
246 requests[i].params = r.Payload
247 continue
248 }
249
250 return nil, true, &invalidRequestError{"Unable to parse (un)subscribe request arguments"}
251 }
252
253 if strings.HasSuffix(r.Method, unsubscribeMethodSuffix) {
254 requests[i] = rpcRequest{id: id, isPubSub: true, method: r.Method, params: r.Payload}
255 continue
256 }
257
258 if len(r.Payload) == 0 {
259 requests[i] = rpcRequest{id: id, params: nil}
260 } else {
261 requests[i] = rpcRequest{id: id, params: r.Payload}
262 }
263 if elem := strings.Split(r.Method, serviceMethodSeparator); len(elem) == 2 {
264 requests[i].service, requests[i].method = elem[0], elem[1]
265 } else {
266 requests[i].err = &methodNotFoundError{r.Method, ""}
267 }
268 }
269
270 return requests, true, nil
271}
272
273// ParseRequestArguments tries to parse the given params (json.RawMessage) with the given
274// types. It returns the parsed values or an error when the parsing failed.

Callers 1

ReadRequestHeadersMethod · 0.85

Calls 3

checkReqIdFunction · 0.85
DebugMethod · 0.80
ErrorMethod · 0.65

Tested by

no test coverage detected