rpc.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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::{from_value, 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<bool> {
  31. let req = JsonRequest::new("add", json!([task]));
  32. let rep = self.rpc_client.request(req).await?;
  33. debug!("Got reply: {:?}", rep);
  34. let reply: bool = from_value(rep)?;
  35. Ok(reply)
  36. }
  37. /// Get current open tasks ids.
  38. pub async fn get_ids(&self) -> Result<Vec<u64>> {
  39. let req = JsonRequest::new("get_ids", json!([]));
  40. let rep = self.rpc_client.request(req).await?;
  41. let mut ret = vec![];
  42. for i in rep.as_array().unwrap() {
  43. ret.push(i.as_u64().unwrap());
  44. }
  45. Ok(ret)
  46. }
  47. /// Update existing task given it's ID and some params.
  48. pub async fn update(&self, id: u64, task: BaseTask) -> Result<bool> {
  49. let req = JsonRequest::new("update", json!([id, task]));
  50. let rep = self.rpc_client.request(req).await?;
  51. debug!("Got reply: {:?}", rep);
  52. let reply: bool = from_value(rep)?;
  53. Ok(reply)
  54. }
  55. /// Set the state for a task.
  56. pub async fn set_state(&self, id: u64, state: &State) -> Result<bool> {
  57. let req = JsonRequest::new("set_state", json!([id, state.to_string()]));
  58. let rep = self.rpc_client.request(req).await?;
  59. debug!("Got reply: {:?}", rep);
  60. let reply: bool = from_value(rep)?;
  61. Ok(reply)
  62. }
  63. /// Set a comment for a task.
  64. pub async fn set_comment(&self, id: u64, content: &str) -> Result<bool> {
  65. let req = JsonRequest::new("set_comment", json!([id, content]));
  66. let rep = self.rpc_client.request(req).await?;
  67. debug!("Got reply: {:?}", rep);
  68. let reply: bool = from_value(rep)?;
  69. Ok(reply)
  70. }
  71. /// Get task data by its ID.
  72. pub async fn get_task_by_id(&self, id: u64) -> Result<TaskInfo> {
  73. let req = JsonRequest::new("get_task_by_id", json!([id]));
  74. let rep = self.rpc_client.request(req).await?;
  75. Ok(serde_json::from_value(rep)?)
  76. }
  77. /// Get month's stopped tasks.
  78. pub async fn get_stop_tasks(&self, month: Option<i64>) -> Result<Vec<TaskInfo>> {
  79. let req = JsonRequest::new("get_stop_tasks", json!([month]));
  80. let rep = self.rpc_client.request(req).await?;
  81. Ok(serde_json::from_value(rep)?)
  82. }
  83. /// Switch workspace.
  84. pub async fn switch_ws(&self, workspace: String) -> Result<bool> {
  85. let req = JsonRequest::new("switch_ws", json!([workspace]));
  86. let rep = self.rpc_client.request(req).await?;
  87. debug!("Got reply: {:?}", rep);
  88. let reply: bool = from_value(rep)?;
  89. Ok(reply)
  90. }
  91. /// Get current workspace.
  92. pub async fn get_ws(&self) -> Result<String> {
  93. let req = JsonRequest::new("get_ws", json!([]));
  94. let rep = self.rpc_client.request(req).await?;
  95. Ok(serde_json::from_value(rep)?)
  96. }
  97. /// Export tasks.
  98. pub async fn export_to(&self, path: String) -> Result<bool> {
  99. let req = JsonRequest::new("export", json!([path]));
  100. let rep = self.rpc_client.request(req).await?;
  101. debug!("Got reply: {:?}", rep);
  102. Ok(serde_json::from_value(rep)?)
  103. }
  104. /// Import tasks.
  105. pub async fn import_from(&self, path: String) -> Result<bool> {
  106. let req = JsonRequest::new("import", json!([path]));
  107. let rep = self.rpc_client.request(req).await?;
  108. debug!("Got reply: {:?}", rep);
  109. Ok(serde_json::from_value(rep)?)
  110. }
  111. }