rawrequest.go 2.5 KB

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