jsonrpc.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, bool, Vec<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("restore") => self.restore(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, bool, Vec<String>)>,
  34. receiver: async_channel::Receiver<Vec<Vec<(String, String)>>>,
  35. ) -> Self {
  36. Self { sender, receiver }
  37. }
  38. // RPCAPI:
  39. // Update files
  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 dry = params[0].as_bool().unwrap();
  44. let files: Vec<String> =
  45. params[1].as_array().unwrap().iter().map(|f| f.as_str().unwrap().to_string()).collect();
  46. let res = self.sender.send(("update".into(), dry, files)).await.map_err(Error::from);
  47. if let Err(e) = res {
  48. error!("Failed to update: {}", e);
  49. return JsonError::new(ErrorCode::InternalError, None, id).into()
  50. }
  51. let response = self.receiver.recv().await.unwrap();
  52. JsonResponse::new(json!(response), id).into()
  53. }
  54. // RPCAPI:
  55. // Undo the local changes
  56. // --> {"jsonrpc": "2.0", "method": "restore", "params": [dry, files], "id": 1}
  57. // <-- {"jsonrpc": "2.0", "result": [String, ..], "id": 1}
  58. async fn restore(&self, id: Value, params: &[Value]) -> JsonResult {
  59. let dry = params[0].as_bool().unwrap();
  60. let files: Vec<String> =
  61. params[1].as_array().unwrap().iter().map(|f| f.as_str().unwrap().to_string()).collect();
  62. let res = self.sender.send(("restore".into(), dry, files)).await.map_err(Error::from);
  63. if let Err(e) = res {
  64. error!("Failed to restore: {}", e);
  65. return JsonError::new(ErrorCode::InternalError, None, id).into()
  66. }
  67. let response = self.receiver.recv().await.unwrap();
  68. JsonResponse::new(json!(response), id).into()
  69. }
  70. // RPCAPI:
  71. // Show all patches
  72. // --> {"jsonrpc": "2.0", "method": "log", "params": [dry, files], "id": 1}
  73. // <-- {"jsonrpc": "2.0", "result": [[(String, String)]], "id": 1}
  74. async fn log(&self, id: Value, _params: &[Value]) -> JsonResult {
  75. JsonResponse::new(json!(true), id).into()
  76. }
  77. }