jsonrpc

package module
v0.0.0-...-39c5ba2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2019 License: MIT Imports: 8 Imported by: 5

README

golang-jsonrpc2

Implementation for JSON-RPC2 protocol

Full specification: https://www.jsonrpc.org/specification

HTTP example

Server

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"

	jrpc "github.com/gumeniukcom/golang-jsonrpc2"
)

func main() {

	serv := jrpc.New()

	if err := serv.RegisterMethod("sum", sum); err != nil {
		panic(err)
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		ctx := context.Background()
		body, err := ioutil.ReadAll(r.Body)
		if err != nil {
			panic(err)
		}
		defer r.Body.Close()

		w.Header().Set("Content-Type", "applicaition/json")
		w.WriteHeader(http.StatusOK)
		if _, err = w.Write(serv.HandleRPCJsonRawMessage(ctx, body)); err != nil {
			panic(err)
		}
	})

	if err := http.ListenAndServe(":8088", nil); err != nil {
		panic(err)
	}
}

type income struct {
	A int `json:"a"`
	B int `json:"b"`
}
type outcome struct {
	Sum int `json:"sum"`
}

func sum(ctx context.Context, data json.RawMessage) (json.RawMessage, int, error) {
	if data == nil {
		return nil, jrpc.InvalidRequestErrorCode, fmt.Errorf("empty request")
	}
	inc := &income{}
	err := json.Unmarshal(data, inc)
	if err != nil {
		return nil, jrpc.InvalidRequestErrorCode, err
	}

	C := outcome{
		Sum: inc.A + inc.B,
	}

	mdata, err := json.Marshal(C)
	if err != nil {
		return nil, jrpc.InternalErrorCode, err
	}
	return mdata, jrpc.OK, nil
}

Request

curl -d '{"jsonrpc":"2.0", "id":"qwe", "method":"sum", "params":{"a":5, "b":3}}' -H "Content-Type: application/json" -X POST http://localhost:8088/

Response

{"jsonrpc":"2.0","result":{"sum":8},"id":"qwe"}

Documentation

Index

Constants

View Source
const (
	// ParseErrorCode : parse error. not well formed
	ParseErrorCode = -32700

	// InvalidRequestErrorCode : Invalid Request
	InvalidRequestErrorCode = -32600

	// MethodNotFoundErrorCode : requested method not found
	MethodNotFoundErrorCode = -32601

	// InvalidParamsErrorCode : invalid method parameters
	InvalidParamsErrorCode = -32602

	// InternalErrorCode : Internal error
	InternalErrorCode = -32603

	// OK : everything is ok
	OK = 0

	//MethodNotImplemented : use for develop method
	MethodNotImplemented = -32604

	//RequestTimeLimit : tooooo long
	RequestTimeLimit = -32605
)

@see http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php

View Source
const (
	// Version const for define version of protocol
	// @see https://www.jsonrpc.org/specification#request_object
	Version = "2.0"
)

Variables

This section is empty.

Functions

func Request

func Request(ctx context.Context, methodName string, params ParamsDataMarshaller) (*structs.Request, error)

Request return request instance

func Response

func Response(
	ctx context.Context,
	id interface{},
	data *json.RawMessage,
	error *structs.Error,
) *structs.Response

Response ...

Types

type ErrorMessages

type ErrorMessages map[int]string

ErrorMessages map of errors

type InterceptorCallMethod

type InterceptorCallMethod func(ctx context.Context,
	methodName string,
	data json.RawMessage,
	id interface{}) (context.Context, int, error)

InterceptorCallMethod interface for interceptor method

type InterceptorCallMethods

type InterceptorCallMethods []InterceptorCallMethod

InterceptorCallMethods registry for global interceptors

type JSONRPC

type JSONRPC struct {
	// contains filtered or unexported fields
}

JSONRPC container for jsonrpc

func New

func New() *JSONRPC

New creates new instance of JSONRPC

func (*JSONRPC) Error

func (j *JSONRPC) Error(
	ctx context.Context,
	err error,
	errorCode int,
	id interface{},
) *structs.Response

Error is method for create response with error code

func (*JSONRPC) HandleBatchRPC

func (j *JSONRPC) HandleBatchRPC(ctx context.Context, data structs.Requests) structs.BatchFullResponse

HandleBatchRPC make batch rpc

func (*JSONRPC) HandleRPC

func (j *JSONRPC) HandleRPC(ctx context.Context, data *structs.Request) *structs.Response

HandleRPC make rpc request

func (*JSONRPC) HandleRPCJsonRawMessage

func (j *JSONRPC) HandleRPCJsonRawMessage(ctx context.Context, data json.RawMessage) json.RawMessage

HandleRPCJsonRawMessage receive jsonRaw , parse, magic and send jsonRaw

func (*JSONRPC) RegisterError

func (j *JSONRPC) RegisterError(code int, msg string) error

RegisterError register new error @see http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php

func (*JSONRPC) RegisterGlobalInterceptorCall

func (j *JSONRPC) RegisterGlobalInterceptorCall(method InterceptorCallMethod)

RegisterGlobalInterceptorCall register global interceptors

func (*JSONRPC) RegisterMethod

func (j *JSONRPC) RegisterMethod(name string, method RPCMethod) error

RegisterMethod new method

func (*JSONRPC) SetDefaultTimeOut

func (j *JSONRPC) SetDefaultTimeOut(timeout int)

SetDefaultTimeOut set timeout for func run

type ParamsDataMarshaller

type ParamsDataMarshaller interface {
	MarshalJSON() ([]byte, error)
}

ParamsDataMarshaller interface for Request params

type RPCMethod

type RPCMethod func(ctx context.Context, data json.RawMessage) (json.RawMessage, int, error)

RPCMethod define function interface

type RPCMethods

type RPCMethods map[string]RPCMethod

RPCMethods container for methods

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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