main.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. use clap::{IntoApp, Parser, Subcommand};
  2. use log::debug;
  3. use serde_json::{json, Value};
  4. use url::Url;
  5. use darkfi::{
  6. rpc::{jsonrpc, jsonrpc::JsonResult},
  7. Error, Result,
  8. };
  9. #[derive(Subcommand)]
  10. pub enum CliDaoSubCommands {
  11. /// Say hello to the RPC
  12. Hello {},
  13. }
  14. /// DAO cli
  15. #[derive(Parser)]
  16. #[clap(name = "dao")]
  17. #[clap(arg_required_else_help(true))]
  18. pub struct CliDao {
  19. /// Increase verbosity
  20. #[clap(short, parse(from_occurrences))]
  21. pub verbose: u8,
  22. #[clap(subcommand)]
  23. pub command: Option<CliDaoSubCommands>,
  24. }
  25. pub struct Client {
  26. url: String,
  27. }
  28. impl Client {
  29. pub fn new(url: String) -> Self {
  30. Self { url }
  31. }
  32. async fn request(&self, r: jsonrpc::JsonRequest) -> Result<Value> {
  33. let reply: JsonResult =
  34. match jsonrpc::send_request(&Url::parse(&self.url)?, json!(r), None).await {
  35. Ok(v) => v,
  36. Err(e) => return Err(e),
  37. };
  38. match reply {
  39. JsonResult::Resp(r) => {
  40. debug!(target: "RPC", "<-- {}", serde_json::to_string(&r)?);
  41. Ok(r.result)
  42. }
  43. JsonResult::Err(e) => {
  44. debug!(target: "RPC", "<-- {}", serde_json::to_string(&e)?);
  45. Err(Error::JsonRpcError(e.error.message.to_string()))
  46. }
  47. JsonResult::Notif(n) => {
  48. debug!(target: "RPC", "<-- {}", serde_json::to_string(&n)?);
  49. Err(Error::JsonRpcError("Unexpected reply".to_string()))
  50. }
  51. }
  52. }
  53. // --> {"jsonrpc": "2.0", "method": "say_hello", "params": [], "id": 42}
  54. // <-- {"jsonrpc": "2.0", "result": "hello world", "id": 42}
  55. async fn say_hello(&self) -> Result<Value> {
  56. let req = jsonrpc::request(json!("say_hello"), json!([]));
  57. Ok(self.request(req).await?)
  58. }
  59. }
  60. async fn start(options: CliDao) -> Result<()> {
  61. let rpc_addr = "tcp://127.0.0.1:7777";
  62. let client = Client::new(rpc_addr.to_string());
  63. match options.command {
  64. Some(CliDaoSubCommands::Hello {}) => {
  65. let reply = client.say_hello().await?;
  66. println!("Server replied: {}", &reply.to_string());
  67. return Ok(())
  68. }
  69. None => {}
  70. }
  71. Ok(())
  72. }
  73. #[async_std::main]
  74. async fn main() -> Result<()> {
  75. let args = CliDao::parse();
  76. let _matches = CliDao::command().get_matches();
  77. //let config_path = if args.config.is_some() {
  78. // expand_path(&args.config.clone().unwrap())?
  79. //} else {
  80. // join_config_path(&PathBuf::from("drk.toml"))?
  81. //};
  82. // Spawn config file if it's not in place already.
  83. //spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  84. //let (lvl, conf) = log_config(matches)?;
  85. //TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  86. //let config = Config::<DrkConfig>::load(config_path)?;
  87. start(args).await
  88. }