diff --git a/lib/clients/zinit/zinit.v b/lib/clients/zinit/zinit.v index 761f2dab..eb8cc9b3 100644 --- a/lib/clients/zinit/zinit.v +++ b/lib/clients/zinit/zinit.v @@ -1,7 +1,8 @@ module zinit +import json import freeflowuniverse.herolib.schemas.jsonrpc -import freeflowuniverse.herolib.schemas.jsonrpcmodel +import freeflowuniverse.herolib.schemas.openrpc // Helper function to get or create the RPC client fn (mut c ZinitRPC) client_() !&jsonrpc.Client { @@ -13,10 +14,11 @@ fn (mut c ZinitRPC) client_() !&jsonrpc.Client { // Admin methods // rpc_discover returns the OpenRPC specification for the API -pub fn (mut c ZinitRPC) rpc_discover() !jsonrpcmodel.OpenRPCSpec { +pub fn (mut c ZinitRPC) rpc_discover() !openrpc.OpenRPC { mut client := c.client_()! - request := jsonrpc.new_request_generic('rpc.discover', []string{}) - return client.send[[]string, jsonrpcmodel.OpenRPCSpec](request)! + request := jsonrpc.new_request('rpc.discover', '') + openrpc_str := client.send_str(request)! + return json.decode(openrpc.OpenRPC, openrpc_str) } // service_list lists all services managed by Zinit @@ -98,9 +100,9 @@ pub fn (mut c ZinitRPC) service_create(name string, config ServiceConfig) !strin content: config } println(params) - $dbg; + // $dbg; request := jsonrpc.new_request_generic('service_create', params) - $dbg; + // $dbg; return client.send[ServiceCreateParams, string](request)! } diff --git a/lib/schemas/jsonrpc/client.v b/lib/schemas/jsonrpc/client.v index e924d9bc..52a92594 100644 --- a/lib/schemas/jsonrpc/client.v +++ b/lib/schemas/jsonrpc/client.v @@ -84,3 +84,25 @@ pub fn (mut c Client) send[T, D](request RequestGeneric[T], params SendParams) ! // Return the result or propagate any error from the response return response.result()! } + +pub fn (mut c Client) send_str(request Request, params SendParams) !string { + // Send the encoded request through the transport layer + console.print_debug('Sending request: ${request.encode()}') + response_json := c.transport.send(request.encode(), params)! + + // Decode the response JSON into a strongly-typed response object + response := decode_response(response_json) or { + return error('Unable to decode response.\n- Response: ${response_json}\n- Error: ${err}') + } + + // Validate the response according to the JSON-RPC specification + response.validate() or { return error('Received invalid response: ${err}') } + + // Ensure the response ID matches the request ID to prevent response/request mismatch + if response.id != request.id { + return error('Received response with different id ${response}') + } + + // Return the result or propagate any error from the response + return response.result()! +}