urpc

package
v0.0.0-...-ea2125a Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package urpc provides a universal RPC client supporting JSON-RPC 2.0 over HTTP and IPC. It abstracts the transport layer, allowing the same client API for different protocols.

Index

Constants

View Source
const (
	JSON_RPC_VERSION_1_0 = "1.0" // JSON-RPC 1.0 (legacy)
	JSON_RPC_VERSION_2_0 = "2.0" // JSON-RPC 2.0 (standard)
)

JSON-RPC version constants.

View Source
const (
	// Parse error: Invalid JSON was received.
	ERROR_CODE_PARSE_ERROR    = -32700
	ERROR_MESSAGE_PARSE_ERROR = "Parse error"

	// Invalid Request: The JSON sent is not a valid Request object.
	ERROR_CODE_INVALID_REQUEST    = -32600
	ERROR_MESSAGE_INVALID_REQUEST = "invalid request"

	// Method not found: The method does not exist.
	ERROR_CODE_METHOD_NOT_FOUND    = -32601
	ERROR_MESSAGE_METHOD_NOT_FOUND = "method not found"

	// Server error: Reserved for implementation-defined server errors.
	ERROR_CODE_SERVER_ERROR    = -32000
	ERROR_MESSAGE_SERVER_ERROR = "server error"
)

Standard JSON-RPC 2.0 error codes and messages.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is the universal RPC client that supports multiple transports. It can use HTTP-RPC, IPC sockets, or REST depending on configuration.

func NewClient

func NewClient(options ...ClientOption) *Client

NewClient creates a new universal RPC client with the specified options. Options determine the transport type (HTTP-RPC, IPC socket, or REST).

func (*Client) Call

func (c *Client) Call(request *Request) (response *Response, err error)

Call executes a JSON-RPC request and returns the response. Returns an error if the request fails or if the response contains an error.

func (*Client) Get

func (c *Client) Get(method string, params map[string]interface{}, response interface{}) (err error)

Get performs a REST GET request to the specified method/endpoint.

func (*Client) Post

func (c *Client) Post(method string, params interface{}, response interface{}) (err error)

Post performs a REST POST request to the specified method/endpoint.

type ClientOption

type ClientOption func(client *Client)

ClientOption is a function that configures a Client.

func WithHTTPRest

func WithHTTPRest(url string, headers map[string]string) ClientOption

WithHTTPRest sets the URL of the REST server for http network requests This is used for RESTful like API requests for TRON and similar networks

func WithHTTPRpc

func WithHTTPRpc(url string, headers map[string]string) ClientOption

WithHTTPRpc sets the URL of the RPC server for http network requests

func WithRpcIPCSocket

func WithRpcIPCSocket(socketPath string) ClientOption

WithRpcIPCSocket sets the RPC client for use unix socket interactions Please note, may not work on Windows (differences between Unix sockets and Windows named pipes)

type Error

type Error struct {
	Code    int    `json:"code"`    // Error code
	Message string `json:"message"` // Error message
}

Error represents a JSON-RPC 2.0 error object.

type Request

type Request struct {
	Id           RequestId              `json:"id"`
	JsonRpc      string                 `json:"jsonrpc"`
	Method       string                 `json:"method"`
	ParamsNamed  map[string]interface{} `json:"paramsN,omitempty"`
	ParamsArray  []interface{}          `json:"paramsA,omitempty"`
	Params       json.RawMessage        `json:"params,omitempty"`
	ParamsObject interface{}            `json:"paramsO,omitempty"`
	// contains filtered or unexported fields
}

Request represents a JSON-RPC 2.0 request.

func NewRequest

func NewRequest(method string, params ...interface{}) (req *Request)

NewRequest creates a new JSON-RPC 2.0 request with positional parameters.

func NewRequestWithNamedParams

func NewRequestWithNamedParams(method string, params map[string]interface{}) (req *Request)

NewRequestWithNamedParams creates a new JSON-RPC 2.0 request with named parameters.

func NewRequestWithObject

func NewRequestWithObject(method string, params interface{}) (req *Request)

NewRequestWithObject creates a new JSON-RPC 2.0 request with parameters from a struct. The params object is marshaled to JSON.

func NewRequestWithRawParams

func NewRequestWithRawParams(method string, params json.RawMessage) (req *Request)

NewRequestWithRawParams creates a new JSON-RPC 2.0 request with raw JSON parameters.

func (*Request) AddParams

func (r *Request) AddParams(values ...interface{})

AddParams appends positional parameters.

func (*Request) MarshalJSON

func (r *Request) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling for JSON-RPC 2.0 format.

func (*Request) SetId

func (r *Request) SetId(id RequestId)

SetId sets the request ID.

func (*Request) SetNamedParam

func (r *Request) SetNamedParam(key string, value interface{})

SetNamedParam sets a named parameter.

func (*Request) SetParams

func (r *Request) SetParams(values ...interface{})

SetParams replaces all positional parameters.

func (*Request) String

func (r *Request) String() string

String returns a pretty-printed JSON representation of the request.

type RequestId

type RequestId string

RequestId is a JSON-RPC request identifier. Stored as string but marshaled as numeric for JSON-RPC compatibility.

func (RequestId) MarshalJSON

func (id RequestId) MarshalJSON() ([]byte, error)

MarshalJSON serializes the ID as a number (JSON-RPC standard).

func (RequestId) String

func (id RequestId) String() string

String returns the string representation of the request ID.

func (*RequestId) UnmarshalJSON

func (id *RequestId) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes a numeric ID, stripping quotes and leading zeros.

type Response

type Response struct {
	Id      RequestId       `json:"id"`               // Request ID (echoed back)
	JsonRpc string          `json:"jsonrpc"`          // JSON-RPC version
	Error   *Error          `json:"error,omitempty"`  // Error object (nil on success)
	Result  json.RawMessage `json:"result,omitempty"` // Result data (raw JSON)
}

Response represents a JSON-RPC 2.0 response.

func NewResponse

func NewResponse() *Response

NewResponse creates a new JSON-RPC 2.0 response.

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess returns true if the response contains no error.

func (*Response) ParseError

func (r *Response) ParseError() error

ParseError converts the response error to a Go error. Returns nil if there is no error.

func (*Response) ParseResult

func (r *Response) ParseResult(target interface{}) (err error)

ParseResult unmarshals the result into the target object.

func (*Response) String

func (r *Response) String() string

String returns a pretty-printed JSON representation of the response.

type WarpedError

type WarpedError struct {
	Code    int    `json:"code"`    // JSON-RPC error code
	Message string `json:"message"` // Error message
}

WarpedError wraps a JSON-RPC error as a Go error. Implements the error interface.

func (*WarpedError) Error

func (err *WarpedError) Error() string

Error returns the error message, implementing the error interface.

Jump to

Keyboard shortcuts

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