jsonrpc.rs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. use async_trait::async_trait;
  2. use log::error;
  3. use serde_json::{json, Value};
  4. use darkfi::{
  5. rpc::{
  6. jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult},
  7. server::RequestHandler,
  8. },
  9. Error,
  10. };
  11. pub struct JsonRpcInterface {
  12. sender: async_channel::Sender<String>,
  13. receiver: async_channel::Receiver<Vec<Vec<(String, String)>>>,
  14. }
  15. #[async_trait]
  16. impl RequestHandler for JsonRpcInterface {
  17. async fn handle_request(&self, req: JsonRequest) -> JsonResult {
  18. if !req.params.is_array() {
  19. return JsonError::new(ErrorCode::InvalidParams, None, req.id).into()
  20. }
  21. let params = req.params.as_array().unwrap();
  22. let rep = match req.method.as_str() {
  23. Some("update") => self.update(req.id, params).await,
  24. Some("dry_run") => self.dry_run(req.id, params).await,
  25. Some("log") => self.log(req.id, params).await,
  26. Some(_) | None => return JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
  27. };
  28. rep
  29. }
  30. }
  31. impl JsonRpcInterface {
  32. pub fn new(
  33. sender: async_channel::Sender<String>,
  34. receiver: async_channel::Receiver<Vec<Vec<(String, String)>>>,
  35. ) -> Self {
  36. Self { sender, receiver }
  37. }
  38. // RPCAPI:
  39. // Update files in ~/darkwiki
  40. // --> {"jsonrpc": "2.0", "method": "update", "params": [], "id": 1}
  41. // <-- {"jsonrpc": "2.0", "result": [[(String, String)]], "id": 1}
  42. async fn update(&self, id: Value, _params: &[Value]) -> JsonResult {
  43. let res = self.sender.send("update".into()).await.map_err(Error::from);
  44. if let Err(e) = res {
  45. error!("Failed to update: {}", e);
  46. return JsonError::new(ErrorCode::InternalError, None, id).into()
  47. }
  48. let response = self.receiver.recv().await.unwrap();
  49. JsonResponse::new(json!(response), id).into()
  50. }
  51. // RPCAPI:
  52. // Update files in darkwiki (dry_run)
  53. // --> {"jsonrpc": "2.0", "method": "dry_run", "params": [], "id": 1}
  54. // <-- {"jsonrpc": "2.0", "result": [[(String, String)]], "id": 1}
  55. async fn dry_run(&self, id: Value, _params: &[Value]) -> JsonResult {
  56. let res = self.sender.send("dry_run".into()).await.map_err(Error::from);
  57. if let Err(e) = res {
  58. error!("Failed to update(dry run): {}", e);
  59. return JsonError::new(ErrorCode::InternalError, None, id).into()
  60. }
  61. let response = self.receiver.recv().await.unwrap();
  62. JsonResponse::new(json!(response), id).into()
  63. }
  64. // RPCAPI:
  65. // Show all patches
  66. // --> {"jsonrpc": "2.0", "method": "log", "params": [], "id": 1}
  67. // <-- {"jsonrpc": "2.0", "result": [[(String, String)]], "id": 1}
  68. async fn log(&self, id: Value, _params: &[Value]) -> JsonResult {
  69. let res = self.sender.send("log".into()).await.map_err(Error::from);
  70. if let Err(e) = res {
  71. error!("Failed to show all patches: {}", e);
  72. return JsonError::new(ErrorCode::InternalError, None, id).into()
  73. }
  74. let response = self.receiver.recv().await.unwrap();
  75. JsonResponse::new(json!(response), id).into()
  76. }
  77. }