rpc.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. use darkfi::{
  2. error::Result,
  3. rpc::{client::RpcClient, jsonrpc::JsonRequest},
  4. };
  5. use serde_json::{json, Value};
  6. use url::Url;
  7. use crate::error::{DnetViewError, DnetViewResult};
  8. pub struct RpcConnect {
  9. pub name: String,
  10. pub rpc_client: RpcClient,
  11. }
  12. impl RpcConnect {
  13. pub async fn new(url: Url, name: String) -> Result<Self> {
  14. let rpc_client = RpcClient::new(url).await?;
  15. Ok(Self { name, rpc_client })
  16. }
  17. // --> {"jsonrpc": "2.0", "method": "ping", "params": [], "id": 42}
  18. // <-- {"jsonrpc": "2.0", "result": "pong", "id": 42}
  19. pub async fn ping(&self) -> Result<Value> {
  20. let req = JsonRequest::new("ping", json!([]));
  21. self.rpc_client.request(req).await
  22. }
  23. //--> {"jsonrpc": "2.0", "method": "poll", "params": [], "id": 42}
  24. // <-- {"jsonrpc": "2.0", "result": {"nodeID": [], "nodeinfo" [], "id": 42}
  25. pub async fn get_info(&self) -> DnetViewResult<Value> {
  26. let req = JsonRequest::new("get_info", json!([]));
  27. match self.rpc_client.request(req).await {
  28. Ok(req) => Ok(req),
  29. Err(e) => Err(DnetViewError::Darkfi(e)),
  30. }
  31. }
  32. }