CallContext performs a JSON-RPC call with the given arguments. If the context is canceled before the call has successfully returned, CallContext returns immediately. The result must be a pointer so that package json can unmarshal into it. You can also pass nil, in which case the result is ignored.
(ctx context.Context, result interface{}, method string, args ...interface{})
| 289 | // The result must be a pointer so that package json can unmarshal into it. You |
| 290 | // can also pass nil, in which case the result is ignored. |
| 291 | func (c *Client) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error { |
| 292 | msg, err := c.newMessage(method, args...) |
| 293 | if err != nil { |
| 294 | return err |
| 295 | } |
| 296 | op := &requestOp{ids: []json.RawMessage{msg.ID}, resp: make(chan *jsonrpcMessage, 1)} |
| 297 | |
| 298 | if c.isHTTP { |
| 299 | err = c.sendHTTP(ctx, op, msg) |
| 300 | } else { |
| 301 | err = c.send(ctx, op, msg) |
| 302 | } |
| 303 | if err != nil { |
| 304 | return err |
| 305 | } |
| 306 | |
| 307 | // dispatch has accepted the request and will close the channel it when it quits. |
| 308 | switch resp, err := op.wait(ctx); { |
| 309 | case err != nil: |
| 310 | return err |
| 311 | case resp.Error != nil: |
| 312 | return resp.Error |
| 313 | case len(resp.Result) == 0: |
| 314 | return ErrNoResult |
| 315 | default: |
| 316 | return json.Unmarshal(resp.Result, &result) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // BatchCall sends all given requests as a single batch and waits for the server |
| 321 | // to return a response for all of them. |