summaryrefslogtreecommitdiff
path: root/src/api/guzzle.rs
blob: 6d1e41b7294deefb32ea9b39bd54b65d33d8caf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::api::REQWEST_CLIENT;
use crate::Error;

use reqwest::StatusCode;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct GuzzleResponse {
	pub url: String,
}

const GUZZLE: &str = "https://api.mydadleft.me";

pub async fn get_random_teawie() -> Result<String, Error> {
	let endpoint = "/get_random_teawie";

	let req = REQWEST_CLIENT
		.get(format!("{GUZZLE}{endpoint}"))
		.build()
		.unwrap();

	let resp = REQWEST_CLIENT.execute(req).await.unwrap();

	if let StatusCode::OK = resp.status() {
		match resp.json::<GuzzleResponse>().await {
			Ok(data) => Ok(data.url),
			Err(why) => Err(Box::new(why)),
		}
	} else {
		Err(resp.status().to_string().into())
	}
}