Documentation
¶
Index ¶
- Constants
- func Params(params ...any) any
- type RPCClient
- type RPCError
- type RPCOption
- type RPCRequest
- type RPCRequests
- type RPCResponse
- func (resp *RPCResponse) GetBool() (bool, error)
- func (resp *RPCResponse) GetFloat64() (float64, error)
- func (resp *RPCResponse) GetInt64() (int64, error)
- func (resp *RPCResponse) GetRPCError() *RPCError
- func (resp *RPCResponse) GetString() (string, error)
- func (resp RPCResponse) MarshalJSON() ([]byte, error)
- func (resp *RPCResponse) ReadToObject(to any) error
- func (resp *RPCResponse) UnmarshalJSON(data []byte) error
- type RPCResponses
Constants ¶
const (
// RPCVersion 默认 JSON-RPC 版本
RPCVersion = "2.0"
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type RPCClient ¶
type RPCClient interface {
// Call 进行 JSON-RPC 调用
Call(ctx context.Context, method string, params ...any) (*RPCResponse, error)
// CallRaw 基于所给请求体进行 JSON-RPC 调用
CallRaw(ctx context.Context, req *RPCRequest) (*RPCResponse, error)
// CallFor 进行 JSON-RPC 调用并将响应结果反序列化到所给类型对象中
CallFor(ctx context.Context, out any, method string, params ...any) error
// CallRawFor 基于所给请求体进行 JSON-RPC 调用并将响应结果反序列化到所给类型对象中
CallRawFor(ctx context.Context, out any, req *RPCRequest) error
// CallBatch 进行 JSON-RPC 批量调用(内部会自动设置 JSONRPC 与 ID 字段,ID 将从 1 开始递增)
CallBatch(ctx context.Context, reqs RPCRequests) (RPCResponses, error)
// CallBatchRaw 基于所给请求体列表进行 JSON-RPC 批量调用
CallBatchRaw(ctx context.Context, reqs RPCRequests) (RPCResponses, error)
// NewHTTPRequest 新建 HTTP 请求体(req 可以为 *RPCRequest 或 RPCRequests)
NewHTTPRequest(ctx context.Context, req any) (*http.Request, error)
// CallWithHTTPRequest 使用 http.Request 进行 JSON-RPC 调用
CallWithHTTPRequest(httpReq *http.Request) (*http.Response, *RPCResponse, error)
// CallBatchWithHTTPRequest 使用 http.Request 进行 JSON-RPC 批量调用
CallBatchWithHTTPRequest(httpReq *http.Request) (*http.Response, []*RPCResponse, error)
}
RPCClient 通用 JSON-RPC 客户端接口
规范:https://wiki.geekdream.com/Specification/json-rpc_2.0.html
func NewRPCClient ¶
NewRPCClient 新建通用 JSON-RPC 客户端
type RPCError ¶
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
RPCError 通用 JSON-RPC 错误
type RPCOption ¶
type RPCOption func(server *rpcClient)
RPCOption JSON-RPC 客户端可选配置
func WithCustomHeaders ¶
WithCustomHeaders 使用配置的 HTTP 请求头
func WithDefaultRequestID ¶
WithDefaultRequestID 使用配置的默认请求ID
type RPCRequest ¶
type RPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Method string `json:"method"`
Params any `json:"params,omitempty"`
Extensions map[string]any `json:"-"` // 规范定义之外的拓展字段
}
RPCRequest 通用 JSON-RPC 请求体
func NewRPCRequest ¶
func NewRPCRequest(method string, params ...any) *RPCRequest
NewRPCRequest 新建通用 JSON-RPC 请求体
func NewRPCRequestWithID ¶
func NewRPCRequestWithID(id int, method string, params ...any) *RPCRequest
NewRPCRequestWithID 使用指定 id 新建通用 JSON-RPC 请求体
func (RPCRequest) MarshalJSON ¶
func (req RPCRequest) MarshalJSON() ([]byte, error)
MarshalJSON 实现 json.Marshaler 接口的 MarshalJSON 方法
func (*RPCRequest) UnmarshalJSON ¶
func (req *RPCRequest) UnmarshalJSON(data []byte) error
UnmarshalJSON 实现 json.Unmarshaler 接口的 UnmarshalJSON 方法
func (*RPCRequest) WithExtensions ¶
func (req *RPCRequest) WithExtensions(exts map[string]any) *RPCRequest
WithExtensions 使用拓展字段
type RPCResponse ¶
type RPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Result any `json:"result,omitempty"`
Error any `json:"error,omitempty"`
Extensions map[string]any `json:"-"` // 规范定义之外的拓展字段
}
RPCResponse 通用 JSON-RPC 响应体
func (*RPCResponse) GetBool ¶
func (resp *RPCResponse) GetBool() (bool, error)
GetBool 获取响应结果的 bool 类型值
func (*RPCResponse) GetFloat64 ¶
func (resp *RPCResponse) GetFloat64() (float64, error)
GetFloat64 获取响应结果的 float64 类型值
func (*RPCResponse) GetInt64 ¶
func (resp *RPCResponse) GetInt64() (int64, error)
GetInt64 获取响应结果的 int64 类型值
func (*RPCResponse) GetRPCError ¶
func (resp *RPCResponse) GetRPCError() *RPCError
GetRPCError 获取通用 JSON-RPC 错误
func (*RPCResponse) GetString ¶
func (resp *RPCResponse) GetString() (string, error)
GetString 获取响应结果的 string 类型值
func (RPCResponse) MarshalJSON ¶
func (resp RPCResponse) MarshalJSON() ([]byte, error)
MarshalJSON 实现 json.Marshaler 接口的 MarshalJSON 方法
func (*RPCResponse) ReadToObject ¶
func (resp *RPCResponse) ReadToObject(to any) error
ReadToObject 将响应结果反序列化到所给类型对象中
func (*RPCResponse) UnmarshalJSON ¶
func (resp *RPCResponse) UnmarshalJSON(data []byte) error
UnmarshalJSON 实现 json.Unmarshaler 接口的 UnmarshalJSON 方法
type RPCResponses ¶
type RPCResponses []*RPCResponse
RPCResponses 通用 JSON-RPC 响应体列表
func (RPCResponses) AsMap ¶
func (resps RPCResponses) AsMap() map[int]*RPCResponse
AsMap 将响应体列表作为 map 返回,其中 key 为响应对应的 id
func (RPCResponses) GetByID ¶
func (resps RPCResponses) GetByID(id int) *RPCResponse
GetByID 返回给定 id 的响应体,如果响应体不存在,则返回 nil
func (RPCResponses) HasError ¶
func (resps RPCResponses) HasError() bool
HasError 判断响应体列表中是否有响应体发生了错误