From f204ee190bdf334aaedde95fc3e3466b36ef35ca Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Wed, 6 Mar 2019 12:28:47 -0500 Subject: [PATCH] Add some docs for streaming --- README.md | 31 +++++++++++++++++++++++++++++ src/lib.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/README.md b/README.md index b8486b2..e979e4a 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,37 @@ fn register() -> Result> { } ``` +It also supports the [Streaming API](https://docs.joinmastodon.org/api/streaming): + +```no_run +use elefren::prelude::*; +use elefren::entities::event::Event; + +use std::error::Error; + +fn main() -> Result<(), Box> { + let data = Data { + base: "".into(), + client_id: "".into(), + client_secret: "".into(), + redirect: "".into(), + token: "".into(), + }; + + let client = Mastodon::from(data); + + for event in client.streaming_user()? { + match event { + Event::Update(ref status) => { /* .. */ }, + Event::Notification(ref notification) => { /* .. */ }, + Event::Delete(ref id) => { /* .. */ }, + Event::FiltersChanged => { /* .. */ }, + } + } + Ok(()) +} +``` + ## Relationship to [Mammut](https://github.com/Aaronepower/mammut) This library was forked from Mammut around elefren commit diff --git a/src/lib.rs b/src/lib.rs index ebfc25b..98f3561 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,36 @@ //! # Ok(()) //! # } //! ``` +//! +//! Elefren also supports Mastodon's Streaming API: +//! +//! # Example +//! +//! ```no_run +//! # extern crate elefren; +//! # use elefren::prelude::*; +//! # use std::error::Error; +//! use elefren::entities::event::Event; +//! # fn main() -> Result<(), Box> { +//! # let data = Data { +//! # base: "".into(), +//! # client_id: "".into(), +//! # client_secret: "".into(), +//! # redirect: "".into(), +//! # token: "".into(), +//! # }; +//! let client = Mastodon::from(data); +//! for event in client.streaming_user()? { +//! match event { +//! Event::Update(ref status) => { /* .. */ }, +//! Event::Notification(ref notification) => { /* .. */ }, +//! Event::Delete(ref id) => { /* .. */ }, +//! Event::FiltersChanged => { /* .. */ }, +//! } +//! } +//! # Ok(()) +//! # } +//! ``` #![deny( missing_docs, @@ -428,6 +458,34 @@ impl MastodonClient for Mastodon { /// returns events that are relevant to the authorized user, i.e. home /// timeline & notifications + /// + /// # Example + /// + /// ```no_run + /// # extern crate elefren; + /// # use elefren::prelude::*; + /// # use std::error::Error; + /// use elefren::entities::event::Event; + /// # fn main() -> Result<(), Box> { + /// # let data = Data { + /// # base: "".into(), + /// # client_id: "".into(), + /// # client_secret: "".into(), + /// # redirect: "".into(), + /// # token: "".into(), + /// # }; + /// let client = Mastodon::from(data); + /// for event in client.streaming_user()? { + /// match event { + /// Event::Update(ref status) => { /* .. */ }, + /// Event::Notification(ref notification) => { /* .. */ }, + /// Event::Delete(ref id) => { /* .. */ }, + /// Event::FiltersChanged => { /* .. */ }, + /// } + /// } + /// # Ok(()) + /// # } + /// ``` fn streaming_user(&self) -> Result { let response = self.send(self.client.get(&self.route("/api/v1/streaming/user")))?; let reader = BufReader::new(response);