github.com/lbryio/lbcd@v0.22.119/rpcclient/rawrequest.go (about) 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. 4 5 package rpcclient 6 7 import ( 8 "encoding/json" 9 "errors" 10 11 "github.com/lbryio/lbcd/btcjson" 12 ) 13 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 17 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) 22 } 23 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. 27 // 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. 31 if method == "" { 32 return newFutureError(errors.New("no method")) 33 } 34 35 // Marshal parameters as "[]" instead of "null" when no parameters 36 // are passed. 37 if params == nil { 38 params = []json.RawMessage{} 39 } 40 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. 45 id := c.NextID() 46 rawRequest := &btcjson.Request{ 47 Jsonrpc: btcjson.RpcVersion1, 48 ID: id, 49 Method: method, 50 Params: params, 51 } 52 marshalledJSON, err := json.Marshal(rawRequest) 53 if err != nil { 54 return newFutureError(err) 55 } 56 57 // Generate the request and send it along with a channel to respond on. 58 responseChan := make(chan *Response, 1) 59 jReq := &jsonRequest{ 60 id: id, 61 method: method, 62 cmd: nil, 63 marshalledJSON: marshalledJSON, 64 responseChan: responseChan, 65 } 66 c.sendRequest(jReq) 67 68 return responseChan 69 } 70 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 75 // handled directly. 76 func (c *Client) RawRequest(method string, params []json.RawMessage) (json.RawMessage, error) { 77 return c.RawRequestAsync(method, params).Receive() 78 }