From 1a5474c8b0b80e21858ac6892fa775509228fa2d Mon Sep 17 00:00:00 2001 From: Yoshihiro Sugi Date: Tue, 5 Mar 2024 23:08:08 +0900 Subject: [PATCH] chore: Update examples (#129) --- examples/concurrent/Cargo.toml | 10 +++++----- examples/concurrent/src/main.rs | 4 +++- examples/firehose/Cargo.toml | 2 +- examples/firehose/src/main.rs | 11 +++++++---- examples/firehose/src/subscription.rs | 6 ++---- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/examples/concurrent/Cargo.toml b/examples/concurrent/Cargo.toml index 09da4676..2f60b216 100644 --- a/examples/concurrent/Cargo.toml +++ b/examples/concurrent/Cargo.toml @@ -6,8 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -atrium-api = "0.14" -atrium-xrpc-client = "0.2" -clap = { version = "4.4.7", features = ["derive"] } -futures = "0.3.29" -tokio = { version = "1.33.0", features = ["macros", "rt-multi-thread"] } +atrium-api = "0.18.1" +atrium-xrpc-client = "0.4.0" +clap = { version = "4.5.1", features = ["derive"] } +futures = "0.3.30" +tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] } diff --git a/examples/concurrent/src/main.rs b/examples/concurrent/src/main.rs index 4d0c90cf..21e2e05a 100644 --- a/examples/concurrent/src/main.rs +++ b/examples/concurrent/src/main.rs @@ -28,6 +28,7 @@ async fn main() -> Result<(), Box> { let handles = actors .iter() .map(|&actor| { + println!("fetching profile of {actor}..."); let agent = Arc::clone(&agent); tokio::spawn(async move { agent @@ -36,13 +37,14 @@ async fn main() -> Result<(), Box> { .bsky .actor .get_profile(atrium_api::app::bsky::actor::get_profile::Parameters { - actor: actor.into(), + actor: actor.parse().expect("invalid actor"), }) .await }) }) .collect::>(); let results = join_all(handles).await; + println!("{} profiles fetched!", results.len()); for (actor, result) in actors.iter().zip(results) { println!("{actor}: {:#?}", result?); } diff --git a/examples/firehose/Cargo.toml b/examples/firehose/Cargo.toml index c5ab8146..9c05a46e 100644 --- a/examples/firehose/Cargo.toml +++ b/examples/firehose/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" anyhow = "1.0.80" atrium-api = { version = "0.18.1", features = ["dag-cbor"] } chrono = "0.4.34" -futures = "0.3.28" +futures = "0.3.30" ipld-core = { version = "0.2.0", features = ["serde"] } rs-car = "0.4.1" serde_ipld_dagcbor = { git = "https://github.com/ipld/serde_ipld_dagcbor.git", rev = "297a6c26c8c89807e6602cab9803ef2c4ae8b459" } diff --git a/examples/firehose/src/main.rs b/examples/firehose/src/main.rs index fd276b18..e70a237e 100644 --- a/examples/firehose/src/main.rs +++ b/examples/firehose/src/main.rs @@ -1,3 +1,4 @@ +use anyhow::{anyhow, Result}; use atrium_api::app::bsky::feed::post::Record; use atrium_api::com::atproto::sync::subscribe_repos::{Commit, NSID}; use atrium_api::types::{CidLink, Collection}; @@ -23,7 +24,9 @@ impl RepoSubscription { if let Ok(Frame::Message(Some(t), message)) = result { if t.as_str() == "#commit" { let commit = serde_ipld_dagcbor::from_reader(message.body.as_slice())?; - handler.handle_commit(&commit).await?; + if let Err(err) = handler.handle_commit(&commit).await { + eprintln!("FAILED: {err:?}"); + } } } } @@ -44,7 +47,7 @@ impl Subscription for RepoSubscription { struct Firehose; impl CommitHandler for Firehose { - async fn handle_commit(&self, commit: &Commit) -> Result<(), Box> { + async fn handle_commit(&self, commit: &Commit) -> Result<()> { for op in &commit.ops { let collection = op.path.split('/').next().expect("op.path is empty"); if op.action != "create" || collection != atrium_api::app::bsky::feed::Post::NSID { @@ -62,11 +65,11 @@ impl CommitHandler for Firehose { println!(" {line}"); } } else { - panic!( + return Err(anyhow!( "FAILED: could not find item with operation cid {:?} out of {} items", op.cid, items.len() - ); + )); } } Ok(()) diff --git a/examples/firehose/src/subscription.rs b/examples/firehose/src/subscription.rs index d0c8bcb5..90393105 100644 --- a/examples/firehose/src/subscription.rs +++ b/examples/firehose/src/subscription.rs @@ -1,4 +1,5 @@ use crate::stream::frames::Frame; +use anyhow::Result; use atrium_api::com::atproto::sync::subscribe_repos::Commit; use std::future::Future; @@ -8,8 +9,5 @@ pub trait Subscription { } pub trait CommitHandler { - fn handle_commit( - &self, - commit: &Commit, - ) -> impl Future>>; + fn handle_commit(&self, commit: &Commit) -> impl Future>; }