README

jsonrpc2

基于json-rpc2.0规范撰写

Expand ▾ Collapse ▴

Documentation

Overview

    Package jsonrpc helps JSON-RPC 2.0 implements.

    Index

    Constants

    View Source
    const (
    	// Version is JSON-RPC 2.0.
    	Version = "2.0"
    )

    Variables

    View Source
    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 Methods

          func Methods() map[string]Metadata

            Methods returns registered methods.

            func ParseRequest

            func ParseRequest(r *http.Request) ([]Request, bool, *Error)

              ParseRequest parses a HTTP request to JSON-RPC request.

              func PurgeMethods

              func PurgeMethods()

                PurgeMethods purges all registered methods.

                func RegisterMethod

                func RegisterMethod(method string, f Func, params, result interface{}) error

                  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

                    func TakeMethod(r Request) (Func, *Error)

                      TakeMethod takes jsonrpc.Func in MethodRepository.

                      Types

                      type Client

                      type Client struct {
                      	Url string
                      	// contains filtered or unexported fields
                      }

                      func NewClient

                      func NewClient(url string) *Client

                      func (*Client) Call

                      func (c *Client) Call(method string, params interface{}, id int) (*Resp, error)

                      func (*Client) CallTimeout

                      func (c *Client) CallTimeout(method string, params interface{}, id int, timeout time.Duration) (*Resp, error)

                      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 ErrInternal

                        func ErrInternal() *Error

                          ErrInternal returns internal error.

                          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 ErrParse

                                func ErrParse() *Error

                                  ErrParse returns parse error.

                                  func Unmarshal

                                  func Unmarshal(params *json.RawMessage, dst interface{}) *Error

                                    Unmarshal decodes JSON-RPC params.

                                    func (*Error) Error

                                    func (e *Error) Error() string

                                      Error implements error interface.

                                      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.

                                                  type Resp

                                                  type Resp struct {
                                                  	Id     int         `json:"id"`
                                                  	Result interface{} `json:"result"`
                                                  	Error  *RespError  `json:"error"`
                                                  }

                                                  type RespError

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

                                                  func (*RespError) Error

                                                  func (this *RespError) Error() string

                                                  type Response

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

                                                    A Response represents a JSON-RPC Resp returned by the server.

                                                    func NewResponse

                                                    func NewResponse(r Request) Response

                                                      NewResponse generates a JSON-RPC Resp.

                                                      Directories

                                                      Path Synopsis