Documentation ¶
Overview ¶
Package jsonrpc provides an jsonrpc 2.0 client that sends jsonrpc requests and receives jsonrpc responses using http.
Example ¶
type Person struct { Name string `json:"name"` Age int `json:"age"` Country string `json:"country"` } // create client rpcClient := NewRPCClient("http://my-rpc-service") // execute rpc to service response, _ := rpcClient.Call("getPersonByID", 12345) // parse result into struct var person Person response.GetObject(&person) // change result and send back using rpc person.Age = 35 rpcClient.Call("setPersonByID", 12345, person)
Output:
Index ¶
- type BatchResponse
- type RPCClient
- func (client *RPCClient) Batch(requests ...interface{}) (*BatchResponse, error)
- func (client *RPCClient) Call(method string, params ...interface{}) (*RPCResponse, error)
- func (client *RPCClient) CallNamed(method string, params map[string]interface{}) (*RPCResponse, error)
- func (client *RPCClient) NewRPCNotificationObject(method string, params ...interface{}) *RPCNotification
- func (client *RPCClient) NewRPCRequestObject(method string, params ...interface{}) *RPCRequest
- func (client *RPCClient) Notification(method string, params ...interface{}) error
- func (client *RPCClient) SetAutoIncrementID(flag bool)
- func (client *RPCClient) SetBasicAuth(username string, password string)
- func (client *RPCClient) SetCustomHeader(key string, value string)
- func (client *RPCClient) SetHTTPClient(httpClient *http.Client)
- func (client *RPCClient) SetNextID(id uint)
- func (client *RPCClient) UnsetCustomHeader(key string)
- func (client *RPCClient) UpdateRequestID(rpcRequest *RPCRequest)
- type RPCError
- type RPCNotification
- type RPCRequest
- type RPCResponse
- func (rpcResponse *RPCResponse) GetBool() (bool, error)
- func (rpcResponse *RPCResponse) GetFloat64() (float64, error)
- func (rpcResponse *RPCResponse) GetInt() (int, error)
- func (rpcResponse *RPCResponse) GetInt64() (int64, error)
- func (rpcResponse *RPCResponse) GetObject(toType interface{}) error
- func (rpcResponse *RPCResponse) GetString() (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BatchResponse ¶
type BatchResponse struct {
// contains filtered or unexported fields
}
BatchResponse a list of jsonrpc response objects as a result of a batch request
if you are interested in the response of a specific request use: GetResponseOf(request)
func (*BatchResponse) GetResponseOf ¶
func (batchResponse *BatchResponse) GetResponseOf(request *RPCRequest) (*RPCResponse, error)
GetResponseOf returns the rpc response of the corresponding request by matching the id.
For this method to work, autoincrementID should be set to true (default).
type RPCClient ¶
type RPCClient struct {
// contains filtered or unexported fields
}
RPCClient sends jsonrpc requests over http to the provided rpc backend. RPCClient is created using the factory function NewRPCClient().
func NewRPCClient ¶
NewRPCClient returns a new RPCClient instance with default configuration (no custom headers, default http.Client, autoincrement ids). Endpoint is the rpc-service url to which the rpc requests are sent.
func (*RPCClient) Batch ¶
func (client *RPCClient) Batch(requests ...interface{}) (*BatchResponse, error)
Batch sends a jsonrpc batch request to the rpc-service. The parameter is a list of requests the could be one of:
RPCRequest RPCNotification.
The batch requests returns a list of RPCResponse structs.
Example ¶
rpcClient := NewRPCClient(httpServer.URL) req1 := rpcClient.NewRPCRequestObject("addNumbers", 1, 2, 3) req2 := rpcClient.NewRPCRequestObject("getTimestamp") responses, _ := rpcClient.Batch(req1, req2) response, _ := responses.GetResponseOf(req2) timestamp, _ := response.GetInt() fmt.Println(timestamp)
Output:
func (*RPCClient) Call ¶
func (client *RPCClient) Call(method string, params ...interface{}) (*RPCResponse, error)
Call sends an jsonrpc request over http to the rpc-service url that was provided on client creation.
If something went wrong on the network / http level or if json parsing failed it returns an error.
If something went wrong on the rpc-service / protocol level the Error field of the returned RPCResponse is set and contains information about the error.
If the request was successful the Error field is nil and the Result field of the RPCRespnse struct contains the rpc result.
Example ¶
rpcClient := NewRPCClient("http://my-rpc-service") // result processing omitted, see: RPCResponse methods rpcClient.Call("getTimestamp") rpcClient.Call("getPerson", 1234) rpcClient.Call("addNumbers", 5, 2, 3) rpcClient.Call("strangeFunction", 1, true, "alex", 3.4) type Person struct { Name string `json:"name"` Age int `json:"age"` Country string `json:"country"` } person := Person{ Name: "alex", Age: 33, Country: "germany", } rpcClient.Call("setPersonByID", 123, person)
Output:
func (*RPCClient) CallNamed ¶
func (client *RPCClient) CallNamed(method string, params map[string]interface{}) (*RPCResponse, error)
CallNamed sends an jsonrpc request over http to the rpc-service url that was provided on client creation. This differs from Call() by sending named, rather than positional, arguments.
If something went wrong on the network / http level or if json parsing failed it returns an error.
If something went wrong on the rpc-service / protocol level the Error field of the returned RPCResponse is set and contains information about the error.
If the request was successful the Error field is nil and the Result field of the RPCRespnse struct contains the rpc result.
Example ¶
rpcClient := NewRPCClient("http://my-rpc-service") // result processing omitted, see: RPCResponse methods rpcClient.CallNamed("createPerson", map[string]interface{}{ "name": "Bartholomew Allen", "nicknames": []string{"Barry", "Flash"}, "male": true, "age": 28, "address": map[string]interface{}{"street": "Main Street", "city": "Central City"}, })
Output:
func (*RPCClient) NewRPCNotificationObject ¶
func (client *RPCClient) NewRPCNotificationObject(method string, params ...interface{}) *RPCNotification
NewRPCNotificationObject creates and returns a raw RPCNotification structure. It is mainly used when building batch requests. For single notifications use RPCClient.Notification(). NewRPCNotificationObject struct can also be created directly, but this function sets the ID and the jsonrpc field to the correct values.
func (*RPCClient) NewRPCRequestObject ¶
func (client *RPCClient) NewRPCRequestObject(method string, params ...interface{}) *RPCRequest
NewRPCRequestObject creates and returns a raw RPCRequest structure. It is mainly used when building batch requests. For single requests use RPCClient.Call(). RPCRequest struct can also be created directly, but this function sets the ID and the jsonrpc field to the correct values.
func (*RPCClient) Notification ¶
Notification sends a jsonrpc request to the rpc-service. The difference to Call() is that this request does not expect a response. The ID field of the request is omitted.
func (*RPCClient) SetAutoIncrementID ¶
SetAutoIncrementID if set to true, the id field of an rpcjson request will be incremented automatically
func (*RPCClient) SetBasicAuth ¶
SetBasicAuth is a helper function that sets the header for the given basic authentication credentials. To reset / disable authentication just set username or password to an empty string value.
func (*RPCClient) SetCustomHeader ¶
SetCustomHeader is used to set a custom header for each rpc request. You could for example set the Authorization Bearer here.
func (*RPCClient) SetHTTPClient ¶
SetHTTPClient can be used to set a custom http.Client. This can be useful for example if you want to customize the http.Client behaviour (e.g. proxy settings)
func (*RPCClient) UnsetCustomHeader ¶
UnsetCustomHeader is used to removes a custom header that was added before.
func (*RPCClient) UpdateRequestID ¶
func (client *RPCClient) UpdateRequestID(rpcRequest *RPCRequest)
UpdateRequestID updates the ID of an RPCRequest structure.
This is used if a request is sent another time and the request should get an updated id.
This does only make sense when used on with Batch() since Call() and Notififcation() do update the id automatically.
type RPCError ¶
type RPCError struct { Code int `json:"code"` Message string `json:"message"` Data interface{} `json:"data"` }
RPCError represents a jsonrpc error object if an rpc error occurred.
type RPCNotification ¶
type RPCNotification struct { JSONRPC string `json:"jsonrpc"` Method string `json:"method"` Params interface{} `json:"params,omitempty"` }
RPCNotification represents a jsonrpc notification object. A notification object omits the id field since there will be no server response.
type RPCRequest ¶
type RPCRequest struct { JSONRPC string `json:"jsonrpc"` Method string `json:"method"` Params interface{} `json:"params,omitempty"` ID uint `json:"id"` }
RPCRequest represents a jsonrpc request object.
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 jsonrpc response object. If no rpc specific error occurred Error field is nil.
See: http://www.jsonrpc.org/specification#response_object
Example ¶
rpcClient := NewRPCClient("http://my-rpc-service") response, _ := rpcClient.Call("addNumbers", 1, 2, 3) sum, _ := response.GetInt() fmt.Println(sum) response, _ = rpcClient.Call("isValidEmail", "my@ema.il") valid, _ := response.GetBool() fmt.Println(valid) response, _ = rpcClient.Call("getJoke") joke, _ := response.GetString() fmt.Println(joke) response, _ = rpcClient.Call("getPi", 10) piRounded, _ := response.GetFloat64() fmt.Println(piRounded) var rndNumbers []int response, _ = rpcClient.Call("getRndIntNumbers", 10) response.GetObject(&rndNumbers) fmt.Println(rndNumbers[0]) type Person struct { Name string `json:"name"` Age int `json:"age"` Country string `json:"country"` } var p Person response, _ = rpcClient.Call("getPersonByID", 1234) response.GetObject(&p) fmt.Println(p.Name)
Output:
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) GetFloat64 ¶
func (rpcResponse *RPCResponse) GetFloat64() (float64, error)
GetFloat64 converts the rpc response to an float64 and returns it.
If result was not an float64 an error is returned.
func (*RPCResponse) GetInt ¶
func (rpcResponse *RPCResponse) GetInt() (int, error)
GetInt converts the rpc response to an int and returns it.
This is a convenient function. Int could be 32 or 64 bit, depending on the architecture the code is running on. For a deterministic result use GetInt64().
If result was not an integer an error is returned.
func (*RPCResponse) GetInt64 ¶
func (rpcResponse *RPCResponse) GetInt64() (int64, error)
GetInt64 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 object (e.g. a struct) and returns it. The parameter should be a structure that can hold the data of the response object.
For example if the following json return value is expected: {"name": "alex", age: 33, "country": "Germany"} the struct should look like
type Person struct { Name string Age int Country string }
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.