rpc.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use log::debug;
  2. use serde_json::json;
  3. use darkfi::{rpc::jsonrpc::JsonRequest, Result};
  4. use crate::{
  5. primitives::{BaseTask, State, TaskInfo},
  6. Tau,
  7. };
  8. impl Tau {
  9. pub async fn close_connection(&self) -> Result<()> {
  10. self.rpc_client.close().await
  11. }
  12. /// Add a new task.
  13. pub async fn add(&self, task: BaseTask) -> Result<()> {
  14. let req = JsonRequest::new("add", json!([task]));
  15. let rep = self.rpc_client.request(req).await?;
  16. debug!("Got reply: {:?}", rep);
  17. Ok(())
  18. }
  19. /// Get current open tasks ids.
  20. pub async fn get_ids(&self) -> Result<Vec<u64>> {
  21. let req = JsonRequest::new("get_ids", json!([]));
  22. let rep = self.rpc_client.request(req).await?;
  23. let mut ret = vec![];
  24. for i in rep.as_array().unwrap() {
  25. ret.push(i.as_u64().unwrap());
  26. }
  27. Ok(ret)
  28. }
  29. /// Update existing task given it's ID and some params.
  30. pub async fn update(&self, id: u64, task: BaseTask) -> Result<()> {
  31. let req = JsonRequest::new("update", json!([id, task]));
  32. let rep = self.rpc_client.request(req).await?;
  33. debug!("Got reply: {:?}", rep);
  34. Ok(())
  35. }
  36. /// Set the state for a task.
  37. pub async fn set_state(&self, id: u64, state: &State) -> Result<()> {
  38. let req = JsonRequest::new("set_state", json!([id, state.to_string()]));
  39. let rep = self.rpc_client.request(req).await?;
  40. debug!("Got reply: {:?}", rep);
  41. Ok(())
  42. }
  43. /// Set a comment for a task.
  44. pub async fn set_comment(&self, id: u64, content: &str) -> Result<()> {
  45. let req = JsonRequest::new("set_comment", json!([id, content]));
  46. let rep = self.rpc_client.request(req).await?;
  47. debug!("Got reply: {:?}", rep);
  48. Ok(())
  49. }
  50. /// Get task data by its ID.
  51. pub async fn get_task_by_id(&self, id: u64) -> Result<TaskInfo> {
  52. let req = JsonRequest::new("get_task_by_id", json!([id]));
  53. let rep = self.rpc_client.request(req).await?;
  54. Ok(serde_json::from_value(rep)?)
  55. }
  56. /// Get month's stopped tasks.
  57. pub async fn get_stop_tasks(&self, month: Option<i64>) -> Result<Vec<TaskInfo>> {
  58. let req = JsonRequest::new("get_stop_tasks", json!([month]));
  59. let rep = self.rpc_client.request(req).await?;
  60. Ok(serde_json::from_value(rep)?)
  61. }
  62. /// Switch workspace.
  63. pub async fn switch_ws(&self, workspace: String) -> Result<()> {
  64. let req = JsonRequest::new("switch_ws", json!([workspace]));
  65. let rep = self.rpc_client.request(req).await?;
  66. debug!("Got reply: {:?}", rep);
  67. Ok(())
  68. }
  69. /// Get current workspace.
  70. pub async fn get_ws(&self) -> Result<String> {
  71. let req = JsonRequest::new("get_ws", json!([]));
  72. let rep = self.rpc_client.request(req).await?;
  73. Ok(serde_json::from_value(rep)?)
  74. }
  75. /// Export tasks.
  76. pub async fn export_to(&self, path: String) -> Result<bool> {
  77. let req = JsonRequest::new("export", json!([path]));
  78. let rep = self.rpc_client.request(req).await?;
  79. debug!("Got reply: {:?}", rep);
  80. Ok(serde_json::from_value(rep)?)
  81. }
  82. /// Import tasks.
  83. pub async fn import_from(&self, path: String) -> Result<bool> {
  84. let req = JsonRequest::new("import", json!([path]));
  85. let rep = self.rpc_client.request(req).await?;
  86. debug!("Got reply: {:?}", rep);
  87. Ok(serde_json::from_value(rep)?)
  88. }
  89. }