suirpc

package
v0.0.24 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package suirpc: RPC client implementation with HTTP transport and debug capabilities Provides generic RPC request sending with automatic JSON marshaling and response handling Supports client configuration, timeout settings, and debug mode logging Built on resty HTTP client with connection pooling and retry mechanisms

suirpc: 带有 HTTP 传输和调试功能的 RPC 客户端实现 提供具有自动 JSON 序列化和响应处理的通用 RPC 请求发送 支持客户端配置、超时设置和调试模式日志记录 基于 resty HTTP 客户端构建,包含连接池和重试机制

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetClient added in v0.0.6

func SetClient(client *resty.Client)

SetClient allows custom HTTP client configuration Accepts pre-configured resty client instance Uses sync.Once to prevent multiple client assignments

SetClient 允许自定义 HTTP 客户端配置 接受预配置的 resty 客户端实例 使用 sync.Once 防止多次客户端分配

func SetDebugMode added in v0.0.6

func SetDebugMode(enable bool)

SetDebugMode enables or disables debug logging When enabled, logs request and response details Accepts boolean flag to control debug state

SetDebugMode 启用或禁用调试日志 启用时记录请求和响应详细信息 接受布尔标志来控制调试状态

Types

type RpcError

type RpcError struct {
	Code    int    `json:"code"`           // Error code identifying error type // 标识错误类型的错误代码
	Message string `json:"message"`        // Human-readable error description // 人类可读的错误描述
	Data    any    `json:"data,omitempty"` // Additional error information // 附加错误信息
}

RpcError represents JSON-RPC 2.0 error information Contains error code, message, and optional data Implements error interface to work with Go error handling Follows standard JSON-RPC error object format

RpcError 表示 JSON-RPC 2.0 错误信息 包含错误代码、消息和可选数据 实现 error 接口以配合 Go 错误处理 遵循标准 JSON-RPC 错误对象格式

func (*RpcError) Error

func (rpcError *RpcError) Error() string

Error implements error interface to return formatted error description Returns string with code, message, and data fields Enables RpcError to work seamlessly with Go error handling

Error 实现 error 接口以返回格式化的错误描述 返回包含代码、消息和数据字段的字符串 使 RpcError 能够与 Go 错误处理无缝配合

type RpcRequest

type RpcRequest struct {
	Jsonrpc string `json:"jsonrpc"` // JSON-RPC version (always "2.0") // JSON-RPC 版本(始终为 "2.0")
	Method  string `json:"method"`  // RPC method name to invoke // 要调用的 RPC 方法名称
	Params  []any  `json:"params"`  // Method parameters as generic slice // 作为通用切片的方法参数
	ID      int    `json:"id"`      // Request identifier to match responses // 请求标识符以匹配响应
}

RpcRequest represents JSON-RPC 2.0 request structure Contains method name, parameters, and request identifier Follows standard JSON-RPC specification with version field Used to build blockchain RPC method calls

RpcRequest 表示 JSON-RPC 2.0 请求结构 包含方法名称、参数和请求标识符 遵循标准 JSON-RPC 规范,包含版本字段 用于构建区块链 RPC 方法调用

type RpcResponse

type RpcResponse[RES any] struct {
	Jsonrpc string    `json:"jsonrpc"`         // JSON-RPC version (always "2.0") // JSON-RPC 版本(始终为 "2.0")
	ID      int       `json:"id"`              // Request identifier matching request // 匹配请求的请求标识符
	Result  RES       `json:"result"`          // Response result with generic type // 带有通用类型的响应结果
	Error   *RpcError `json:"error,omitempty"` // Error information if call failed // 调用失败时的错误信息
}

RpcResponse represents JSON-RPC 2.0 response structure with generic result type Contains result data typed as RES or error information if call failed Generic type parameter enables type-safe response deserialization Matches request ID to correlate responses with requests

RpcResponse 表示带有通用结果类型的 JSON-RPC 2.0 响应结构 包含类型为 RES 的结果数据,如果调用失败则包含错误信息 通用类型参数支持类型安全的响应反序列化 匹配请求 ID 以关联响应与请求

func SendRpc

func SendRpc[RES any](ctx context.Context, serverUrl string, request *RpcRequest) (rpcResponse *RpcResponse[RES], err error)

SendRpc sends RPC request and deserializes response into generic type Accepts context, server URL, and RPC request structure Returns typed RPC response or error if request fails Supports debug logging when debug mode is enabled

SendRpc 发送 RPC 请求并将响应反序列化为通用类型 接受上下文、服务器 URL 和 RPC 请求结构 返回类型化的 RPC 响应,如果请求失败则返回错误 启用调试模式时支持调试日志记录

Jump to

Keyboard shortcuts

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