From 147d55a5d9cc7ea280cd076d004e7086a33aab48 Mon Sep 17 00:00:00 2001 From: ghassmo Date: Tue, 6 Jul 2021 08:33:48 +0300 Subject: [PATCH] create Drk struct and add say_hello function --- src/bin/darkfi-cli.rs | 46 ++++++++++++++++++++++++++++++-- src/cli/client_cli/cli_config.rs | 2 +- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/bin/darkfi-cli.rs b/src/bin/darkfi-cli.rs index 5a245ee77..dd54170f8 100644 --- a/src/bin/darkfi-cli.rs +++ b/src/bin/darkfi-cli.rs @@ -3,11 +3,53 @@ use drk::Result; use async_executor::Executor; use easy_parallel::Parallel; +use log::*; use async_std::sync::Arc; +use std::collections::HashMap; use std::path::PathBuf; -async fn start(_executor: Arc>, _config: Arc) -> Result<()> { +type Payload = HashMap; + +struct Drk { + url: String, + payload: Payload, +} + +impl Drk { + pub fn new(url: String) -> Self { + let mut payload = HashMap::new(); + payload.insert(String::from("jsonrpc"), String::from("2.0")); + payload.insert(String::from("id"), String::from("0")); + Self { payload, url } + } + + pub async fn say_hello(&mut self) -> Result<()> { + self.payload + .insert(String::from("method"), String::from("say_hello")); + + self.request().await + } + + async fn request(&self) -> Result<()> { + let mut res = surf::post(&self.url) + .body(http_types::Body::from_json(&self.payload)?) + .await?; + + if res.status() == 200 { + let response = res.body_json::().await?; + info!("Response Result: {:?}", response); + } + Ok(()) + } +} + +async fn start(_executor: Arc>, config: Arc) -> Result<()> { + let url = config.rpc_url.clone(); + + let mut client = Drk::new(url); + client.say_hello().await?; + Ok(()) } @@ -30,7 +72,7 @@ fn main() -> Result<()> { let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build(); let debug_level = if options.verbose { - LevelFilter::Debug + LevelFilter::Info } else { LevelFilter::Off }; diff --git a/src/cli/client_cli/cli_config.rs b/src/cli/client_cli/cli_config.rs index 6ab7d1708..b88ff43c6 100644 --- a/src/cli/client_cli/cli_config.rs +++ b/src/cli/client_cli/cli_config.rs @@ -49,7 +49,7 @@ pub struct DarkfidCliConfig { impl Default for DarkfiCliConfig { fn default() -> Self { - let rpc_url = String::from("127.0.0.1:8000"); + let rpc_url = String::from("http://127.0.0.1:8000"); let log_path = String::from("/tmp/darkfi_cli.log"); Self { rpc_url,