Skip to content

Commit

Permalink
add publish and example
Browse files Browse the repository at this point in the history
  • Loading branch information
mycognosist committed Feb 22, 2024
1 parent 5abd301 commit 0c5cee6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
32 changes: 32 additions & 0 deletions solar_client/examples/publish.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use anyhow::Result;
use serde_json::json;
use solar_client::{Client, SolarClient};

const SERVER_ADDR: &str = "http://127.0.0.1:3030";
const TEXT: &str = "Testing message publishing via the solar JSON-RPC client";

#[tokio::main]
async fn main() -> Result<()> {
let client = Client::new(SERVER_ADDR.to_owned())?;

// Construct a JSON post.
//
// This can take any shape conforming to
// `kuska_ssb::api::dto::content::TypedMessage`.
//
// Consult the `kuska_ssb` repo for more details:
// https://github.com/Kuska-ssb/ssb
let post = json!({
"type": "post",
"text": TEXT,
});

let msg_ref_and_seq_num = client.publish(post).await?;
println!("{:?}", msg_ref_and_seq_num);
// ("%J0k3hbXE6CjoA2yFpUsy+A1K+GRcTc5Eh6LH7OuzzUE=.sha256", 231)

// Or destructure the tuple into individual components:
// let (msg_ref, seq_num) = client.publish(post).await?;

Ok(())
}
3 changes: 2 additions & 1 deletion solar_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
// TODO: Should we rather be using a custom kuska type?
use serde_json::Value;

#[jsonrpc_client::api]
Expand Down Expand Up @@ -48,6 +47,8 @@ pub trait SolarClient {

async fn ping(&self) -> String;

async fn publish(&self, msg: Value) -> (String, u64);

async fn subscribers(&self, channel: &str) -> Vec<String>;

async fn subscriptions(&self, pub_key: &str) -> Vec<String>;
Expand Down

0 comments on commit 0c5cee6

Please sign in to comment.