jsonrpc.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. use serde_json::{json, Value};
  2. use darkfi::{
  3. rpc::{jsonrpc, rpcclient::RpcClient},
  4. Result,
  5. };
  6. pub struct Rpc {
  7. pub client: RpcClient,
  8. }
  9. impl Rpc {
  10. // RPCAPI:
  11. // Add new task and returns `true` upon success.
  12. // --> {"jsonrpc": "2.0", "method": "add",
  13. // "params":
  14. // [{
  15. // "title": "..",
  16. // "desc": "..",
  17. // assign: [..],
  18. // project: [..],
  19. // "due": ..,
  20. // "rank": ..
  21. // }],
  22. // "id": 1
  23. // }
  24. // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
  25. pub async fn add(&self, params: Value) -> Result<Value> {
  26. let req = jsonrpc::request(json!("add"), params);
  27. self.client.request(req).await
  28. }
  29. // List tasks
  30. // --> {"jsonrpc": "2.0", "method": "get_ids", "params": [], "id": 1}
  31. // <-- {"jsonrpc": "2.0", "result": [task_id, ...], "id": 1}
  32. pub async fn get_ids(&self, params: Value) -> Result<Value> {
  33. let req = jsonrpc::request(json!("get_ids"), json!(params));
  34. self.client.request(req).await
  35. }
  36. // Update task and returns `true` upon success.
  37. // --> {"jsonrpc": "2.0", "method": "update", "params": [task_id, {"title": "new title"} ], "id": 1}
  38. // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
  39. pub async fn update(&self, id: u64, data: Value) -> Result<Value> {
  40. let req = jsonrpc::request(json!("update"), json!([id, data]));
  41. self.client.request(req).await
  42. }
  43. // Set state for a task and returns `true` upon success.
  44. // --> {"jsonrpc": "2.0", "method": "set_state", "params": [task_id, state], "id": 1}
  45. // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
  46. pub async fn set_state(&self, id: u64, state: &str) -> Result<Value> {
  47. let req = jsonrpc::request(json!("set_state"), json!([id, state]));
  48. self.client.request(req).await
  49. }
  50. // Set comment for a task and returns `true` upon success.
  51. // --> {"jsonrpc": "2.0", "method": "set_comment", "params": [task_id, comment_content], "id": 1}
  52. // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
  53. pub async fn set_comment(&self, id: u64, content: &str) -> Result<Value> {
  54. let req = jsonrpc::request(json!("set_comment"), json!([id, content]));
  55. self.client.request(req).await
  56. }
  57. // Get task by id.
  58. // --> {"jsonrpc": "2.0", "method": "get_task_by_id", "params": [task_id], "id": 1}
  59. // <-- {"jsonrpc": "2.0", "result": "task", "id": 1}
  60. pub async fn get_task_by_id(&self, id: u64) -> Result<Value> {
  61. let req = jsonrpc::request(json!("get_task_by_id"), json!([id]));
  62. self.client.request(req).await
  63. }
  64. }