jsonrpc

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RPCVersion 默认 JSON-RPC 版本
	RPCVersion = "2.0"
)

Variables

This section is empty.

Functions

func Params

func Params(params ...any) any

Params 构建请求参数

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

func NewRPCClient(endpoint string, opts ...RPCOption) RPCClient

NewRPCClient 新建通用 JSON-RPC 客户端

type RPCError

type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

RPCError 通用 JSON-RPC 错误

func (*RPCError) Error

func (e *RPCError) Error() string

Error 实现 Error 方法

func (*RPCError) String

func (e *RPCError) String() string

String 实现 String 方法

type RPCOption

type RPCOption func(server *rpcClient)

RPCOption JSON-RPC 客户端可选配置

func WithCustomHeaders

func WithCustomHeaders(hds map[string]string) RPCOption

WithCustomHeaders 使用配置的 HTTP 请求头

func WithDefaultRequestID

func WithDefaultRequestID(id int) RPCOption

WithDefaultRequestID 使用配置的默认请求ID

func WithHTTPClient

func WithHTTPClient(hc *http.Client) RPCOption

WithHTTPClient 使用配置的 HTTP 客户端

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 RPCRequests

type RPCRequests []*RPCRequest

RPCRequests 通用 JSON-RPC 请求体列表

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 判断响应体列表中是否有响应体发生了错误

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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