reqwest
rustのseanmonstar/reqwestを解説する前に、以下のコマンドを実行してみてください。
$ curl -sL "https://bsky.social/xrpc/com.atproto.repo.listRecords?repo=support.bsky.team&collection=app.bsky.feed.post"
これはsupport.bsky.socialのtimelineを取得するapiを叩いています。
reqwestは主にapiを叩くためのrustのlibだという理解で構いません。
では、実際にコードを書いてみます。
Cargo.toml
[package]
name = "rust"
version = "0.1.0"
edition = "2021"
[dependencies]
seahorse = "*"
reqwest = "*"
tokio = { version = "1", features = ["full"] }
use seahorse::{App, Context, Command};
use std::env;
fn main() {
    let args: Vec<String> = env::args().collect();
    let app = App::new(env!("CARGO_PKG_NAME"))
        .action(s)
        .command(
                Command::new("yes")
                .alias("y")
                .action(y),
                )
        .command(
                Command::new("no")
                .alias("n")
                .action(n),
                )
        .command(
                Command::new("test")
                .alias("t")
                .action(|c| println!("Hello, {:?}", c.args)),
                )
        .command(
                Command::new("bluesky")
                .alias("b")
                .action(c_list_records),
                )
        ;
        app.run(args);
}
fn s(_c: &Context) {
    println!("Hello, world!");
}
fn y(_c: &Context) {
    println!("yes");
}
fn n(_c: &Context) {
    println!("no");
}
#[tokio::main]
async fn list_records() -> reqwest::Result<()> {
    let client = reqwest::Client::new();
    let handle= "support.bsky.team";
    let col = "app.bsky.feed.post";
    let body = client.get("https://bsky.social/xrpc/com.atproto.repo.listRecords")
        .query(&[("repo", &handle),("collection", &col)])
        .send()
        .await?
        .text()
        .await?;
    println!("{}", body);
    Ok(())
}
fn c_list_records(_c: &Context) {
    list_records().unwrap();
}
これをcargo buildしていつものようにコマンドを実行します。
$ ./target/debug/rust b
以降はexample、つまり無駄なコマンドオプションを削除したコードを記述します。
コードの要点は以下の通り。
.command(
        Command::new("bluesky")
        .alias("b")
        .action(c_list_records),
        )
#[tokio::main]
async fn list_records() -> reqwest::Result<()> {
    let client = reqwest::Client::new();
    let handle= "support.bsky.team";
    let col = "app.bsky.feed.post";
    let body = client.get("https://bsky.social/xrpc/com.atproto.repo.listRecords")
        .query(&[("repo", &handle),("collection", &col)])
        .send()
        .await?
        .text()
        .await?;
    println!("{}", body);
    Ok(())
}
fn c_list_records(_c: &Context) {
    list_records().unwrap();
}
query
queryの追加をしてみます。これで出力が1行になり、古い順になります。
src/main.rs
async fn list_records() -> reqwest::Result<()> {
    let client = reqwest::Client::new();
    let handle= "support.bsky.team";
    let col = "app.bsky.feed.post";
    let body = client.get("https://bsky.social/xrpc/com.atproto.repo.listRecords")
        //.query(&[("repo", &handle),("collection", &col)])
        .query(&[("repo", &handle),("collection", &col),("limit", &"1"),("revert", &"true")])
        .send()
        .await?
        .text()
        .await?;
    println!("{}", body);
    Ok(())
}