1 package rpcclient
2 3 import (
4 js "encoding/json"
5 "errors"
6 7 "github.com/p9c/p9/pkg/btcjson"
8 )
9 10 // FutureRawResult is a future promise to deliver the result of a RawRequest RPC invocation (or an applicable error).
11 type FutureRawResult chan *response
12 13 // Receive waits for the response promised by the future and returns the raw response, or an error if the request was unsuccessful.
14 func (r FutureRawResult) Receive() (js.RawMessage, error) {
15 return receiveFuture(r)
16 }
17 18 // RawRequestAsync returns an instance of a type that can be used to get the result of a custom RPC request at some
19 // future time by invoking the Receive function on the returned instance.
20 //
21 // See RawRequest for the blocking version and more details.
22 func (c *Client) RawRequestAsync(method string, params []js.RawMessage) FutureRawResult {
23 // Method may not be empty.
24 if method == "" {
25 return newFutureError(errors.New("no method"))
26 }
27 // Marshal parameters as "[]" instead of "null" when no parameters are passed.
28 if params == nil {
29 params = []js.RawMessage{}
30 }
31 // Create a raw JSON-RPC request using the provided method and netparams and marshal it. This is done rather than
32 // using the sendCmd function since that relies on marshalling registered json commands rather than custom commands.
33 id := c.NextID()
34 rawRequest := &btcjson.Request{
35 Jsonrpc: "1.0",
36 ID: id,
37 Method: method,
38 Params: params,
39 }
40 marshalledJSON, e := js.Marshal(rawRequest)
41 if e != nil {
42 return newFutureError(e)
43 }
44 // Generate the request and send it along with a channel to respond on.
45 responseChan := make(chan *response, 1)
46 jReq := &jsonRequest{
47 id: id,
48 method: method,
49 cmd: nil,
50 marshalledJSON: marshalledJSON,
51 responseChan: responseChan,
52 }
53 c.sendRequest(jReq)
54 return responseChan
55 }
56 57 // RawRequest allows the caller to send a raw or custom request to the server.
58 //
59 // This method may be used to send and receive requests and responses for requests that are not handled by this client
60 // package, or to proxy partially unmarshalled requests to another JSON-RPC server if a request cannot be handled
61 // directly.
62 func (c *Client) RawRequest(method string, params []js.RawMessage) (js.RawMessage, error) {
63 return c.RawRequestAsync(method, params).Receive()
64 }
65