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

Function convert_json_to_response

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

Source from the content-addressed store, hash-verified

305}
306
307fn convert_json_to_response(
308 headers: axum::http::HeaderMap,
309 rpc_method: &str,
310 cln_result: serde_json::Value,
311) -> Result<Response, AppError> {
312 let accept = headers
313 .get("accept")
314 .and_then(|v| v.to_str().ok())
315 .unwrap_or("application/json");
316
317 let format = match accept {
318 a if a.contains("*/*") => "json",
319 a if a.contains("application/json") => "json",
320 a if a.contains("application/yaml") => "yaml",
321 a if a.contains("application/xml") => "xml",
322 a if a.contains("application/x-www-form-urlencoded") => "form",
323 _ => {
324 return Err(AppError::NotAcceptable(RpcError {
325 code: None,
326 data: None,
327 message: format!("Unsupported accept header: {}", accept),
328 }));
329 }
330 };
331
332 match format {
333 "yaml" => match serde_yaml_ng::to_string(&cln_result) {
334 Ok(yaml) => Ok((
335 StatusCode::CREATED,
336 [("Content-Type", "application/yaml")],
337 yaml,
338 )
339 .into_response()),
340 Err(e) => Err(AppError::InternalServerError(RpcError {
341 code: None,
342 data: None,
343 message: format!("Could not serialize to YAML: {}", e),
344 })),
345 },
346 "xml" => match quick_xml::se::to_string_with_root(rpc_method, &cln_result) {
347 Ok(xml) => Ok((
348 StatusCode::CREATED,
349 [("Content-Type", "application/xml")],
350 xml,
351 )
352 .into_response()),
353 Err(e) => Err(AppError::InternalServerError(RpcError {
354 code: None,
355 data: None,
356 message: format!("Could not serialize to XML: {}", e),
357 })),
358 },
359 "form" => match serde_qs::to_string(&cln_result) {
360 Ok(form) => Ok((
361 StatusCode::CREATED,
362 [("Content-Type", "application/x-www-form-urlencoded")],
363 form,
364 )

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