MCPcopy Create free account
hub / github.com/ElementsProject/lightning / convert_json_to_response

Function convert_json_to_response

plugins/rest-plugin/src/handlers.rs:463–539  ·  view source on GitHub ↗
(
    headers: axum::http::HeaderMap,
    rpc_method: &str,
    cln_result: serde_json::Value,
)

Source from the content-addressed store, hash-verified

461}
462
463fn convert_json_to_response(
464 headers: axum::http::HeaderMap,
465 rpc_method: &str,
466 cln_result: serde_json::Value,
467) -> Result<Response, AppError> {
468 let accept = headers
469 .get("accept")
470 .and_then(|v| v.to_str().ok())
471 .unwrap_or("application/json");
472
473 let format = match accept {
474 a if a.contains("*/*") => "json",
475 a if a.contains("application/json") => "json",
476 a if a.contains("application/yaml") => "yaml",
477 a if a.contains("application/xml") => "xml",
478 a if a.contains("application/x-www-form-urlencoded") => "form",
479 _ => {
480 return Err(AppError::NotAcceptable(RpcError {
481 code: None,
482 data: None,
483 message: format!("Unsupported accept header: {}", accept),
484 }));
485 }
486 };
487
488 match format {
489 "yaml" => match serde_yaml_ng::to_string(&cln_result) {
490 Ok(yaml) => Ok((
491 StatusCode::CREATED,
492 [("Content-Type", "application/yaml")],
493 yaml,
494 )
495 .into_response()),
496 Err(e) => Err(AppError::InternalServerError(RpcError {
497 code: None,
498 data: None,
499 message: format!("Could not serialize to YAML: {}", e),
500 })),
501 },
502 "xml" => match quick_xml::se::to_string_with_root(rpc_method, &cln_result) {
503 Ok(xml) => Ok((
504 StatusCode::CREATED,
505 [("Content-Type", "application/xml")],
506 xml,
507 )
508 .into_response()),
509 Err(e) => Err(AppError::InternalServerError(RpcError {
510 code: None,
511 data: None,
512 message: format!("Could not serialize to XML: {}", e),
513 })),
514 },
515 "form" => match serde_qs::to_string(&cln_result) {
516 Ok(form) => Ok((
517 StatusCode::CREATED,
518 [("Content-Type", "application/x-www-form-urlencoded")],
519 form,
520 )

Callers 1

call_rpc_methodFunction · 0.85

Calls 5

to_strMethod · 0.80
containsMethod · 0.80
into_responseMethod · 0.80
to_stringFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected