Skip to content

rpc.jsonrpc #

JSON RPC

JSON RPC Call

Defined in call.v, the module provides two JSON RPC functions to call JSON RPC Methods. These act as wrappers functions that handle decoding encoding and errors for method calls to the function provided in the call parameters.

To perform calls to methods that don't take any parameters, json2.Null must be passed as the generic parameter for the function parameter

As with all JSONRPC functionalities, calls with multiple parameters aren't supported in these call functions. Function call parameters must be scaffolded into a single struct

Notifications

A JSON-RPC call that does not require any response data, or where the client does not expect any result (other than possibly an acknowledgement of receipt), is typically referred to as a "notification". In JSON-RPC 2.0, a notification is characterized by the absence of an "id" field in the request. This signals to the server that no response is expected. (chatgpt 4.0)

Handler

Handler Generation

Defined in handler_gen.v, handler generation allows for JSON RPC Handlers to be generated for a provided set of RPC Methods.

handler_gen_test.v

In the handler generation test, a handler is generated for a Tester struct which has methods with various parameters and return types. This tests ensures that the handle method generated for the Handler can exhaustively generate method call matchers for functions with varying return and parameter types.

Constants #

const parse_error = InnerJsonRpcError{
	code:    32700
	message: 'Parse error'
	data:    'Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.'
}

Default JSONRPC errors, as defined in https://www.jsonrpc.org/specification

const invalid_request = InnerJsonRpcError{
	code:    32600
	message: 'Invalid Request'
	data:    'The JSON sent is not a valid Request object.'
}
const method_not_found = InnerJsonRpcError{
	code:    32601
	message: 'Method not found'
	data:    'The method does not exist / is not available.'
}
const invalid_params = InnerJsonRpcError{
	code:    32602
	message: 'Invalid params'
	data:    'Invalid method parameter(s).'
}
const internal_error = InnerJsonRpcError{
	code:    32603
	message: 'Internal Error'
	data:    'Internal JSON-RPC error.'
}

fn call #

fn call[T, D](msg string, method fn (T) !D) !string

performs jsonrpc call on provided method

fn call_void #

fn call_void[T](msg string, method fn (T) !) !string

performs jsonrpc call on provided method that doesnt return anything returns ok or error, unlike notify which does not return anything

fn decode_request #

fn decode_request[T](data string) !JsonRpcRequest[T]

fn decode_request_id #

fn decode_request_id(data string) !string

fn decode_request_method #

fn decode_request_method(data string) !string

fn decode_response #

fn decode_response[D](data string) !Response[D]

fn generate_client #

fn generate_client(config GenerateClientConfig) Module

fn generate_client_factory #

fn generate_client_factory(name string) !CodeFile

generate_client_factory generates a factory code file with factory functions for the client

fn generate_client_method #

fn generate_client_method(client_struct Struct, method Function) !Function

fn generate_client_methods #

fn generate_client_methods(client_struct Struct, methods []Function) ![]Function

fn generate_client_struct #

fn generate_client_struct(name string) Struct

fn generate_handler #

fn generate_handler(args HandlerArgs) ![]codemodel.CodeItem

fn generate_ws_factory_code #

fn generate_ws_factory_code(name_ string) ![]CodeItem

fn invoke #

fn invoke[D](msg string, method fn () !D) !string

performs jsonrpc call on provided method without parameters

fn jsonrpcerror_decode #

fn jsonrpcerror_decode(data string) !JsonRpcError

pub fn decode_response[D](data string) !JsonRpcResponse[D] { raw := json2.raw_decode(data)! if 'error' in raw.as_map() { return json.decode(JsonRpcError, data)! } return json.decode(JsonRpcResponse[D], data)! }

fn jsonrpcrequest_decode #

fn jsonrpcrequest_decode[T](data string) !JsonRpcRequest[T]

fn jsonrpcrequest_decode_any #

fn jsonrpcrequest_decode_any(data string) !JsonRpcRequestAny

fn jsonrpcrequest_decode_method #

fn jsonrpcrequest_decode_method(data string) !string

fn jsonrpcresponse_decode #

fn jsonrpcresponse_decode[D](data string) !JsonRpcResponse[D]

fn new_error #

fn new_error(id string, error InnerJsonRpcError) JsonRpcError

fn new_handler #

fn new_handler(logger &log.Logger) !&JsonRpcHandler

fn new_jsonrpcerror #

fn new_jsonrpcerror(id string, error InnerJsonRpcError) JsonRpcError

fn new_jsonrpcrequest #

fn new_jsonrpcrequest[T](method string, params T) JsonRpcRequest[T]

fn new_jsonrpcresponse #

fn new_jsonrpcresponse[T](id string, result T) JsonRpcResponse[T]

fn new_response #

fn new_response[T](id string, result T) JsonRpcResponse[T]

fn notify #

fn notify[T](params_json string, method fn (T) !)

fn request_decode_any #

fn request_decode_any(data string) !JsonRpcRequestAny

fn request_params #

fn request_params(data string) !string

returns json encoded params field of request

interface IJsonRpcClient #

interface IJsonRpcClient {
mut:
	transport IRpcTransportClient
}

JSON-RPC WebSoocket Server

fn (IJsonRpcClient) send_json_rpc #

fn (mut client IJsonRpcClient) send_json_rpc[T, D](method string, data T, timeout int) !D

interface IRpcTransportClient #

interface IRpcTransportClient {
mut:
	send(string, int) !string
}

fn (JsonRpcRequest[T]) to_json #

fn (j &JsonRpcRequest[T]) to_json() string

fn (JsonRpcResponse[D]) to_json #

fn (j &JsonRpcResponse[D]) to_json() string

struct ClientConfig #

@[params]
struct ClientConfig {
	address string // address of ws server
	logger  &log.Logger
}

struct GenerateClientConfig #

struct GenerateClientConfig {
	name    string
	methods []Function
}

struct HandlerArgs #

struct HandlerArgs {
pub:
	stateful bool
	receiver Struct
	methods  []codemodel.Function
}

struct InnerJsonRpcError #

struct InnerJsonRpcError {
pub mut:
	code    int    @[required]
	message string @[required]
	data    string
}

fn (InnerJsonRpcError) code #

fn (err InnerJsonRpcError) code() int

fn (InnerJsonRpcError) msg #

fn (err InnerJsonRpcError) msg() string

struct JsonRpcError #

struct JsonRpcError {
pub mut:
	jsonrpc string            @[required]
	error   InnerJsonRpcError @[required]
	id      string            @[required]
}

fn (JsonRpcError) to_json #

fn (j &JsonRpcError) to_json() string

struct JsonRpcHandler #

@[heap]
struct JsonRpcHandler {
pub mut:
	// rpcwebsocket.RpcWsServer // server for ws communication
	logger &log.Logger
	// map of method names to procedure handlers
	procedures map[string]ProcedureHandler
	state      voidptr
}

JSON-RPC WebSoocket Server

fn (JsonRpcHandler) register #

fn (mut server JsonRpcHandler) register(name string, procedure ProcedureHandler) !

registers procedure handlers by method name

fn (JsonRpcHandler) handler #

fn (mut handler JsonRpcHandler) handler(client &websocket.Client, message string) string

fn (JsonRpcHandler) handle #

fn (mut handler JsonRpcHandler) handle(message string) !string

struct JsonRpcRequest #

struct JsonRpcRequest[T] {
pub mut:
	jsonrpc string @[required]
	method  string @[required]
	params  T      @[required]
	id      string @[required]
}

struct JsonRpcRequestAny #

struct JsonRpcRequestAny {
pub mut:
	jsonrpc string @[required]
	method  string @[required]
	id      string @[required]
}

struct JsonRpcResponse #

struct JsonRpcResponse[D] {
pub mut:
	jsonrpc string @[required]
	result  D
	id      string @[required]
}