jsonrpc

package
v0.1.8-alpha.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2019 License: GPL-3.0 Imports: 11 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeSessionToken

func MakeSessionToken(username, secretKey string, ttl int64) string

MakeSessionToken creates a session token for RPC requests. If ttl is zero, the session will never expire.

func VerifySessionToken

func VerifySessionToken(tokenStr, secretKey string) error

VerifySessionToken verifies that the given token was created using the given secretKey

Types

type API

type API interface {
	APIs() APISet
}

API defines an interface for providing and accessing API functions. Packages that offer services accessed via RPC or any service-oriented interface must implement it.

type APIInfo

type APIInfo struct {

	// Func is the API function to be execute.
	Func func(params interface{}) *Response

	// Private indicates a requirement for a private, authenticated
	// user session before this API function is executed.
	Private bool

	// Namespace is the JS namespace where the method should reside
	Namespace string

	// Description describes the API
	Description string
}

APIInfo defines a standard API function type and other parameters.

type APISet

type APISet map[string]APIInfo

APISet defines a collection of APIs

func (APISet) Get

func (a APISet) Get(name string) *APIInfo

Get gets an API function by name and namespace

type Err

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

Err represents JSON RPC error object

type JSONRPC

type JSONRPC struct {

	// OnRequestFunc accepts a function. It is called
	// each time a request is received. It is a good
	// place to verify authentication. If error
	// is returned, the handler is not called and
	// the error is returned.
	OnRequest OnRequestFunc
	// contains filtered or unexported fields
}

JSONRPC defines a wrapper over mux json rpc that works with RPC functions of type `types.API` defined in packages that offer RPC APIs.`

func New

func New(addr string, sessionKey string, disableAuth bool) *JSONRPC

New creates a JSONRPC server

func (*JSONRPC) APIs

func (s *JSONRPC) APIs() APISet

APIs returns system APIs

func (*JSONRPC) AddAPI

func (s *JSONRPC) AddAPI(name string, api APIInfo)

AddAPI adds an API to s api set

func (*JSONRPC) MergeAPISet

func (s *JSONRPC) MergeAPISet(apiSets ...APISet)

MergeAPISet merges an API set with s current api sets

func (*JSONRPC) Methods

func (s *JSONRPC) Methods() (methodsInfo []MethodInfo)

Methods gets the names of all methods in the API set.

func (*JSONRPC) Serve

func (s *JSONRPC) Serve()

Serve starts the server

func (*JSONRPC) Stop

func (s *JSONRPC) Stop()

Stop stops the RPC server

type MethodInfo

type MethodInfo struct {
	Name        string `json:"name"`
	Namespace   string `json:"-"`
	Description string `json:"description"`
	Private     bool   `json:"private"`
}

MethodInfo describe an RPC method info

type OnRequestFunc

type OnRequestFunc func(r *http.Request) error

OnRequestFunc is the type of function to use as a callback when new requests are received

type Params

type Params map[string]interface{}

Params represent JSON API parameters

func (*Params) Scan

func (p *Params) Scan(dest interface{}) error

Scan attempts to convert the params to a struct or map type

type Request

type Request struct {
	JSONRPCVersion string      `json:"jsonrpc"`
	Method         string      `json:"method"`
	Params         interface{} `json:"params"`
	ID             interface{} `json:"id,omitempty"`
}

Request represent a JSON RPC request

func (Request) IsNotification

func (r Request) IsNotification() bool

IsNotification checks whether the request is a notification according to JSON RPC specification. When ID is nil, we assume it's a notification request.

type Response

type Response struct {
	JSONRPCVersion string      `json:"jsonrpc"`
	Result         interface{} `json:"result"`
	Err            *Err        `json:"error,omitempty"`
	ID             interface{} `json:"id,omitempty"` // string or float64
}

Response represents a JSON RPC response

func Error

func Error(code int, message string, data interface{}) *Response

Error creates an error response

func Success

func Success(result interface{}) *Response

Success creates a success response

func (Response) IsError

func (r Response) IsError() bool

IsError checks whether r is an error response

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL