jsonrpc

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2020 License: MIT Imports: 0 Imported by: 0

README

logo

go-jsonrpc

Release version Project language Build Status Coverage Go Report License

This package provides JsonRPC 2.0 implementation based on interfaces with easy components extending.

Installation and usage

The import path for the package is github.com/tarampampam/go-jsonrpc.

To install it, run:

go get github.com/tarampampam/go-jsonrpc

API documentation can be found here.

Usage example

Working examples can be found in examples directory.

RPC methods definition is very simple:

package main

import "github.com/tarampampam/go-jsonrpc"

type myRpcMethod struct{} // Implements `jsonrpc.Method` interface

// GetParamsType says to Router a structure (or nil) which must be used for params parsing (fields, etc.).
func (*myRpcMethod) GetParamsType() interface{} { return nil }

// GetName returns method name in string representation.
func (*myRpcMethod) GetName() string { return "my.method" }

// Handle will be called by Router when method with current name will be requested.
func (*myRpcMethod) Handle(_ interface{}) (interface{}, jsonrpc.Error) {
	return "your method response", nil
}

You can define method params as a structure too:

package main

import "errors"

type (
    myRpcMethod struct{} // Implements `jsonrpc.Method` interface

    myRpcMethodParams struct { // Implements `jsonrpc.Validator` interface
        Number     int     `json:"number"`
        IsOptional *string `json:"is_optional"` // optional value (can be nil)
    }
)

// Implement `jsonrpc.Validator` interface for easy incoming params validation
func (p *myRpcMethodParams) Validate() error {
	if p.Number <= 0 {
		return errors.New("number must be positive")
	}

	return nil // all is ok
}

// GetParamsType NOW returns structure for method params
func (*myRpcMethod) GetParamsType() interface{} { return &myRpcMethodParams{} }

// ...

And use use provided kernel and router:

package main

import (
	"fmt"

	"github.com/tarampampam/go-jsonrpc"
	rpcKernel "github.com/tarampampam/go-jsonrpc/kernel"
	rpcRouter "github.com/tarampampam/go-jsonrpc/router"
)

// ... methods and params definitions ...

func main () {
    // create router instance
	router := rpcRouter.New()

    // register our RPC method
    router.RegisterMethod(new(myRpcMethod))

    // create kernel using our router
    kernel := rpcKernel.New(router)

    // handle RPC request
    responseAsJSON := kernel.HandleJSONRequest([]byte(`{"jsonrpc":"2.0", "method":"ping", "id":1}`))

    fmt.Println(responseAsJSON)
}
Testing

For application testing we use built-in golang testing feature and docker-ce + docker-compose as develop environment. So, just write into your terminal after repository cloning:

$ make test

Changelog

Release date Commits since latest release

Changes log can be found here.

Support

Issues Issues

If you will find any package errors, please, make an issue in current repository.

License

This is open-sourced software licensed under the MIT License.

Documentation

Index

Constants

View Source
const Version string = "2.0"

Version is version of current JsonRPC <https://www.jsonrpc.org/specification>

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error interface {
	error

	// GetCode returns error code.
	GetCode() int

	// GetMessage returns error message.
	GetMessage() string

	// GetData returns error extra-data.
	GetData() interface{}
}

Error is general RPC error.

type Method

type Method interface {
	// GetName returns method name in string representation.
	GetName() string

	// GetParamsType says to Router a structure (or nil) which must be used for params parsing (fields, etc.).
	GetParamsType() interface{}

	// Handle will be called by Router when method with current name will be requested.
	Handle(params interface{}) (interface{}, Error)
}

Method used as RPC method handler.

type Router

type Router interface {
	// RegisterMethod make a method registration for later invoking.
	RegisterMethod(method Method) error

	// MethodIsRegistered returns `true` only if passed method is registered.
	MethodIsRegistered(methodName string) bool

	// Invoke accepts method name and invoke registered method with same name.
	Invoke(methodName string, params interface{}) (interface{}, Error)
}

Router is used for methods registration and invoking.

type Validator

type Validator interface {
	// IsValid returns `error` only if structure has INCORRECT state or properties.
	Validate() error
}

Validator allows to validate different structures, like method params (but not only).

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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