1 // Copyright (c) 2014-2017 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
11 "github.com/btcsuite/btcd/btcjson"
14 // FutureRawResult is a future promise to deliver the result of a RawRequest RPC
15 // invocation (or an applicable error).
16 type FutureRawResult chan *response
18 // Receive waits for the response promised by the future and returns the raw
19 // response, or an error if the request was unsuccessful.
20 func (r FutureRawResult) Receive() (json.RawMessage, error) {
21 return receiveFuture(r)
24 // RawRequestAsync returns an instance of a type that can be used to get the
25 // result of a custom RPC request at some future time by invoking the Receive
26 // function on the returned instance.
28 // See RawRequest for the blocking version and more details.
29 func (c *Client) RawRequestAsync(method string, params []json.RawMessage) FutureRawResult {
30 // Method may not be empty.
32 return newFutureError(errors.New("no method"))
35 // Marshal parameters as "[]" instead of "null" when no parameters
38 params = []json.RawMessage{}
41 // Create a raw JSON-RPC request using the provided method and params
42 // and marshal it. This is done rather than using the sendCmd function
43 // since that relies on marshalling registered btcjson commands rather
44 // than custom commands.
46 rawRequest := &btcjson.Request{
52 marshalledJSON, err := json.Marshal(rawRequest)
54 return newFutureError(err)
57 // Generate the request and send it along with a channel to respond on.
58 responseChan := make(chan *response, 1)
63 marshalledJSON: marshalledJSON,
64 responseChan: responseChan,
71 // RawRequest allows the caller to send a raw or custom request to the server.
72 // This method may be used to send and receive requests and responses for
73 // requests that are not handled by this client package, or to proxy partially
74 // unmarshaled requests to another JSON-RPC server if a request cannot be
76 func (c *Client) RawRequest(method string, params []json.RawMessage) (json.RawMessage, error) {
77 return c.RawRequestAsync(method, params).Receive()