farcaster_rs/casts/
publish_cast.rs1use crate::constants::merkle::API_ROOT;
2use crate::types::casts::published_cast::PublishedCast;
3use crate::Farcaster;
4
5use serde_json::{json, Value};
6
7impl Farcaster {
8 pub async fn publish_cast(
22 &self,
23 content: &str,
24 reply_to_hash: Option<&str>,
25 reply_to_fid: Option<i64>,
26 ) -> Result<PublishedCast, Box<dyn std::error::Error>> {
27 let payload: Value;
28
29 if reply_to_hash.is_some() && reply_to_fid.is_some() {
30 let parent_hash = reply_to_hash.unwrap();
31 let parent_fid = reply_to_fid.unwrap();
32
33 payload = json!({
34 "parent": {
35 "hash": parent_hash,
36 "fid": parent_fid
37 },
38 "text": content
39 })
40 } else {
41 payload = json!({ "text": content })
42 }
43
44 let publish_cast_reqwest = reqwest::Client::new()
45 .post(format!("{}/v2/casts", API_ROOT))
46 .header("Content-Type", "application/json")
47 .header(
48 "Authorization",
49 &self.account.session_token.as_ref().unwrap().secret,
50 )
51 .json(&payload)
52 .send()
53 .await?
54 .text()
55 .await?;
56
57 let published_cast: PublishedCast = serde_json::from_str(&publish_cast_reqwest)?;
58
59 Ok(published_cast)
60 }
61}