From b643a6a235b0c1c9902b97421f24eff2b0d0a5ac Mon Sep 17 00:00:00 2001 From: seth Date: Fri, 9 Aug 2024 23:35:41 -0400 Subject: tree-wide: end of summer cleanup (#214) * api: refactor & rename module to http * client: split from main.rs * tree-wide: use eyre::Report as error * nix: alejandra -> nixfmt * nix: start using treefmt-nix * nix: simplify flake * nix: refactor derivation & docker image * nix: remove overlay * ci: update & cleanup workflows * commands: assign all commands automatically * commands/copypasta: remove * http/teawie: update response struct for upstream rust rewrite * handlers: rename modules to events; flatten * crates: rename self to teawie-bot * nix: fenix -> rust-overlay i want a specific rust version grrrrrrr * ci: pin rust to 1.79 this is what our nix dev shell uses and what we can compile on. it seems the time crate doesn't like v1.80 of the compiler :( * ci: always run release gates * nix: fix static toolchain * nix: rust-overlay -> nixpkgs * ci: adopt actions-rust-lang actions * nix: use docker arch names for containers * crates/time: 0.3.30 -> 0.3.36 fixes building on rust 1.80.0 --- src/http/mod.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/http/shiggy.rs | 20 ++++++++++++++++++++ src/http/teawie.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 src/http/mod.rs create mode 100644 src/http/shiggy.rs create mode 100644 src/http/teawie.rs (limited to 'src/http') diff --git a/src/http/mod.rs b/src/http/mod.rs new file mode 100644 index 0000000..0f16852 --- /dev/null +++ b/src/http/mod.rs @@ -0,0 +1,43 @@ +use eyre::Result; +use log::trace; +use serde::de::DeserializeOwned; + +pub mod shiggy; +pub mod teawie; + +pub type Client = reqwest::Client; +pub type Response = reqwest::Response; + +/// Primary extensions for HTTP Client +pub trait Ext { + async fn get_request(&self, url: &str) -> Result; + async fn get_json(&self, url: &str) -> Result; + fn default() -> Self; +} + +impl Ext for Client { + fn default() -> Self { + reqwest::ClientBuilder::new() + .user_agent(format!( + "teawie-bot/{}", + option_env!("CARGO_PKG_VERSION").unwrap_or("development") + )) + .build() + .unwrap() + } + + async fn get_request(&self, url: &str) -> Result { + trace!("Making request to {url}"); + let resp = self.get(url).send().await?; + resp.error_for_status_ref()?; + + Ok(resp) + } + + async fn get_json(&self, url: &str) -> Result { + let resp = self.get_request(url).await?; + let json = resp.json().await?; + + Ok(json) + } +} diff --git a/src/http/shiggy.rs b/src/http/shiggy.rs new file mode 100644 index 0000000..397d397 --- /dev/null +++ b/src/http/shiggy.rs @@ -0,0 +1,20 @@ +use eyre::Result; +use serde::Deserialize; + +const SHIGGY: &str = "https://safebooru.donmai.us"; +const RANDOM: &str = "/posts/random.json?tags=kemomimi-chan_(naga_u)+naga_u&only=file_url"; + +#[derive(Deserialize)] +struct SafebooruResponse { + file_url: String, +} + +pub async fn random(http: &T) -> Result +where + T: super::Ext, +{ + let url = format!("{SHIGGY}{RANDOM}"); + let resp: SafebooruResponse = http.get_json(&url).await?; + + Ok(resp.file_url) +} diff --git a/src/http/teawie.rs b/src/http/teawie.rs new file mode 100644 index 0000000..368fad5 --- /dev/null +++ b/src/http/teawie.rs @@ -0,0 +1,28 @@ +use eyre::{bail, OptionExt, Result}; +use serde::{Deserialize, Serialize}; + +// https://github.com/getchoo/teawieAPI +#[derive(Deserialize, Serialize)] +struct RandomTeawieResponse { + url: Option, + error: Option, +} + +// TODO: read this from an env var +const TEAWIE: &str = "https://api.getchoo.com"; +const RANDOM: &str = "/random_teawie"; + +pub async fn random(http: &T) -> Result +where + T: super::Ext, +{ + let url = format!("{TEAWIE}{RANDOM}"); + let json: RandomTeawieResponse = http.get_json(&url).await?; + + if let Some(error) = json.error { + bail!("TeawieAPI reported error: {error}"); + }; + + json.url + .ok_or_eyre("TeawieAPI didn't return an error or URL???") +} -- cgit v1.2.3