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

Method hook_typed

plugins/src/lib.rs:236–274  ·  view source on GitHub ↗

Add a hook subscription for `hookname` with typed request and response. The request is deserialized from JSON into `Req` and the response is serialized from `Resp` back to JSON automatically. If deserialization of the request fails, an error is returned and handled according to the hook fallback policy. Use [`Builder::hook_from_builder`] with [`HookBuilder::new_typed`] if you additionally need to

(mut self, hookname: &str, callback: C)

Source from the content-addressed store, hash-verified

234 /// Use [`Builder::hook_from_builder`] with [`HookBuilder::new_typed`] if you additionally need
235 /// to configure `before`, `after`, or `filters`.
236 pub fn hook_typed<C, F, Req, Resp>(mut self, hookname: &str, callback: C) -> Self
237 where
238 C: Send + Sync + 'static,
239 C: Fn(Plugin<S>, Req) -> F + 'static,
240 F: Future<Output = Result<Resp, Error>> + Send + 'static,
241 Req: DeserializeOwned + Send + 'static,
242 Resp: Serialize + Send + 'static,
243 {
244 let hookname = hookname.to_string();
245 self.hooks.insert(
246 hookname.clone(),
247 Hook {
248 name: hookname.clone(),
249 callback: Box::new(move |p, r| {
250 let hookname = hookname.clone();
251 let typed_req = match serde_json::from_value(r) {
252 Ok(req) => req,
253 Err(e) => {
254 let error = format!(
255 "cln-plugin: hook '{hookname}' received a request that doesn't \
256 match the expected schema. Error: {e}"
257 );
258 log::warn!("{error}");
259 return Box::pin(async move { Err(anyhow!("{error}")) });
260 }
261 };
262 let fut = callback(p, typed_req);
263 Box::pin(async move {
264 let typed_resp = fut.await?;
265 serde_json::to_value(typed_resp).map_err(Error::from)
266 })
267 }),
268 before: Vec::new(),
269 after: Vec::new(),
270 filters: None,
271 },
272 );
273 self
274 }
275
276 /// Register a custom RPC method for the RPC passthrough from the
277 /// main daemon

Calls 2

to_stringMethod · 0.45
insertMethod · 0.45

Tested by 1