jsonrpc

package
v0.0.0-...-07b51e5 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2021 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilResponse       = errors.New("nil response")
	ErrUnauthorized      = errors.New("unauthorized")
	ErrBadRequest        = errors.New("bad request")
	ErrHTTPError         = errors.New("http error")
	ErrConnectionRefused = errors.New("connection refused")
)

Functions

This section is empty.

Types

type Client

type Client interface {
	CallParamArray(ctx context.Context, method string, additionalHeaders map[string]string, params ...interface{}) (*RPCResponse, error)
	CallParamStruct(ctx context.Context, method string, additionalHeaders map[string]string, params interface{}) (*RPCResponse, error)
}

type HTTPClient

type HTTPClient struct {
	// contains filtered or unexported fields
}

func NewHTTPClient

func NewHTTPClient(
	urlEndpoint string,
) *HTTPClient

func NewHTTPClientFromOpts

func NewHTTPClientFromOpts(
	endpoint string,
	opts RPCClientOpts,
) *HTTPClient

func (*HTTPClient) CallParamArray

func (c *HTTPClient) CallParamArray(ctx context.Context, method string, additionalHeaders map[string]string, params ...interface{}) (*RPCResponse, error)

func (*HTTPClient) CallParamStruct

func (c *HTTPClient) CallParamStruct(ctx context.Context, method string, additionalHeaders map[string]string, params interface{}) (*RPCResponse, error)

type HTTPError

type HTTPError struct {
	Code int
	// contains filtered or unexported fields
}

HTTPError represents a error that occurred on HTTP level.

An error of type HTTPError is returned when a HTTP error occurred (status code) and the body could not be parsed to a valid RPCResponse object that holds a RPCError.

Otherwise a RPCResponse object is returned with a RPCError field that is not nil.

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error function is provided to be used as error object.

type MockClient

type MockClient struct {
	T                              *testing.T
	CallParamArrayFunc             func(t *testing.T, m *MockClient, ctx context.Context, method string, additionalHeaders map[string]string, params ...interface{}) (*RPCResponse, error)
	CallParamArrayFuncInvocations  int
	CallParamStructFunc            func(t *testing.T, m *MockClient, ctx context.Context, method string, additionalHeaders map[string]string, params interface{}) (*RPCResponse, error)
	CallParamStructFuncInvocations int
}

func (*MockClient) CallParamArray

func (m *MockClient) CallParamArray(ctx context.Context, method string, additionalHeaders map[string]string, params ...interface{}) (*RPCResponse, error)

func (*MockClient) CallParamStruct

func (m *MockClient) CallParamStruct(ctx context.Context, method string, additionalHeaders map[string]string, params interface{}) (*RPCResponse, error)

type RPCClientOpts

type RPCClientOpts struct {
	HTTPClient    *http.Client
	CustomHeaders map[string]string
}

RPCClientOpts can be provided to NewClientWithOpts() to change configuration of HTTPClient. 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: holds the error code
	Code int `json:"code"`

	// Message: holds a short error message
	Message string `json:"message"`

	// Data: holds additional error data, may be nil
	Data interface{} `json:"data,omitempty"`
}

RPCError represents a JSON-RPC error object if an RPC error occurred. See: http://www.jsonrpc.org/specification#error_object

func (*RPCError) Error

func (e *RPCError) Error() string

Error function is provided to be used as error object.

type RPCRequest

type RPCRequest struct {
	// Method: string containing the method to be invoked
	Method string `json:"method"`

	// Params: can be nil. if not must be an json array or object
	Params interface{} `json:"params,omitempty"`

	// ID: may always set to 1 for single requests.
	// Should be unique for every request in a batch request.
	ID int `json:"id"`

	// JSONRPC: must always be set to "2.0" for JSON-RPC version 2.0
	JSONRPC string `json:"jsonrpc"`
}

RPCRequest represents a JSON-RPC request object. See: http://www.jsonrpc.org/specification#request_object

type RPCResponse

type RPCResponse struct {
	// JSONRPC: must always be set to "2.0" for JSON-RPC version 2.0
	JSONRPC string `json:"jsonrpc"`

	// Result: holds the result of the rpc call if no error occurred, nil otherwise.
	Result json.RawMessage `json:"result,omitempty"`

	// Error: holds an RPCError object if an error occurred. must be nil on success.
	Error *RPCError `json:"error,omitempty"`

	// ID: may always be 0 for single requests, should be unique for batch requests.
	ID int `json:"id"`
}

RPCResponse represents a JSON-RPC response object. See: http://www.jsonrpc.org/specification#response_object

func (*RPCResponse) GetObject

func (RPCResponse *RPCResponse) GetObject(target interface{}) error

GetObject converts the rpc response to an arbitrary type. The function works as you would expect it from json.Unmarshal()

Jump to

Keyboard shortcuts

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