rpc.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use log::debug;
  19. use serde_json::json;
  20. use darkfi::{rpc::jsonrpc::JsonRequest, Result};
  21. use crate::{
  22. primitives::{BaseTask, State, TaskInfo},
  23. Tau,
  24. };
  25. impl Tau {
  26. pub async fn close_connection(&self) -> Result<()> {
  27. self.rpc_client.close().await
  28. }
  29. /// Add a new task.
  30. pub async fn add(&self, task: BaseTask) -> Result<()> {
  31. let req = JsonRequest::new("add", json!([task]));
  32. let rep = self.rpc_client.request(req).await?;
  33. debug!("Got reply: {:?}", rep);
  34. Ok(())
  35. }
  36. /// Get current open tasks ids.
  37. pub async fn get_ids(&self) -> Result<Vec<u64>> {
  38. let req = JsonRequest::new("get_ids", json!([]));
  39. let rep = self.rpc_client.request(req).await?;
  40. let mut ret = vec![];
  41. for i in rep.as_array().unwrap() {
  42. ret.push(i.as_u64().unwrap());
  43. }
  44. Ok(ret)
  45. }
  46. /// Update existing task given it's ID and some params.
  47. pub async fn update(&self, id: u64, task: BaseTask) -> Result<()> {
  48. let req = JsonRequest::new("update", json!([id, task]));
  49. let rep = self.rpc_client.request(req).await?;
  50. debug!("Got reply: {:?}", rep);
  51. Ok(())
  52. }
  53. /// Set the state for a task.
  54. pub async fn set_state(&self, id: u64, state: &State) -> Result<()> {
  55. let req = JsonRequest::new("set_state", json!([id, state.to_string()]));
  56. let rep = self.rpc_client.request(req).await?;
  57. debug!("Got reply: {:?}", rep);
  58. Ok(())
  59. }
  60. /// Set a comment for a task.
  61. pub async fn set_comment(&self, id: u64, content: &str) -> Result<()> {
  62. let req = JsonRequest::new("set_comment", json!([id, content]));
  63. let rep = self.rpc_client.request(req).await?;
  64. debug!("Got reply: {:?}", rep);
  65. Ok(())
  66. }
  67. /// Get task data by its ID.
  68. pub async fn get_task_by_id(&self, id: u64) -> Result<TaskInfo> {
  69. let req = JsonRequest::new("get_task_by_id", json!([id]));
  70. let rep = self.rpc_client.request(req).await?;
  71. Ok(serde_json::from_value(rep)?)
  72. }
  73. /// Get month's stopped tasks.
  74. pub async fn get_stop_tasks(&self, month: Option<i64>) -> Result<Vec<TaskInfo>> {
  75. let req = JsonRequest::new("get_stop_tasks", json!([month]));
  76. let rep = self.rpc_client.request(req).await?;
  77. Ok(serde_json::from_value(rep)?)
  78. }
  79. /// Switch workspace.
  80. pub async fn switch_ws(&self, workspace: String) -> Result<()> {
  81. let req = JsonRequest::new("switch_ws", json!([workspace]));
  82. let rep = self.rpc_client.request(req).await?;
  83. debug!("Got reply: {:?}", rep);
  84. Ok(())
  85. }
  86. /// Get current workspace.
  87. pub async fn get_ws(&self) -> Result<String> {
  88. let req = JsonRequest::new("get_ws", json!([]));
  89. let rep = self.rpc_client.request(req).await?;
  90. Ok(serde_json::from_value(rep)?)
  91. }
  92. /// Export tasks.
  93. pub async fn export_to(&self, path: String) -> Result<bool> {
  94. let req = JsonRequest::new("export", json!([path]));
  95. let rep = self.rpc_client.request(req).await?;
  96. debug!("Got reply: {:?}", rep);
  97. Ok(serde_json::from_value(rep)?)
  98. }
  99. /// Import tasks.
  100. pub async fn import_from(&self, path: String) -> Result<bool> {
  101. let req = JsonRequest::new("import", json!([path]));
  102. let rep = self.rpc_client.request(req).await?;
  103. debug!("Got reply: {:?}", rep);
  104. Ok(serde_json::from_value(rep)?)
  105. }
  106. }