initial rewrite in rust & moderation commands
Signed-off-by: seth <getchoo@tuta.io>
This commit is contained in:
parent
b17e357b75
commit
45403e9d9b
53 changed files with 3297 additions and 2820 deletions
21
src/api/dadjoke.rs
Normal file
21
src/api/dadjoke.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use crate::api::REQWEST_CLIENT;
|
||||
|
||||
use color_eyre::eyre::{eyre, Result};
|
||||
use log::*;
|
||||
use reqwest::StatusCode;
|
||||
|
||||
const DADJOKE: &str = "https://icanhazdadjoke.com";
|
||||
|
||||
pub async fn get_joke() -> Result<String> {
|
||||
let req = REQWEST_CLIENT.get(DADJOKE).build()?;
|
||||
|
||||
info!("making request to {}", req.url());
|
||||
let resp = REQWEST_CLIENT.execute(req).await?;
|
||||
let status = resp.status();
|
||||
|
||||
if let StatusCode::OK = status {
|
||||
Ok(resp.text().await?)
|
||||
} else {
|
||||
Err(eyre!("Failed to fetch joke from {DADJOKE} with {status}"))
|
||||
}
|
||||
}
|
17
src/api/mod.rs
Normal file
17
src/api/mod.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use once_cell::sync::Lazy;
|
||||
|
||||
pub mod dadjoke;
|
||||
pub mod rory;
|
||||
|
||||
pub static USER_AGENT: Lazy<String> = Lazy::new(|| {
|
||||
let version = option_env!("CARGO_PKG_VERSION").unwrap_or("development");
|
||||
|
||||
format!("refraction/{version}")
|
||||
});
|
||||
|
||||
pub static REQWEST_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| {
|
||||
reqwest::Client::builder()
|
||||
.user_agent(USER_AGENT.to_string())
|
||||
.build()
|
||||
.unwrap_or_default()
|
||||
});
|
42
src/api/rory.rs
Normal file
42
src/api/rory.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use crate::api::REQWEST_CLIENT;
|
||||
|
||||
use color_eyre::eyre::{eyre, Result};
|
||||
use log::*;
|
||||
use reqwest::StatusCode;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct RoryResponse {
|
||||
pub id: u64,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
const RORY: &str = "https://rory.cat";
|
||||
const ENDPOINT: &str = "/purr";
|
||||
|
||||
pub async fn get_rory(id: Option<u64>) -> Result<RoryResponse> {
|
||||
let target = {
|
||||
if let Some(id) = id {
|
||||
id.to_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
};
|
||||
|
||||
let req = REQWEST_CLIENT
|
||||
.get(format!("{RORY}{ENDPOINT}/{target}"))
|
||||
.build()?;
|
||||
|
||||
info!("making request to {}", req.url());
|
||||
let resp = REQWEST_CLIENT.execute(req).await?;
|
||||
let status = resp.status();
|
||||
|
||||
if let StatusCode::OK = status {
|
||||
let data = resp.json::<RoryResponse>().await?;
|
||||
Ok(data)
|
||||
} else {
|
||||
Err(eyre!(
|
||||
"Failed to get rory from {RORY}{ENDPOINT}/{target} with {status}",
|
||||
))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue