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

Function call_rpc_method

plugins/rest-plugin/src/handlers.rs:106–161  ·  view source on GitHub ↗
(
    http_method: http::Method,
    Path(path): Path<String>,
    headers: axum::http::HeaderMap,
    Extension(plugin): Extension<Plugin<PluginState>>,
    body: Request<Body>,
)

Source from the content-addressed store, hash-verified

104 security(("api_key" = []))
105)]
106pub async fn call_rpc_method(
107 http_method: http::Method,
108 Path(path): Path<String>,
109 headers: axum::http::HeaderMap,
110 Extension(plugin): Extension<Plugin<PluginState>>,
111 body: Request<Body>,
112) -> Result<Response, AppError> {
113 let rune = headers
114 .get("rune")
115 .and_then(|v| v.to_str().ok())
116 .map(String::from);
117
118 let request_bytes = match to_bytes(body.into_body(), usize::MAX).await {
119 Ok(o) => o,
120 Err(e) => {
121 return Err(AppError::InternalServerError(RpcError {
122 code: None,
123 data: None,
124 message: format!("Could not read request body: {}", e),
125 }));
126 }
127 };
128
129 let (mut rest_map, mut rpc_params) = path_to_rest_map_and_params(&plugin, &path, &http_method)?;
130
131 request_body_to_rpc_params(
132 &mut rpc_params,
133 &headers,
134 &rest_map.rpc_method,
135 request_bytes,
136 )?;
137
138 fill_rune_restrictions(&mut rest_map, &rpc_params);
139
140 let mut rpc_params_value = json!(rpc_params);
141
142 filter_json(&mut rpc_params_value);
143
144 if rest_map.rune_required || http_method != http::Method::GET {
145 verify_rune(&plugin, rune, &rest_map.rune_restrictions.unwrap()).await?;
146 }
147
148 let cln_result = match call_rpc(&plugin, &rest_map.rpc_method, rpc_params_value).await {
149 Ok(result) => result,
150 Err(err) => {
151 if let Some(code) = err.code {
152 if code == -32601 {
153 return Err(AppError::NotFound(err));
154 }
155 }
156 return Err(AppError::InternalServerError(err));
157 }
158 };
159
160 convert_json_to_response(headers, &rest_map.rpc_method, cln_result)
161}
162
163fn fill_rune_restrictions(

Callers

nothing calls this directly

Calls 11

fill_rune_restrictionsFunction · 0.85
filter_jsonFunction · 0.85
verify_runeFunction · 0.85
call_rpcFunction · 0.85
convert_json_to_responseFunction · 0.85
mapMethod · 0.80
to_strMethod · 0.80
getMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected