refactor: Update method names and add curl example generation

- Rename API method names using dot notation
- Add endpoint_url and curl_example to DocMethod
- Implement generate_curl_example function
- Update DocMethod struct with new fields
This commit is contained in:
Mahmoud-Emad
2025-10-21 15:39:59 +03:00
parent a120ef2676
commit 8ef9522676
3 changed files with 106 additions and 71 deletions

View File

@@ -114,7 +114,8 @@ fn process_method(method openrpc.Method, config DocConfig) !DocMethod {
// Example is always generated by extract_example_from_schema
example_response := doc_result.example
// example_call := generate_example_call(doc_params)
endpoint_url := '${config.base_url}/api/${config.handler_type}'
curl_example := generate_curl_example(method.name, example_request, endpoint_url)
doc_method := DocMethod{
name: method.name
@@ -124,13 +125,10 @@ fn process_method(method openrpc.Method, config DocConfig) !DocMethod {
result: doc_result
example_response: example_response
example_request: example_request
endpoint_url: endpoint_url
curl_example: curl_example
}
// endpoint_url: '${config.base_url}/api/${config.handler_type}'
// example_call: example_call
// curl_example: generate_curl_example_jsonrpc(method.name, doc_params, config.base_url,
// config.handler_type)
return doc_method
}

View File

@@ -35,6 +35,43 @@ fn prettify_json(compact_json string) string {
// Request Example Generation
// ============================================================================
// generate_curl_example creates a working curl command for a JSON-RPC method.
// Takes the method name, example request payload (params only), and endpoint URL.
// Returns a properly formatted curl command with JSON-RPC 2.0 wrapper.
pub fn generate_curl_example(method_name string, params_json string, endpoint_url string) string {
// Build the complete JSON-RPC request
mut jsonrpc_request := '{\n'
jsonrpc_request += ' "jsonrpc": "2.0",\n'
jsonrpc_request += ' "method": "${method_name}",\n'
jsonrpc_request += ' "params": '
// Add the params (already formatted with proper indentation)
// Need to indent each line by 2 spaces to align with the "params" key
params_lines := params_json.split('\n')
for i, line in params_lines {
if i == 0 {
jsonrpc_request += line
} else {
jsonrpc_request += '\n ' + line
}
}
// Add comma after params and then id field
jsonrpc_request += ',\n'
jsonrpc_request += ' "id": 1\n'
jsonrpc_request += '}'
// Escape single quotes for shell
escaped_request := jsonrpc_request.replace("'", "'\\''")
// Build curl command
mut curl := "curl -X POST '${endpoint_url}' \\\n"
curl += " -H 'Content-Type: application/json' \\\n"
curl += " -d '${escaped_request}'"
return curl
}
fn generate_request_example[T](model T) !string {
mut field_parts := []string{} // Build JSON manually to avoid type conflicts