summaryrefslogtreecommitdiff
path: root/src/api/shiggy.rs
diff options
context:
space:
mode:
authoruku <[email protected]>2023-10-13 18:31:21 +0200
committerseth <[email protected]>2023-10-13 13:05:37 -0400
commitdec178a883769e221c8da3c125f83c6512b733e3 (patch)
tree2c83398eea4375a12ba3e6583ec43ae3a6bf3070 /src/api/shiggy.rs
parent05e0831f7926dd4afda8e72c6ea5aa7a466b6bea (diff)
move client & logic into api module
Diffstat (limited to 'src/api/shiggy.rs')
-rw-r--r--src/api/shiggy.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/api/shiggy.rs b/src/api/shiggy.rs
new file mode 100644
index 0000000..0e9fd19
--- /dev/null
+++ b/src/api/shiggy.rs
@@ -0,0 +1,29 @@
+use crate::api::REQWEST_CLIENT;
+use reqwest::StatusCode;
+use serde::Deserialize;
+
+const URL: &str = "https://safebooru.donmai.us/posts/random.json?tags=kemomimi-chan_(naga_u)+naga_u&only=file_url";
+const ERROR_MSG: &str = "couldn't get a shiggy";
+
+#[derive(Deserialize)]
+struct SafebooruResponse {
+ file_url: String,
+}
+
+pub async fn get_random_shiggy() -> String {
+ let resp = match REQWEST_CLIENT
+ .execute(REQWEST_CLIENT.get(URL).build().unwrap())
+ .await
+ {
+ Ok(r) => r,
+ Err(e) => return format!("{} ({:?})", ERROR_MSG, e),
+ };
+
+ match resp.status() {
+ StatusCode::OK => match resp.json::<SafebooruResponse>().await {
+ Ok(sr) => sr.file_url,
+ Err(e) => format!("{} ({:?})", ERROR_MSG, e),
+ },
+ other => format!("{} ({:?})", ERROR_MSG, other),
+ }
+}