...
This commit is contained in:
@@ -11,8 +11,9 @@ import x.json2 as json
|
||||
pub struct Handler {
|
||||
pub mut:
|
||||
// A map where keys are method names and values are the corresponding procedure handler functions
|
||||
procedures map[string]ProcedureHandler
|
||||
params map[string]string
|
||||
procedures map[string]ProcedureHandler
|
||||
procedures_group map[string]ProcedureHandlerGroup
|
||||
servercontext map[string]string
|
||||
}
|
||||
|
||||
// ProcedureHandler is a function type that processes a JSON-RPC request payload and returns a response.
|
||||
@@ -23,6 +24,8 @@ pub mut:
|
||||
// If an error occurs during any of these steps, it should be returned.
|
||||
pub type ProcedureHandler = fn (request Request) !Response
|
||||
|
||||
pub type ProcedureHandlerGroup = fn (rpcid int, servercontext map[string]string, actorname string, methodname string, params string) !Response
|
||||
|
||||
// new_handler creates a new JSON-RPC handler with the specified procedure handlers.
|
||||
//
|
||||
// Parameters:
|
||||
@@ -62,13 +65,22 @@ pub fn (mut handler Handler) register_procedure_void[T](method string, function
|
||||
handler.procedures[procedure.method] = procedure.handle
|
||||
}
|
||||
|
||||
// // register_procedure registers a new procedure handler for the specified method.
|
||||
// //
|
||||
// // Parameters:
|
||||
// // - method: The name of the method to register
|
||||
// // - procedure: The procedure handler function to register
|
||||
// pub fn (mut handler Handler) register_procedure(method string, procedure ProcedureHandler) {
|
||||
// handler.procedures[method] = procedure
|
||||
// }
|
||||
|
||||
// register_procedure registers a new procedure handler for the specified method.
|
||||
//
|
||||
// Parameters:
|
||||
// - method: The name of the method to register
|
||||
// - procedure: The procedure handler function to register
|
||||
pub fn (mut handler Handler) register_procedure_handle(method string, procedure ProcedureHandler) {
|
||||
handler.procedures[method] = procedure
|
||||
// - procedure: The procedure handler group function to register
|
||||
pub fn (mut handler Handler) register_api_handler(groupname string, procedure_group ProcedureHandlerGroup) {
|
||||
handler.procedures_group[groupname] = procedure_group
|
||||
}
|
||||
|
||||
pub struct Procedure[T, U] {
|
||||
@@ -158,12 +170,46 @@ fn error_to_jsonrpc(err IError) !RPCError {
|
||||
// Returns:
|
||||
// - The JSON-RPC response as a string, or an error if processing fails
|
||||
pub fn (handler Handler) handle(request Request) !Response {
|
||||
if request.method.contains('.') {
|
||||
parts := request.method.split('.')
|
||||
mut groupname := ''
|
||||
mut actorname := ''
|
||||
mut methodname := ''
|
||||
if parts.len == 2 {
|
||||
groupname = 'default'
|
||||
actorname = parts[0]
|
||||
methodname = parts[1]
|
||||
} else if parts.len == 3 {
|
||||
groupname = parts[0]
|
||||
actorname = parts[1]
|
||||
methodname = parts[2]
|
||||
} else {
|
||||
return new_error(request.id, invalid_params)
|
||||
}
|
||||
procedure_group := handler.procedures_group[groupname] or {
|
||||
return new_error(request.id, RPCError{
|
||||
code: -32602
|
||||
message: 'Could not find procedure group ${groupname} in function on rpc request.'
|
||||
data: '${request}'
|
||||
})
|
||||
}
|
||||
return procedure_group(request.id, handler.servercontext, actorname, methodname,
|
||||
request.params) or {
|
||||
// Return proper JSON-RPC error instead of panicking
|
||||
return new_error(request.id, RPCError{
|
||||
code: -32603
|
||||
message: 'Error in function on rpc request.'
|
||||
data: '${request}\n${err}'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
procedure_func := handler.procedures[request.method] or {
|
||||
return new_error(request.id, method_not_found)
|
||||
}
|
||||
|
||||
// Execute the procedure handler with the request payload
|
||||
return procedure_func(handler.params, request) or {
|
||||
return procedure_func(request) or {
|
||||
// Return proper JSON-RPC error instead of panicking
|
||||
return new_error(request.id, RPCError{
|
||||
code: -32603
|
||||
|
||||
@@ -51,6 +51,7 @@ pub const internal_error = RPCError{
|
||||
// RPCError represents a JSON-RPC 2.0 error object as defined in the specification.
|
||||
// Error objects contain a code, message, and optional data field to provide
|
||||
// more information about the error that occurred.
|
||||
@[params]
|
||||
pub struct RPCError {
|
||||
pub mut:
|
||||
// Numeric error code. Predefined codes are in the range -32768 to -32000.
|
||||
|
||||
@@ -22,8 +22,6 @@ pub mut:
|
||||
// An identifier established by the client that must be included in the response
|
||||
// This is used to correlate requests with their corresponding responses
|
||||
id int @[required]
|
||||
|
||||
handler_params map[string]string
|
||||
}
|
||||
|
||||
// new_request creates a new JSON-RPC request with the specified method and parameters.
|
||||
@@ -52,7 +50,19 @@ pub fn new_request(method string, params string) Request {
|
||||
// Returns:
|
||||
// - A Request object or an error if parsing fails
|
||||
pub fn decode_request(data string) !Request {
|
||||
return json2.decode[Request](data)!
|
||||
mut r2 := json2.decode[json2.Any](data)!
|
||||
mut r3 := r2.as_map()
|
||||
a := r3['jsonrpc'].str()
|
||||
b := r3['method'].str()
|
||||
c := r3['params'].str()
|
||||
d := r3['id'].int()
|
||||
mut r4 := Request{
|
||||
jsonrpc: a
|
||||
method: b
|
||||
params: c
|
||||
id: d
|
||||
}
|
||||
return r4
|
||||
}
|
||||
|
||||
// encode serializes the Request object into a JSON string.
|
||||
|
||||
@@ -48,6 +48,14 @@ pub fn new_response_true(id int) Response {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_response_ok(id int) Response {
|
||||
return Response{
|
||||
jsonrpc: jsonrpc_version
|
||||
result: ''
|
||||
id: id
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_response_false(id int) Response {
|
||||
return Response{
|
||||
jsonrpc: jsonrpc_version
|
||||
@@ -88,6 +96,9 @@ pub fn new_error_response(id int, error RPCError) Response {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// decode_response parses a JSON string into a Response object.
|
||||
// This function handles the complex validation rules for JSON-RPC responses.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user