Documentation
¶
Index ¶
- type Context
- type JSONRPCID
- type JSONRPCIntID
- type JSONRPCStringID
- type RPCError
- type RPCRequest
- type RPCRequests
- type RPCResponse
- func NewRPCErrorResponse(id JSONRPCID, code int, msg string, data string) RPCResponse
- func NewRPCSuccessResponse(id JSONRPCID, res any) RPCResponse
- func RPCInternalError(id JSONRPCID, err error) RPCResponse
- func RPCInvalidParamsError(id JSONRPCID, err error) RPCResponse
- func RPCInvalidRequestError(id JSONRPCID, err error) RPCResponse
- func RPCMethodNotFoundError(id JSONRPCID) RPCResponse
- func RPCParseError(id JSONRPCID, err error) RPCResponse
- type RPCResponses
- type WSRPCConnection
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { // json-rpc request JSONReq *RPCRequest // websocket connection WSConn WSRPCConnection // http request HTTPReq *http.Request }
Context is the first parameter for all functions. It carries a json-rpc request, http request and websocket connection.
- JSONReq is non-nil when JSONRPC is called over websocket or HTTP. - WSConn is non-nil when we're connected via a websocket. - HTTPReq is non-nil when URI or JSONRPC is called over HTTP.
func (*Context) Context ¶
Context returns the request's context. The returned context is always non-nil; it defaults to the background context. HTTP:
The context is canceled when the client's connection closes, the request is canceled (with HTTP/2), or when the ServeHTTP method returns.
WS:
The context is canceled when the client's connections closes.
func (*Context) RemoteAddr ¶
RemoteAddr returns the remote address (usually a string "IP:port"). If neither HTTPReq nor WSConn is set, an empty string is returned. HTTP:
http.Request#RemoteAddr
WS:
result of GetRemoteAddr
type JSONRPCID ¶
type JSONRPCID interface {
String() string
}
JSONRPCID is a wrapper type for JSON-RPC request IDs, which can be a string value | number value | not set (nil)
type JSONRPCIntID ¶
type JSONRPCIntID int
JSONRPCIntID a wrapper for JSON-RPC integer IDs
func (JSONRPCIntID) String ¶
func (id JSONRPCIntID) String() string
type JSONRPCStringID ¶
type JSONRPCStringID string
JSONRPCStringID a wrapper for JSON-RPC string IDs
func (JSONRPCStringID) String ¶
func (id JSONRPCStringID) String() string
type RPCError ¶
type RPCRequest ¶
type RPCRequest struct { JSONRPC string `json:"jsonrpc"` ID JSONRPCID `json:"id"` Method string `json:"method"` Params json.RawMessage `json:"params"` // must be map[string]interface{} or []interface{} }
func MapToRequest ¶
MapToRequest generates an RPC request with the given ID and method. The params are encoded as a JSON map
func NewRPCRequest ¶
func NewRPCRequest(id JSONRPCID, method string, params json.RawMessage) RPCRequest
func (RPCRequest) String ¶
func (request RPCRequest) String() string
func (*RPCRequest) UnmarshalJSON ¶
func (request *RPCRequest) UnmarshalJSON(data []byte) error
UnmarshalJSON custom JSON unmarshalling due to JSONRPCID being string or int
type RPCRequests ¶
type RPCRequests []RPCRequest
type RPCResponse ¶
type RPCResponse struct { JSONRPC string `json:"jsonrpc"` ID JSONRPCID `json:"id"` Result json.RawMessage `json:"result,omitempty"` Error *RPCError `json:"error,omitempty"` }
func NewRPCErrorResponse ¶
func NewRPCErrorResponse(id JSONRPCID, code int, msg string, data string) RPCResponse
func NewRPCSuccessResponse ¶
func NewRPCSuccessResponse(id JSONRPCID, res any) RPCResponse
func RPCInternalError ¶
func RPCInternalError(id JSONRPCID, err error) RPCResponse
func RPCInvalidParamsError ¶
func RPCInvalidParamsError(id JSONRPCID, err error) RPCResponse
func RPCInvalidRequestError ¶
func RPCInvalidRequestError(id JSONRPCID, err error) RPCResponse
func RPCMethodNotFoundError ¶
func RPCMethodNotFoundError(id JSONRPCID) RPCResponse
func RPCParseError ¶
func RPCParseError(id JSONRPCID, err error) RPCResponse
func (RPCResponse) String ¶
func (response RPCResponse) String() string
func (*RPCResponse) UnmarshalJSON ¶
func (response *RPCResponse) UnmarshalJSON(data []byte) error
UnmarshalJSON custom JSON unmarshalling due to JSONRPCID being string or int
type RPCResponses ¶
type RPCResponses []RPCResponse
type WSRPCConnection ¶
type WSRPCConnection interface { // GetRemoteAddr returns a remote address of the connection. GetRemoteAddr() string // WriteRPCResponses writes the resp onto connection (BLOCKING). WriteRPCResponses(resp RPCResponses) // TryWriteRPCResponses tries to write the resp onto connection (NON-BLOCKING). TryWriteRPCResponses(resp RPCResponses) bool // Context returns the connection's context. Context() context.Context }
WSRPCConnection represents a websocket connection.