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"
|
||||
skeptic = "0.13"
|
||||
tempfile = "3"
|
||||
femme = "2.2.1"
|
||||
|
||||
[build-dependencies.skeptic]
|
||||
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::*;
|
||||
|
||||
use std::{error::Error, io};
|
||||
use std::io;
|
||||
|
||||
use elefren::helpers::cli;
|
||||
#[cfg(feature = "toml")]
|
||||
use elefren::helpers::toml;
|
||||
use elefren::{helpers::cli, Result};
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(feature = "toml")]
|
||||
fn main() -> Result<(), Box<Error>> {
|
||||
register()?;
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
register().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[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") {
|
||||
Ok(Mastodon::from(data))
|
||||
} else {
|
||||
register()
|
||||
register().await
|
||||
}
|
||||
}
|
||||
|
||||
#[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 registration = Registration::new(website.trim())
|
||||
.client_name("elefren-examples")
|
||||
.scopes(Scopes::all())
|
||||
.website("https://github.com/pwoolcoc/elefren")
|
||||
.build()?;
|
||||
let mastodon = cli::authenticate(registration)?;
|
||||
.website("https://github.com/dscottboggs/mastodon-async")
|
||||
.build()
|
||||
.await?;
|
||||
let mastodon = cli::authenticate(registration).await?;
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
#[cfg(feature = "toml")]
|
||||
pub fn read_line(message: &str) -> Result<String, Box<Error>> {
|
||||
pub fn read_line(message: &str) -> Result<String> {
|
||||
println!("{}", message);
|
||||
|
||||
let mut input = String::new();
|
||||
|
|
Loading…
Reference in New Issue