api

package
v0.0.0-...-a2ce4e4 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HttpClient = iota
	GrpcClient
	WsClient
)
View Source
const (
	Success = 0
	// ParseError begin for json-rpc 2.0
	ParseError      = -32700
	InvalidRequest  = -32600
	MethodNotFound  = -32601
	InvalidParams   = -32602
	InternalError   = -32603
	ReserveMinError = -32099
	ReserveMaxError = -32000
)
View Source
const (
	JsonRpcNotifierKey = "_JsonRpcNotifierKey_"
	RawWSNotifierKey   = "_RawWSNotifierKey_"
)
View Source
const (
	BuiltInPathJsonRPC   = "_jsonrpc_"
	BuiltInPathWsJsonRPC = "_jsonrpc_ws_"
	BuiltInPathRawWS     = "_raw_ws_"

	BuiltInPathDIR     = "_dir_"
	BuiltInPathMetrics = "_metrics_"
)
View Source
const (
	Version = "2.0"
)

Variables

View Source
var SysCodeMap = map[int]string{
	ParseError:     "Parse error",
	InvalidRequest: "Invalid request",
	MethodNotFound: "Method not found",
	InvalidParams:  "Invalid params",
	InternalError:  "Internal error",
}

Functions

This section is empty.

Types

type BasicAuth

type BasicAuth struct {
	User   string
	Passwd string
}

type BatchElem

type BatchElem struct {
	Method string
	Args   interface{}
	// The result is unmarshalled into this field. Result must be set to a
	// non-nil pointer value of the desired type, otherwise the response will be
	// discarded.
	Result interface{}
	// Error is set if the server returns an error for this request, or if
	// unmarshalling into Result fails. It is not set for I/O errors.
	Error error
}

BatchElem is an element in a batch request.

type Client

type Client interface {
	GetType() ClientType
	CallJsonRpc(result interface{}, method string, args interface{}) error
	BatchCallJsonRpc(b []BatchElem) error
	RawCallHttp(method string, path string, body interface{}) (int, io.ReadCloser, error)
	Close() error

	// The WriteByWs ending with "WS" are only intended for WebSocket clients
	WriteByWs(WSMessage) error
	ErrFromWS() chan error

	// The UnderlyingHandle is only intended for grpc clients now
	UnderlyingHandle() interface{}
}

type ClientAdapter

type ClientAdapter struct{}

func (ClientAdapter) BatchCallJsonRpc

func (c ClientAdapter) BatchCallJsonRpc(b []BatchElem) error

func (ClientAdapter) CallJsonRpc

func (c ClientAdapter) CallJsonRpc(result interface{}, method string, args interface{}) error

func (ClientAdapter) Close

func (c ClientAdapter) Close() error

func (ClientAdapter) ErrFromWS

func (c ClientAdapter) ErrFromWS() chan error

func (ClientAdapter) GetType

func (c ClientAdapter) GetType() ClientType

func (ClientAdapter) RawCallHttp

func (c ClientAdapter) RawCallHttp(method string, path string, body interface{}) (int, io.ReadCloser, error)

func (ClientAdapter) UnderlyingHandle

func (c ClientAdapter) UnderlyingHandle() interface{}

func (ClientAdapter) WriteByWs

func (c ClientAdapter) WriteByWs(WSMessage) error

type ClientOption

type ClientOption struct {
	JsonRpcOpt *JsonRpcOption

	// http auth
	Basic *BasicAuth

	// websocket
	RevChan interface{}
}

type ClientType

type ClientType int

type Context

type Context interface {
	CtxID() interface{}
	ClientIP() string
	WithValue(key string, value any)
	Value(key string) (any, bool)
}

type JsonRpcError

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

func FromError

func FromError(err error) (*JsonRpcError, bool)

func NewJsonRpcErrFromCode

func NewJsonRpcErrFromCode(c int, data interface{}) *JsonRpcError

func NewJsonRpcError

func NewJsonRpcError(c int, msg string, data interface{}) *JsonRpcError

func (*JsonRpcError) Error

func (j *JsonRpcError) Error() string

type JsonRpcNotifier

type JsonRpcNotifier interface {
	Notify(sub *SubscriptionNotice) error
	ID() string
	Err() chan error
}

type JsonRpcOption

type JsonRpcOption struct {
	Codec codec.CodecType
}

type JsonRpcRequest

type JsonRpcRequest struct {
	Version string `json:"jsonrpc"`
	Method  string `json:"method"`
	// Params should be json.RawMessage. However, in fact, since the params parameter in the request body is already
	// an interface after the data is deserialized into a map, if it is set to json.rawMessage,
	// essentially a conversion process is required. In other words, the type of the value in map after deserialization
	// is interface{}, and the interface{} cannot be forced to be converted to json.RawMessage
	Params interface{} `json:"params,omitempty"`
	Id     interface{} `json:"id"`
}

type JsonRpcResponse

type JsonRpcResponse struct {
	Version string          `json:"jsonrpc"`
	Id      interface{}     `json:"id"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *JsonRpcError   `json:"error,omitempty"`
}

func NewErrorJsonRpcResponse

func NewErrorJsonRpcResponse(id interface{}, code int, msg string, data interface{}) *JsonRpcResponse

func NewErrorJsonRpcResponseWithError

func NewErrorJsonRpcResponseWithError(id interface{}, err *JsonRpcError) *JsonRpcResponse

func NewSuccessJsonRpcResponse

func NewSuccessJsonRpcResponse(id interface{}, result interface{}) *JsonRpcResponse

type RawHttpContext

type RawHttpContext interface {
	Context
	Param(key string) string
	Query(key string) string
	WriteData(code int, contType string, data []byte) error
}

type RawHttpHandle

type RawHttpHandle func(RawHttpContext, []byte)

RawHttpHandle is a raw interface for creating api based http

type RawWSNotifier

type RawWSNotifier interface {
	Write(WSMessage) error
	ID() string
	Err() chan error
}

type RawWsHandle

type RawWsHandle func(Context, WSMessage) error

RawWsHandle is a raw interface for creating api based ws

type ServerOption

type ServerOption struct {
	Addr        string
	ClusterName string

	Logger log.Logger

	JsonRpcOpt *JsonRpcOption

	BeforeRun          func() error
	EnableInnerService bool
}

type SubscriptionNotice

type SubscriptionNotice struct {
	Version string             `json:"jsonrpc"`
	Method  string             `json:"method"`
	Params  SubscriptionResult `json:"params,omitempty"`
}

func NewSubscriptionNotice

func NewSubscriptionNotice(method string, id interface{}, result interface{}) *SubscriptionNotice

type SubscriptionResult

type SubscriptionResult struct {
	ID     interface{}     `json:"subscription"`
	Result json.RawMessage `json:"result,omitempty"`
}

type WSMessage

type WSMessage struct {
	Type WSMessageType
	Data []byte
}

type WSMessageType

type WSMessageType int
const (
	WSTextMessage   WSMessageType = 1
	WSBinaryMessage WSMessageType = 2
)

Jump to

Keyboard shortcuts

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