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
- type Client
- type ClientOption
- type Error
- type Request
- func NewRequest(method string, params ...interface{}) (req *Request)
- func NewRequestWithNamedParams(method string, params map[string]interface{}) (req *Request)
- func NewRequestWithObject(method string, params interface{}) (req *Request)
- func NewRequestWithRawParams(method string, params json.RawMessage) (req *Request)
- type RequestId
- type Response
- type WarpedError
Constants ¶
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.
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 ¶
Call executes a JSON-RPC request and returns the response. Returns an error if the request fails or if the response contains an error.
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 ¶
NewRequest creates a new JSON-RPC 2.0 request with positional parameters.
func NewRequestWithNamedParams ¶
NewRequestWithNamedParams creates a new JSON-RPC 2.0 request with named parameters.
func NewRequestWithObject ¶
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 ¶
MarshalJSON implements custom JSON marshaling for JSON-RPC 2.0 format.
func (*Request) SetNamedParam ¶
SetNamedParam sets a named parameter.
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 ¶
MarshalJSON serializes the ID as a number (JSON-RPC standard).
func (*RequestId) UnmarshalJSON ¶
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 (*Response) ParseError ¶
ParseError converts the response error to a Go error. Returns nil if there is no error.
func (*Response) ParseResult ¶
ParseResult unmarshals the result into the target object.
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.