create Drk struct and add say_hello function

This commit is contained in:
ghassmo
2021-07-06 08:33:48 +03:00
parent 50316d7ef5
commit 147d55a5d9
2 changed files with 45 additions and 3 deletions

View File

@@ -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<Executor<'_>>, _config: Arc<DarkfiCliConfig>) -> Result<()> {
type Payload = HashMap<String, String>;
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::<Payload>().await?;
info!("Response Result: {:?}", response);
}
Ok(())
}
}
async fn start(_executor: Arc<Executor<'_>>, config: Arc<DarkfiCliConfig>) -> 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
};

View File

@@ -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,