reqwest
Before explaining seanmonstar/reqwest in RUST, try the following command.
$ curl -sL "https://bsky.social/xrpc/com.atproto.repo.listRecords?repo=support.bsky.team&collection=app.bsky.feed.post"
This hits the api to get the timeline for support.bsky.social.
You can understand that reqwest
is mainly a rust lib to hit the api.
Now, let's write the actual code.
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();
}
This is then cargo build
and run the command as usual.
$ ./target/debug/rust b
The following is an example
, i.e., code with useless command options removed.
The main points of the code are as follows.
.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
Try adding query
. Now the output will be on one line and in order of oldest to newest.
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(())
}