mastodon-async/README.md

101 lines
2.6 KiB
Markdown
Raw Normal View History

2022-12-22 18:19:49 +00:00
# Async Mastodon client library
2022-11-27 14:44:43 +00:00
2022-12-22 18:19:49 +00:00
[![Build Status](https://github.com/dscottboggs/mastodon-async/actions/workflows/rust.yml/badge.svg)]
[![crates.io](https://img.shields.io/crates/v/mastodon-async.svg)](https://crates.io/crates/mastodon-async)
[![Docs](https://docs.rs/mastodon-async/badge.svg)](https://docs.rs/mastodon-async)
[![MIT/APACHE-2.0](https://img.shields.io/crates/l/mastodon-async.svg)](https://crates.io/crates/mastodon-async)
2022-11-27 14:44:43 +00:00
2022-12-22 18:19:49 +00:00
[Documentation](https://docs.rs/mastodon-async/)
2022-11-27 14:44:43 +00:00
2022-12-22 18:59:38 +00:00
A type-safe, async wrapper around the client [API](https://github.com/tootsuite/documentation/blob/master/docs/Using-the-API/API.md#tag)
for [Mastodon](https://botsin.space/)
2022-11-27 14:44:43 +00:00
## Installation
2022-12-22 18:19:49 +00:00
To add `mastodon-async` to your project, add the following to the
2022-11-27 14:44:43 +00:00
`[dependencies]` section of your `Cargo.toml`
```toml
2022-12-22 18:19:49 +00:00
mastodon-async = "1.0"
2022-11-27 14:44:43 +00:00
```
2022-12-05 15:35:45 +00:00
Alternatively, run the following command:
~~~console
2022-12-22 18:19:49 +00:00
$ cargo add mastodon-async
2022-12-05 15:35:45 +00:00
~~~
2022-11-27 14:44:43 +00:00
## Example
In your `Cargo.toml`, make sure you enable the `toml` feature:
```toml
2022-12-22 18:59:38 +00:00
[dependencies.mastodon-async]
version = "0.22"
features = ["toml"]
2022-11-27 14:44:43 +00:00
```
```rust,no_run
// src/main.rs
use std::error::Error;
2022-12-22 18:19:49 +00:00
use mastodon_async::prelude::*;
use mastodon_async::helpers::toml; // requires `features = ["toml"]`
use mastodon_async::helpers::cli;
2022-11-27 14:44:43 +00:00
2022-12-22 17:27:30 +00:00
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
2022-11-27 14:44:43 +00:00
let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") {
Mastodon::from(data)
} else {
register()?
};
2022-12-22 17:27:30 +00:00
let you = mastodon.verify_credentials().await?;
2022-11-27 14:44:43 +00:00
println!("{:#?}", you);
Ok(())
}
fn register() -> Result<Mastodon, Box<dyn Error>> {
let registration = Registration::new("https://botsin.space")
2022-12-22 18:19:49 +00:00
.client_name("mastodon-async-examples")
2022-11-27 14:44:43 +00:00
.build()?;
let mastodon = cli::authenticate(registration)?;
// Save app data for using on the next run.
toml::to_file(&*mastodon, "mastodon-data.toml")?;
Ok(mastodon)
}
```
It also supports the [Streaming API](https://docs.joinmastodon.org/api/streaming):
```rust,no_run
2022-12-22 18:19:49 +00:00
use mastodon_async::prelude::*;
use mastodon_async::entities::event::Event;
2022-11-27 14:44:43 +00:00
use std::error::Error;
2022-12-22 17:27:30 +00:00
#[tokio::main]
async fn main() -> Result<(), Box<Error>> {
let client = Mastodon::from(Data::default());
client.stream_user()
.await?
.try_for_each(|event| {
match event {
Event::Update(ref status) => { /* .. */ },
Event::Notification(ref notification) => { /* .. */ },
Event::Delete(ref id) => { /* .. */ },
Event::FiltersChanged => { /* .. */ },
}
})
.await?;
2022-11-27 14:44:43 +00:00
Ok(())
}
```