jensmemesclient/cli/src/util.rs

33 lines
869 B
Rust

use anyhow::Result;
use once_cell::sync::OnceCell;
use reqwest::Client;
use tokio::process::Command;
use crate::api::CatsResp;
static CATS: OnceCell<Vec<String>> = OnceCell::new();
pub async fn open_link(url: &str) -> anyhow::Result<()> {
match std::env::var_os("BROWSER") {
Some(browser) => {
Command::new(&browser).arg(url).status().await?;
},
None => opener::open(&url)?,
}
Ok(())
}
pub async fn cats(http: &Client) -> Result<&Vec<String>> {
if CATS.get().is_none() {
let res = http
.get("https://data.tilera.xyz/api/jensmemes/categories")
.send()
.await?;
let cats = serde_json::from_slice::<CatsResp>(&res.bytes().await?)?;
Ok(CATS.get_or_init(|| cats.categories.into_iter().map(|c| c.id).collect()))
} else {
unreachable!();
}
}