Documentation
¶
Overview ¶
Package jsonrpc provides a JSON-RPC 2.0 client that sends JSON-RPC requests and receives JSON-RPC responses using HTTP.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RPCClient ¶
type RPCClient interface {
// Call is a very handy function to send a JSON-RPC request to the server endpoint.
//
// params can only be an array or an object, no primitive values.
// So there are a few simple rules to notice:
//
// 1. no params: params field is omitted. e.g. Call("getinfo")
//
// 2. single params primitive value: value is wrapped in array. e.g. Call("getByID", 1423)
//
// 3. single params value array or object: value is unchanged. e.g. Call("storePerson", &Person{Name: "Alex"})
//
// 4. multiple params values: always wrapped in array. e.g. Call("setDetails", "Alex, 35, "Germany", true)
//
// Examples:
// Call("getinfo") -> {"method": "getinfo"}
// Call("getPersonId", 123) -> {"method": "getPersonId", "params": [123]}
// Call("setName", "Alex") -> {"method": "setName", "params": ["Alex"]}
// Call("setMale", true) -> {"method": "setMale", "params": [true]}
// Call("setNumbers", []int{1, 2, 3}) -> {"method": "setNumbers", "params": [1, 2, 3]}
// Call("setNumbers", 1, 2, 3) -> {"method": "setNumbers", "params": [1, 2, 3]}
// Call("savePerson", &Person{Name: "Alex", Age: 35}) -> {"method": "savePerson", "params": {"name": "Alex", "age": 35}}
// Call("setPersonDetails", "Alex", 35, "Germany") -> {"method": "setPersonDetails", "params": ["Alex", 35, "Germany"}}
//
// for more information, see the examples or the unit tests
Call(method string, params ...interface{}) (*RPCResponse, error)
// CallFor is a very handy function to send a JSON-RPC request to the server endpoint
// and directly specify an object to store the response.
//
// out: will store the unmarshaled object, if request was successful.
// should always be provided by references. can be nil even on success.
//
// method and params: see Call() function
//
// if the request was not successful or the rpc response returns an error,
// error holds the error object. if it was an JSON-RPC error it can be casted
// to *RPCError.
//
CallFor(out interface{}, method string, params ...interface{}) error
}
RPCClient sends JSON-RPC requests over HTTP to the provided JSON-RPC backend.
RPCClient is created using the factory function NewClient().
func NewClient ¶
NewClient returns a new RPCClient instance with default configuration.
endpoint: JSON-RPC service URL to which JSON-RPC requests are sent.
func NewClientWithOpts ¶
func NewClientWithOpts(endpoint string, opts *RPCClientOpts) RPCClient
NewClientWithOpts returns a new RPCClient instance with custom configuration.
endpoint: JSON-RPC service URL to which JSON-RPC requests are sent.
opts: RPCClientOpts provide custom configuration
type RPCClientOpts ¶
RPCClientOpts can be provided to NewClientWithOpts() to change configuration of RPCClient.
HttpClient: provide a custom http.Client (e.g. to set a proxy, or tls options)
CustomHeaders: provide custom headers, e.g. to set BasicAuth
type RPCError ¶
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
RPCError represents a JSON-RPC error object if an RPC error occurred.
Code: holds the error code
Message: holds a short error message
Data: holds additional error data, may be nil
type RPCRequest ¶
type RPCRequest struct {
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
ID uint `json:"id,omitempty"`
JSONRPC string `json:"jsonrpc"`
}
RPCRequest represents a JSON-RPC request object.
Method: string containing the method to be invoked
Params: can be nil. if not must be an json array or object
ID: may always set to 1 for single requests. Should be unique for every request in one batch request.
JSONRPC: must always be set to "2.0" for JSON-RPC version 2.0
type RPCResponse ¶
type RPCResponse struct {
JSONRPC string `json:"jsonrpc"`
Result interface{} `json:"result,omitempty"`
Error *RPCError `json:"error,omitempty"`
ID uint `json:"id"`
}
RPCResponse represents a JSON-RPC response object.
Result: holds the result of the rpc call if no error occurred, nil otherwise. can be nil even on success.
Error: holds an RPCError object if an error occurred. must be nil on success.
ID: may always be 1 for single requests. should be unique for every request in one batch request.
JSONRPC: must always be set to "2.0" for JSON-RPC version 2.0
See: http://www.jsonrpc.org/specification#response_object
func (*RPCResponse) GetBool ¶
func (RPCResponse *RPCResponse) GetBool() (bool, error)
GetBool converts the rpc response to a bool and returns it.
If result was not a bool an error is returned.
func (*RPCResponse) GetFloat ¶
func (RPCResponse *RPCResponse) GetFloat() (float64, error)
GetFloat converts the rpc response to float64 and returns it.
If result was not an float64 an error is returned.
func (*RPCResponse) GetInt ¶
func (RPCResponse *RPCResponse) GetInt() (int64, error)
GetIn converts the rpc response to an int64 and returns it.
If result was not an integer an error is returned.
func (*RPCResponse) GetObject ¶
func (RPCResponse *RPCResponse) GetObject(toType interface{}) error
GetObject converts the rpc response to an arbitrary type.
The function works as you would expect it from json.Unmarshal()
func (*RPCResponse) GetString ¶
func (RPCResponse *RPCResponse) GetString() (string, error)
GetString converts the rpc response to a string and returns it.
If result was not a string an error is returned.