jsonrpc: github.com/IcanFun/jsonrpc Index | Files

package jsonrpc

import "github.com/IcanFun/jsonrpc"

Package jsonrpc helps JSON-RPC 2.0 implements.


Package Files

context.go debug.go doc.go error.go handler.go jsonrpc.go method.go unmarshal.go


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

func ParseRequest Uses

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

ParseRequest parses a HTTP request to JSON-RPC request.

func SendResponse Uses

func SendResponse(w http.ResponseWriter, resp []*Response, batch bool) error

SendResponse writes JSON-RPC response.

type Context Uses

type Context struct {
    // contains filtered or unexported fields

Context body is http post data

func WithRequestID Uses

func WithRequestID(c Context, id *fastjson.RawMessage) Context

WithRequestID adds request id to context.

func (*Context) Abort Uses

func (c *Context) Abort()

Abort middleware invokes, JSON-RPC stop

func (*Context) Body Uses

func (c *Context) Body() []byte

Body get body

func (*Context) Context Uses

func (c *Context) Context() context.Context

Context get context

func (*Context) IsNext Uses

func (c *Context) IsNext() bool

IsNext can the JSON-RPC continue, default is true

func (*Context) Next Uses

func (c *Context) Next()

Next middleware invokes, JSON-RPC continue

func (*Context) RequestID Uses

func (c *Context) RequestID() *fastjson.RawMessage

RequestID takes request id from context.

type Error Uses

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 Uses

func ErrInternal() *Error

ErrInternal returns internal error.

func ErrInvalidParams Uses

func ErrInvalidParams() *Error

ErrInvalidParams returns invalid params error.

func ErrInvalidRequest Uses

func ErrInvalidRequest() *Error

ErrInvalidRequest returns invalid request error.

func ErrMethodNotFound Uses

func ErrMethodNotFound() *Error

ErrMethodNotFound returns method not found error.

func ErrParse Uses

func ErrParse() *Error

ErrParse returns parse error.

func Unmarshal Uses

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

Unmarshal decodes JSON-RPC params.

func (*Error) Error Uses

func (e *Error) Error() string

Error implements error interface.

type ErrorCode Uses

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 Handler Uses

type Handler interface {
    ServeJSONRPC(c *Context, params *fastjson.RawMessage) (result interface{}, err *Error)

Handler links a method of JSON-RPC request.

type HandlerChain Uses

type HandlerChain []Handler

HandlerChain is Handler slice

type Metadata Uses

type Metadata struct {
    Handler HandlerChain
    Params  interface{}
    Result  interface{}

Metadata has method meta data.

type MethodReference Uses

type MethodReference struct {
    Name    string             `json:"name"`
    Handler string             `json:"handler"`
    Params  *jsonschema.Schema `json:"params,omitempty"`
    Result  *jsonschema.Schema `json:"result,omitempty"`

A MethodReference is a reference of JSON-RPC method.

type MethodRepository Uses

type MethodRepository struct {
    Middlewares MiddlewareChain
    // contains filtered or unexported fields

A MethodRepository has JSON-RPC method functions.

func NewMethodRepository Uses

func NewMethodRepository() *MethodRepository

NewMethodRepository returns new MethodRepository.

func (*MethodRepository) InvokeMethod Uses

func (mr *MethodRepository) InvokeMethod(c Context, r *Request) *Response

InvokeMethod invokes JSON-RPC method.

func (*MethodRepository) InvokeMiddleware Uses

func (mr *MethodRepository) InvokeMiddleware(c Context, r *Request) *Response

InvokeMiddleware invokes middleware method

func (*MethodRepository) Methods Uses

func (mr *MethodRepository) Methods() map[string]Metadata

Methods returns registered methods.

func (*MethodRepository) RegisterMethod Uses

func (mr *MethodRepository) RegisterMethod(method string, params, result interface{}, h ...Handler) error

RegisterMethod registers jsonrpc.Func to MethodRepository.

func (*MethodRepository) ServeDebug Uses

func (mr *MethodRepository) ServeDebug(w http.ResponseWriter, r *http.Request)

ServeDebug views registered method list.

func (*MethodRepository) ServeHTTP Uses

func (mr *MethodRepository) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP provides basic JSON-RPC handling.

func (*MethodRepository) TakeMethod Uses

func (mr *MethodRepository) TakeMethod(r *Request) (HandlerChain, *Error)

TakeMethod takes jsonrpc.Func in MethodRepository.

func (*MethodRepository) Use Uses

func (mr *MethodRepository) Use(middleware ...MiddlewareFunc)

Use registers middleware function

type MiddlewareChain Uses

type MiddlewareChain []MiddlewareFunc

MiddlewareChain middleware function slice

type MiddlewareFunc Uses

type MiddlewareFunc func(*Context) (err *Error)

MiddlewareFunc ... middleware function

type Request Uses

type Request struct {
    Version string               `json:"jsonrpc"`
    Method  string               `json:"method"`
    Params  *fastjson.RawMessage `json:"params"`
    ID      *fastjson.RawMessage `json:"id"`

A Request represents a JSON-RPC request received by the server.

type Response Uses

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

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

func NewResponse Uses

func NewResponse(r *Request) *Response

NewResponse generates a JSON-RPC response.

Package jsonrpc imports 10 packages (graph). Updated 2018-12-06. Refresh now. Tools for package owners.