jsonrpc.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. use log::debug;
  2. use serde_json::{json, Value};
  3. use url::Url;
  4. use darkfi::{
  5. rpc::jsonrpc::{self, JsonResult},
  6. Error, Result,
  7. };
  8. pub async fn request(r: jsonrpc::JsonRequest, url: String) -> Result<Value> {
  9. let reply: JsonResult = match jsonrpc::send_request(&Url::parse(&url)?, json!(r), None).await {
  10. Ok(v) => v,
  11. Err(e) => return Err(e),
  12. };
  13. match reply {
  14. JsonResult::Resp(r) => {
  15. debug!(target: "RPC", "<-- {}", serde_json::to_string(&r)?);
  16. Ok(r.result)
  17. }
  18. JsonResult::Err(e) => {
  19. debug!(target: "RPC", "<-- {}", serde_json::to_string(&e)?);
  20. Err(Error::JsonRpcError(e.error.message.to_string()))
  21. }
  22. JsonResult::Notif(n) => {
  23. debug!(target: "RPC", "<-- {}", serde_json::to_string(&n)?);
  24. Err(Error::JsonRpcError("Unexpected reply".to_string()))
  25. }
  26. }
  27. }
  28. // RPCAPI:
  29. // Add new task and returns `true` upon success.
  30. // --> {"jsonrpc": "2.0", "method": "add",
  31. // "params":
  32. // [{
  33. // "title": "..",
  34. // "desc": "..",
  35. // assign: [..],
  36. // project: [..],
  37. // "due": ..,
  38. // "rank": ..
  39. // }],
  40. // "id": 1
  41. // }
  42. // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
  43. pub async fn add(url: &str, params: Value) -> Result<Value> {
  44. let req = jsonrpc::request(json!("add"), params);
  45. request(req, url.to_string()).await
  46. }
  47. // List tasks
  48. // --> {"jsonrpc": "2.0", "method": "list", "params": [], "id": 1}
  49. // <-- {"jsonrpc": "2.0", "result": [task, ...], "id": 1}
  50. pub async fn list(url: &str, params: Value) -> Result<Value> {
  51. let req = jsonrpc::request(json!("list"), json!(params));
  52. request(req, url.to_string()).await
  53. }
  54. // Update task and returns `true` upon success.
  55. // --> {"jsonrpc": "2.0", "method": "update", "params": [task_id, {"title": "new title"} ], "id": 1}
  56. // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
  57. pub async fn update(url: &str, id: u64, data: Value) -> Result<Value> {
  58. let req = jsonrpc::request(json!("update"), json!([id, data]));
  59. request(req, url.to_string()).await
  60. }
  61. // Set state for a task and returns `true` upon success.
  62. // --> {"jsonrpc": "2.0", "method": "set_state", "params": [task_id, state], "id": 1}
  63. // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
  64. pub async fn set_state(url: &str, id: u64, state: &str) -> Result<Value> {
  65. let req = jsonrpc::request(json!("set_state"), json!([id, state]));
  66. request(req, url.to_string()).await
  67. }
  68. // Get task's state.
  69. // --> {"jsonrpc": "2.0", "method": "get_state", "params": [task_id], "id": 1}
  70. // <-- {"jsonrpc": "2.0", "result": "state", "id": 1}
  71. pub async fn get_state(url: &str, id: u64) -> Result<Value> {
  72. let req = jsonrpc::request(json!("get_state"), json!([id]));
  73. request(req, url.to_string()).await
  74. }
  75. // Set comment for a task and returns `true` upon success.
  76. // --> {"jsonrpc": "2.0", "method": "set_comment", "params": [task_id, comment_author, comment_content], "id": 1}
  77. // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
  78. pub async fn set_comment(url: &str, id: u64, author: &str, content: &str) -> Result<Value> {
  79. let req = jsonrpc::request(json!("set_comment"), json!([id, author, content]));
  80. request(req, url.to_string()).await
  81. }
  82. // Get task by id.
  83. // --> {"jsonrpc": "2.0", "method": "get_by_id", "params": [task_id], "id": 1}
  84. // <-- {"jsonrpc": "2.0", "result": "task", "id": 1}
  85. pub async fn get_by_id(url: &str, id: u64) -> Result<Value> {
  86. let req = jsonrpc::request(json!("get_by_id"), json!([id]));
  87. request(req, url.to_string()).await
  88. }