zinit client fixes

This commit is contained in:
Timur Gordon
2025-08-29 10:17:51 +02:00
parent 3d86ec7cf5
commit b0b1fbf2c2
2 changed files with 30 additions and 6 deletions

View File

@@ -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)!
}

View File

@@ -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()!
}