jrpc2: github.com/bitwurx/jrpc2 Index | Files

package jrpc2

import "github.com/bitwurx/jrpc2"

Index

Package Files

server.go

func NewResponse Uses

func NewResponse(result interface{}, errObj *ErrorObject, id interface{}, nl bool) []byte

NewResponse creates a bytes encoded representation of a response. Both result and error response objects can be created. The nl flag specifies if the response should be newline terminated.

type Batch Uses

type Batch struct {
    // Responses contains the byte representations of a batch of responses.
    Responses [][]byte
}

Batch is a wrapper around multiple response objects.

func (*Batch) AddResponse Uses

func (b *Batch) AddResponse(resp []byte)

AddResponse inserts the response into the batch responses.

func (*Batch) MakeResponse Uses

func (b *Batch) MakeResponse() []byte

MakeResponse creates a bytes encoded representation of a response object.

type ErrorCode Uses

type ErrorCode int

ErrorCode is a json rpc 2.0 error code.

const (
    ParseErrorCode     ErrorCode = -32700
    InvalidRequestCode ErrorCode = -32600
    MethodNotFoundCode ErrorCode = -32601
    InvalidParamsCode  ErrorCode = -32602
    InternalErrorCode  ErrorCode = -32603
    MethodExistsCode   ErrorCode = -32000
    URLSchemeErrorCode ErrorCode = -32001
)

Error codes

type ErrorMsg Uses

type ErrorMsg string

ErrorMsg is a json rpc 2.0 error message.

const (
    ParseErrorMsg     ErrorMsg = "Parse error"
    InvalidRequestMsg ErrorMsg = "Invalid Request"
    MethodNotFoundMsg ErrorMsg = "Method not found"
    InvalidParamsMsg  ErrorMsg = "Invalid params"
    InternalErrorMsg  ErrorMsg = "Internal error"
    ServerErrorMsg    ErrorMsg = "Server error"
    MethodExistsMsg   ErrorMsg = "Method exists"
    URLSchemeErrorMsg ErrorMsg = "URL scheme error"
)

Error message

type ErrorObject Uses

type ErrorObject struct {
    // Code indicates the error type that occurred.
    // Message provides a short description of the error.
    // Data is a primitive or structured value that contains additional information.
    // about the error.
    Code    ErrorCode   `json:"code"`
    Message ErrorMsg    `json:"message"`
    Data    interface{} `json:"data,omitempty"`
}

ErrorObject represents a response error object.

func ParseParams Uses

func ParseParams(params json.RawMessage, p Params) *ErrorObject

ParseParams processes the params data structure from the request. Named parameters will be umarshaled into the provided Params inteface. Positional arguments will be passed to Params interface's FromPositional method for extraction.

type Method Uses

type Method struct {
    // Url is the url of the server that handles the method.
    // Method is the callable function
    Url    string
    Method func(params json.RawMessage) (interface{}, *ErrorObject)
}

Method represents an rpc method.

type MethodWithContext Uses

type MethodWithContext struct {
    // Url is the url of the server that handles the method.
    // Method is the callable function
    Url    string
    Method func(ctx context.Context, params json.RawMessage) (interface{}, *ErrorObject)
}

MethodWithContext represents an rpc method with a context.

type MuxHandler Uses

type MuxHandler struct {
    Methods map[string]MethodWithContext
}

MuxHandler is a method dispatcher that handles request at a designated route.

func NewMuxHandler Uses

func NewMuxHandler() *MuxHandler

NewMuxHandler creates a new mux handler instance.

func (*MuxHandler) Register Uses

func (h *MuxHandler) Register(name string, method Method)

Register adds the method to the handler methods.

func (*MuxHandler) RegisterWithContext Uses

func (h *MuxHandler) RegisterWithContext(name string, method MethodWithContext)

RegisterWithContext adds the method to the handler methods.

type MuxServer Uses

type MuxServer struct {
    Host     string
    Headers  map[string]string
    Handlers map[string]*MuxHandler
}

MuxServer is a json rpc 2 server that handles multiple requests.

func NewMuxServer Uses

func NewMuxServer(host string, headers map[string]string) *MuxServer

NewMuxServer creates a new mux handler instance.

func (*MuxServer) AddHandler Uses

func (s *MuxServer) AddHandler(route string, handler *MuxHandler)

AddHandler add the handler to the mux handlers.

func (*MuxServer) Start Uses

func (s *MuxServer) Start()

Start Starts binds all server rpcHandlers to their handler routes and starts the http server.

type Params Uses

type Params interface {
    FromPositional([]interface{}) error
}

Params defines methods for processing request parameters.

type RegisterRPCParams Uses

type RegisterRPCParams struct {
    // Name is the the name of the method being registered.
    // Url is the url of the server that handles the method.
    Name *string
    Url  *string
}

RegisterRPCParams is a paramater spec for the RegisterRPC method.

func (*RegisterRPCParams) FromPositional Uses

func (rp *RegisterRPCParams) FromPositional(params []interface{}) error

FromPositional extracts the positional name and url parameters from a list of parameters.

type RequestObject Uses

type RequestObject struct {
    // Jsonrpc specifies the version of the JSON-RPC protocol.
    // Must be exactly "2.0".
    // Method contains the name of the method to be invoked.
    // Params is a structured value that holds the parameter values to be used during
    // the invocation of the method.
    // Id is a unique identifier established by the client.
    Jsonrpc string          `json:"jsonrpc"`
    Method  interface{}     `json:"method"`
    Params  json.RawMessage `json:"params"`
    Id      interface{}     `json:"id"`
    // contains filtered or unexported fields
}

RequestObject represents a request object

type ResponseObject Uses

type ResponseObject struct {
    // Jsonrpc specifies the version of the JSON-RPC protocol.
    // Must be exactly "2.0".
    // Error contains the error object if an error occurred while processing the request.
    // Result contains the result of the called method.
    // Id contains the client established request id or null.
    Jsonrpc string       `json:"jsonrpc"`
    Error   *ErrorObject `json:"error,omitempty"`
    Result  interface{}  `json:"result,omitempty"`
    Id      interface{}  `json:"id"`
}

ResponseObject represents a response object.

type Server Uses

type Server struct {
    // Host is the host:port of the server.
    // Route is the path to the rpc api.
    // Methods contains the mapping of registered methods.
    // Headers contains response headers.
    Host    string
    Route   string
    Methods map[string]MethodWithContext
    Headers map[string]string
}

Server represents a jsonrpc 2.0 capable web server.

func NewServer Uses

func NewServer(host, route string, headers map[string]string) *Server

NewServer creates a new server instance

func (*Server) Call Uses

func (s *Server) Call(ctx context.Context, name interface{}, params json.RawMessage) (interface{}, *ErrorObject)

Call invokes the named method with the provided parameters. If a method from the server Methods has a Method member will be called locally. If a method from the server Methods has a Url member it will be called by proxy.

func (*Server) HandleBatch Uses

func (s *Server) HandleBatch(w http.ResponseWriter, reqs []*RequestObject)

HandleBatch validates, calls, and returns the results of a batch of rpc client requests. Batch methods are called in individual goroutines and collected in a single response.

func (*Server) HandleRequest Uses

func (s *Server) HandleRequest(w http.ResponseWriter, req *RequestObject)

HandleRequest validates, calls, and returns the result of a single rpc client request.

func (*Server) ParseRequest Uses

func (s *Server) ParseRequest(w http.ResponseWriter, r *http.Request) *ErrorObject

ParseRequest parses the json request body and unpacks into one or more. RequestObjects for single or batch processing.

func (*Server) Register Uses

func (s *Server) Register(name string, method Method)

Register maps the provided method to the given name for later method calls.

func (*Server) RegisterRPC Uses

func (s *Server) RegisterRPC(ctx context.Context, params json.RawMessage) (interface{}, *ErrorObject)

RegisterRPC accepts a method name and server url to register a proxy rpc method. A method name can be only be registered once.

func (*Server) RegisterWithContext Uses

func (s *Server) RegisterWithContext(name string, method MethodWithContext)

func (*Server) Start Uses

func (s *Server) Start()

Start binds the rpcHandler to the server route and starts the http server

func (*Server) StartWithMiddleware Uses

func (s *Server) StartWithMiddleware(m func(next http.HandlerFunc) http.HandlerFunc)

StartWithMiddleware binds the rpcHandler, with its middleware to the server route and starts the http server

func (*Server) ValidateRequest Uses

func (s *Server) ValidateRequest(req *RequestObject) *ErrorObject

ValidateRequest validates that the request json contains valid values.

Package jrpc2 imports 11 packages (graph). Updated 2019-04-18. Refresh now. Tools for package owners.