jsonrpc

package
v0.0.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

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

func NewRPCClient(endpoint string) *RPCClient

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.

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.

func (*RPCClient) Call2

func (client *RPCClient) Call2(method string, result interface{}, args ...interface{}) error

Call2 .

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.

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

func (client *RPCClient) Notification(method string, params ...interface{}) error

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

func (client *RPCClient) SetAutoIncrementID(flag bool)

SetAutoIncrementID if set to true, the id field of an rpcjson request will be incremented automatically

func (*RPCClient) SetBasicAuth

func (client *RPCClient) SetBasicAuth(username string, password string)

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

func (client *RPCClient) SetCustomHeader(key string, value string)

SetCustomHeader is used to set a custom header for each rpc request. You could for example set the Authorization Bearer here.

func (*RPCClient) SetHTTPClient

func (client *RPCClient) SetHTTPClient(httpClient *http.Client)

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) SetNextID

func (client *RPCClient) SetNextID(id uint)

SetNextID can be used to manually set the next id / reset the id.

func (*RPCClient) UnsetCustomHeader

func (client *RPCClient) UnsetCustomHeader(key string)

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.

See: http://www.jsonrpc.org/specification#error_object

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.

See: http://www.jsonrpc.org/specification#notification

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.

See: http://www.jsonrpc.org/specification#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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL