Add event-logging example
This commit is contained in:
parent
6fb63800d7
commit
e5fe0b6b59
|
@ -73,6 +73,7 @@ indoc = "1.0"
|
||||||
pretty_env_logger = "0.3.0"
|
pretty_env_logger = "0.3.0"
|
||||||
skeptic = "0.13"
|
skeptic = "0.13"
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
|
femme = "2.2.1"
|
||||||
|
|
||||||
[build-dependencies.skeptic]
|
[build-dependencies.skeptic]
|
||||||
version = "0.13"
|
version = "0.13"
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
#![cfg_attr(not(feature = "toml"), allow(dead_code))]
|
||||||
|
#![cfg_attr(not(feature = "toml"), allow(unused_imports))]
|
||||||
|
mod register;
|
||||||
|
|
||||||
|
use elefren::Result;
|
||||||
|
use futures_util::TryStreamExt;
|
||||||
|
use log::{as_serde, info};
|
||||||
|
|
||||||
|
#[cfg(feature = "toml")]
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
use elefren::entities::prelude::Event;
|
||||||
|
use log::warn;
|
||||||
|
|
||||||
|
femme::with_level(log::LevelFilter::Info);
|
||||||
|
let mastodon = register::get_mastodon_data().await?;
|
||||||
|
let stream = mastodon.stream_user().await?;
|
||||||
|
info!("watching mastodon for events. This will run forever, press Ctrl+C to kill the program.");
|
||||||
|
stream
|
||||||
|
.try_for_each(|event| async move {
|
||||||
|
match event {
|
||||||
|
// fill in how you want to handle events here.
|
||||||
|
_ => warn!(event = as_serde!(event); "unrecognized event received"),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "toml"))]
|
||||||
|
fn main() {
|
||||||
|
println!(
|
||||||
|
"examples require the `toml` feature, run this command for this example:\n\ncargo run \
|
||||||
|
--example log_events --features toml\n"
|
||||||
|
);
|
||||||
|
}
|
|
@ -3,47 +3,49 @@
|
||||||
|
|
||||||
pub use elefren::prelude::*;
|
pub use elefren::prelude::*;
|
||||||
|
|
||||||
use std::{error::Error, io};
|
use std::io;
|
||||||
|
|
||||||
use elefren::helpers::cli;
|
|
||||||
#[cfg(feature = "toml")]
|
#[cfg(feature = "toml")]
|
||||||
use elefren::helpers::toml;
|
use elefren::helpers::toml;
|
||||||
|
use elefren::{helpers::cli, Result};
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[cfg(feature = "toml")]
|
#[cfg(feature = "toml")]
|
||||||
fn main() -> Result<(), Box<Error>> {
|
#[tokio::main]
|
||||||
register()?;
|
async fn main() -> Result<()> {
|
||||||
|
register().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[cfg(feature = "toml")]
|
#[cfg(feature = "toml")]
|
||||||
pub fn get_mastodon_data() -> Result<Mastodon, Box<Error>> {
|
pub async fn get_mastodon_data() -> Result<Mastodon> {
|
||||||
if let Ok(data) = toml::from_file("mastodon-data.toml") {
|
if let Ok(data) = toml::from_file("mastodon-data.toml") {
|
||||||
Ok(Mastodon::from(data))
|
Ok(Mastodon::from(data))
|
||||||
} else {
|
} else {
|
||||||
register()
|
register().await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "toml")]
|
#[cfg(feature = "toml")]
|
||||||
pub fn register() -> Result<Mastodon, Box<Error>> {
|
pub async fn register() -> Result<Mastodon> {
|
||||||
let website = read_line("Please enter your mastodon instance url:")?;
|
let website = read_line("Please enter your mastodon instance url:")?;
|
||||||
let registration = Registration::new(website.trim())
|
let registration = Registration::new(website.trim())
|
||||||
.client_name("elefren-examples")
|
.client_name("elefren-examples")
|
||||||
.scopes(Scopes::all())
|
.scopes(Scopes::all())
|
||||||
.website("https://github.com/pwoolcoc/elefren")
|
.website("https://github.com/dscottboggs/mastodon-async")
|
||||||
.build()?;
|
.build()
|
||||||
let mastodon = cli::authenticate(registration)?;
|
.await?;
|
||||||
|
let mastodon = cli::authenticate(registration).await?;
|
||||||
|
|
||||||
// Save app data for using on the next run.
|
// Save app data for using on the next run.
|
||||||
toml::to_file(&*mastodon, "mastodon-data.toml")?;
|
toml::to_file(&mastodon.data, "mastodon-data.toml")?;
|
||||||
|
|
||||||
Ok(mastodon)
|
Ok(mastodon)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "toml")]
|
#[cfg(feature = "toml")]
|
||||||
pub fn read_line(message: &str) -> Result<String, Box<Error>> {
|
pub fn read_line(message: &str) -> Result<String> {
|
||||||
println!("{}", message);
|
println!("{}", message);
|
||||||
|
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
|
|
Loading…
Reference in New Issue