Documentation ¶
Overview ¶
Package jsonrpc helps JSON-RPC 2.0 implements.
Index ¶
- Constants
- Variables
- func DebugHandler(w http.ResponseWriter, r *http.Request)
- func Handler(w http.ResponseWriter, r *http.Request)
- func Methods() map[string]Metadata
- func ParseRequest(r *http.Request) ([]Request, bool, *Error)
- func PurgeMethods()
- func RegisterMethod(method string, f Func, params, result interface{}) error
- func SendResponse(w http.ResponseWriter, resp []Response, batch bool) error
- func TakeMethod(r Request) (Func, *Error)
- type Client
- type Controller
- type Error
- type ErrorCode
- type Func
- type Metadata
- type MethodReference
- type MethodRepository
- type Request
- type Resp
- type RespError
- type Response
Constants ¶
const (
// Version is JSON-RPC 2.0.
Version = "2.0"
)
Variables ¶
var Filter func(context.Context, *Request) *Error
Filter runs before invoke a method.
Functions ¶
func DebugHandler ¶
func DebugHandler(w http.ResponseWriter, r *http.Request)
DebugHandler views registered method list.
func Handler ¶
func Handler(w http.ResponseWriter, r *http.Request)
Handler provides basic JSON-RPC handling.
func ParseRequest ¶
ParseRequest parses a HTTP request to JSON-RPC request.
func RegisterMethod ¶
RegisterMethod registers jsonrpc.Func to MethodRepository.
func SendResponse ¶
func SendResponse(w http.ResponseWriter, resp []Response, batch bool) error
SendResponse writes JSON-RPC Resp.
func TakeMethod ¶
TakeMethod takes jsonrpc.Func in MethodRepository.
Types ¶
type Client ¶
type Client struct { Url string // contains filtered or unexported fields }
type Controller ¶
type Error ¶
type Error struct { Code ErrorCode `json:"code"` Message string `json:"message"` Data interface{} `json:"data,omitempty"` }
An Error is a wrapper for a JSON interface value.
func ErrInvalidParams ¶
func ErrInvalidParams() *Error
ErrInvalidParams returns invalid params error.
func ErrInvalidRequest ¶
func ErrInvalidRequest() *Error
ErrInvalidRequest returns invalid request error.
func ErrMethodNotFound ¶
func ErrMethodNotFound() *Error
ErrMethodNotFound returns method not found error.
func Unmarshal ¶
func Unmarshal(params *json.RawMessage, dst interface{}) *Error
Unmarshal decodes JSON-RPC params.
type ErrorCode ¶
type ErrorCode int
A ErrorCode by JSON-RPC 2.0.
const ( // ErrorCodeParse is parse error code. ErrorCodeParse ErrorCode = -32700 // ErrorCodeInvalidRequest is invalid request error code. ErrorCodeInvalidRequest ErrorCode = -32600 // ErrorCodeMethodNotFound is method not found error code. ErrorCodeMethodNotFound ErrorCode = -32601 // ErrorCodeInvalidParams is invalid params error code. ErrorCodeInvalidParams ErrorCode = -32602 // ErrorCodeInternal is internal error code. ErrorCodeInternal ErrorCode = -32603 )
type Func ¶
type Func func(c context.Context, params *json.RawMessage) (result interface{}, err *Error)
Func links a method of JSON-RPC request.
type Metadata ¶
type Metadata struct { Func Func Params interface{} Result interface{} }
Metadata has method meta data.
type MethodReference ¶
type MethodReference struct { Name string `json:"name"` Function string `json:"function"` Params *jsonschema.Schema `json:"params,omitempty"` Result *jsonschema.Schema `json:"result,omitempty"` }
A MethodReference is a reference of JSON-RPC method.
type MethodRepository ¶
type MethodRepository struct {
// contains filtered or unexported fields
}
A MethodRepository has JSON-RPC method functions.
type Request ¶
type Request struct { ID *json.RawMessage `json:"id"` Version string `json:"jsonrpc"` Method string `json:"method"` Params *json.RawMessage `json:"params"` }
A Request represents a JSON-RPC request received by the server.