mastodon-async/README.md

139 lines
4.1 KiB
Markdown
Raw Permalink 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-24 08:01:37 +00:00
A type-safe, async wrapper around the client [API](https://docs.joinmastodon.org/client/intro/)
2022-12-22 18:59:38 +00:00
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
~~~
### Use Rustls instead of OpenSSL
To use Rustls instead of OpenSSL for HTTPS request, define the dependency as follows
```toml
mastodon-async = { version = "1", default-features = false, features = ["rustls-tls"] }
```
2022-12-29 17:19:23 +00:00
## A Note on Debugging
This library offers structured logging. To get better information about bugs or
how something is working, I recommend adding the femme crate as a dependency,
then adding this line to the beginning of your main() function:
```rust,ignore
2022-12-29 17:19:23 +00:00
femme::with_level(log::LevelFilter::Trace);
```
When compiling for the debug target, this offers a mostly-human-readable output
with a lot of details about what's happening. When targeting release, JSON-
structured metadata is offered, which can be filtered and manipulated with
scripts or at the shell with jq.
There are other crates which make use of the log crate's new (unstable) kv
features, this is just the one that works for me for now.
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]
2022-12-23 17:14:45 +00:00
version = "1.0"
features = ["toml", "mt"]
2022-11-27 14:44:43 +00:00
```
The `"mt"` feature is for tokio multi-threaded. For single threaded, drop the
`"mt"` feature and replace `#[tokio::main]` with
`#[tokio::main(flavor = "current_thread")]`.
<!--
todo swap ignore with no_run just below here & the next example
test passes locally but not in CI
-->
```rust,ignore
2022-11-27 14:44:43 +00:00
// src/main.rs
2022-12-22 18:19:49 +00:00
use mastodon_async::prelude::*;
use mastodon_async::helpers::toml; // requires `features = ["toml"]`
2022-12-23 15:09:33 +00:00
use mastodon_async::{helpers::cli, Result};
2022-11-27 14:44:43 +00:00
#[tokio::main] // requires `features = ["mt"]
2022-12-23 15:09:33 +00:00
async fn main() -> Result<()> {
2022-11-27 14:44:43 +00:00
let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") {
Mastodon::from(data)
} else {
2022-12-23 15:09:33 +00:00
register().await?
2022-11-27 14:44:43 +00:00
};
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(())
}
2022-12-23 15:09:33 +00:00
async fn register() -> Result<Mastodon> {
let registration = Registration::new("https://botsin.space")
2022-12-22 18:19:49 +00:00
.client_name("mastodon-async-examples")
2022-12-23 15:09:33 +00:00
.build()
.await?;
let mastodon = cli::authenticate(registration).await?;
2022-11-27 14:44:43 +00:00
// Save app data for using on the next run.
2022-12-23 15:09:33 +00:00
toml::to_file(&mastodon.data, "mastodon-data.toml")?;
2022-11-27 14:44:43 +00:00
Ok(mastodon)
}
```
It also supports the [Streaming API](https://docs.joinmastodon.org/api/streaming):
> **Note**: this example compiles, but will not run. See the
> [log_events](https://github.com/dscottboggs/mastodon-async/blob/main/examples/log_events.rs)
> example for a more thorough example which does compile and run.
```rust,ignore
2022-12-23 15:09:33 +00:00
use mastodon_async::{prelude::*, Result, entities::event::Event};
use futures_util::TryStreamExt;
2022-11-27 14:44:43 +00:00
2022-12-22 17:27:30 +00:00
#[tokio::main]
2022-12-23 15:09:33 +00:00
async fn main() -> Result<()> {
2022-12-22 17:27:30 +00:00
let client = Mastodon::from(Data::default());
client.stream_user()
.await?
2022-12-23 15:09:33 +00:00
.try_for_each(|event| async move {
2022-12-22 17:27:30 +00:00
match event {
Event::Update(ref status) => { /* .. */ },
Event::Notification(ref notification) => { /* .. */ },
Event::Delete(ref id) => { /* .. */ },
Event::FiltersChanged => { /* .. */ },
}
2022-12-23 15:09:33 +00:00
Ok(())
2022-12-22 17:27:30 +00:00
})
.await?;
2022-11-27 14:44:43 +00:00
Ok(())
}
```