api: use error_for_status_ref()
This commit is contained in:
parent
3c4cf67bbf
commit
e847ea0ad0
4 changed files with 47 additions and 82 deletions
|
@ -1,24 +1,20 @@
|
|||
use crate::api::REQWEST_CLIENT;
|
||||
|
||||
use eyre::{eyre, Result};
|
||||
use eyre::Result;
|
||||
use log::debug;
|
||||
use reqwest::StatusCode;
|
||||
|
||||
const DADJOKE: &str = "https://icanhazdadjoke.com";
|
||||
|
||||
pub async fn get_joke() -> Result<String> {
|
||||
let req = REQWEST_CLIENT
|
||||
debug!("Making request to {DADJOKE}");
|
||||
|
||||
let resp = REQWEST_CLIENT
|
||||
.get(DADJOKE)
|
||||
.header("Accept", "text/plain")
|
||||
.build()?;
|
||||
.send()
|
||||
.await?;
|
||||
resp.error_for_status_ref()?;
|
||||
|
||||
debug!("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!("Couldn't get a joke!"))
|
||||
}
|
||||
let joke = resp.text().await?;
|
||||
Ok(joke)
|
||||
}
|
||||
|
|
|
@ -1,39 +1,31 @@
|
|||
use crate::api::REQWEST_CLIENT;
|
||||
|
||||
use eyre::{eyre, Context, Result};
|
||||
use eyre::{Context, Result};
|
||||
use log::debug;
|
||||
use poise::serenity_prelude::{MessageId, UserId};
|
||||
use reqwest::StatusCode;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct PluralKitMessage {
|
||||
pub struct Message {
|
||||
pub sender: String,
|
||||
}
|
||||
|
||||
const PLURAL_KIT: &str = "https://api.pluralkit.me/v2";
|
||||
const MESSAGES_ENDPOINT: &str = "/messages";
|
||||
const MESSAGES: &str = "/messages";
|
||||
|
||||
pub async fn get_sender(message_id: MessageId) -> Result<UserId> {
|
||||
let req = REQWEST_CLIENT
|
||||
.get(format!("{PLURAL_KIT}{MESSAGES_ENDPOINT}/{message_id}"))
|
||||
.build()?;
|
||||
let url = format!("{PLURAL_KIT}{MESSAGES}/{message_id}");
|
||||
|
||||
debug!("Making request to {}", req.url());
|
||||
let resp = REQWEST_CLIENT.execute(req).await?;
|
||||
let status = resp.status();
|
||||
debug!("Making request to {url}");
|
||||
let resp = REQWEST_CLIENT.get(url).send().await?;
|
||||
resp.error_for_status_ref()?;
|
||||
|
||||
if let StatusCode::OK = status {
|
||||
let data = resp.json::<PluralKitMessage>().await?;
|
||||
let id: u64 = data.sender.parse().wrap_err_with(|| {
|
||||
let data: Message = resp.json().await?;
|
||||
let id: u64 =
|
||||
data.sender.parse().wrap_err_with(|| {
|
||||
format!("Couldn't parse response from PluralKit as a UserId! Here's the response:\n{data:#?}")
|
||||
})?;
|
||||
let sender = UserId::from(id);
|
||||
let sender = UserId::from(id);
|
||||
|
||||
Ok(sender)
|
||||
} else {
|
||||
Err(eyre!(
|
||||
"Failed to get PluralKit message information from {PLURAL_KIT}{MESSAGES_ENDPOINT}/{message_id} with {status}",
|
||||
))
|
||||
}
|
||||
Ok(sender)
|
||||
}
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
use crate::api::REQWEST_CLIENT;
|
||||
|
||||
use eyre::{eyre, Context, OptionExt, Result};
|
||||
use eyre::{OptionExt, Result};
|
||||
use log::debug;
|
||||
use reqwest::StatusCode;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
@ -14,33 +13,22 @@ pub struct MinecraftPackageJson {
|
|||
pub uid: String,
|
||||
}
|
||||
|
||||
const PRISM_META: &str = "https://meta.prismlauncher.org/v1";
|
||||
const MINECRAFT_PACKAGEJSON_ENDPOINT: &str = "/net.minecraft/package.json";
|
||||
const META: &str = "https://meta.prismlauncher.org/v1";
|
||||
const MINECRAFT_PACKAGEJSON: &str = "/net.minecraft/package.json";
|
||||
|
||||
pub async fn get_latest_minecraft_version() -> Result<String> {
|
||||
let req = REQWEST_CLIENT
|
||||
.get(format!("{PRISM_META}{MINECRAFT_PACKAGEJSON_ENDPOINT}"))
|
||||
.build()?;
|
||||
let url = format!("{META}{MINECRAFT_PACKAGEJSON}");
|
||||
|
||||
debug!("Making request to {}", req.url());
|
||||
let resp = REQWEST_CLIENT.execute(req).await?;
|
||||
let status = resp.status();
|
||||
debug!("Making request to {url}");
|
||||
let resp = REQWEST_CLIENT.get(url).send().await?;
|
||||
resp.error_for_status_ref()?;
|
||||
|
||||
if let StatusCode::OK = status {
|
||||
let data = resp
|
||||
.json::<MinecraftPackageJson>()
|
||||
.await
|
||||
.wrap_err("Couldn't parse Minecraft versions!")?;
|
||||
let data: MinecraftPackageJson = resp.json().await?;
|
||||
|
||||
let version = data
|
||||
.recommended
|
||||
.first()
|
||||
.ok_or_eyre("Couldn't find latest version of Minecraft!")?;
|
||||
let version = data
|
||||
.recommended
|
||||
.first()
|
||||
.ok_or_eyre("Couldn't find latest version of Minecraft!")?;
|
||||
|
||||
Ok(version.clone())
|
||||
} else {
|
||||
Err(eyre!(
|
||||
"Failed to get latest Minecraft version from {PRISM_META}{MINECRAFT_PACKAGEJSON_ENDPOINT} with {status}",
|
||||
))
|
||||
}
|
||||
Ok(version.clone())
|
||||
}
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
use crate::api::REQWEST_CLIENT;
|
||||
|
||||
use eyre::{eyre, Context, Result};
|
||||
use eyre::{Context, Result};
|
||||
use log::debug;
|
||||
use reqwest::StatusCode;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
@ -13,34 +12,24 @@ pub struct Response {
|
|||
}
|
||||
|
||||
const RORY: &str = "https://rory.cat";
|
||||
const ENDPOINT: &str = "/purr";
|
||||
const PURR: &str = "/purr";
|
||||
|
||||
pub async fn get(id: Option<u64>) -> Result<Response> {
|
||||
let target = id.map(|id| id.to_string()).unwrap_or_default();
|
||||
let url = format!("{RORY}{PURR}/{target}");
|
||||
|
||||
let req = REQWEST_CLIENT
|
||||
.get(format!("{RORY}{ENDPOINT}/{target}"))
|
||||
.build()
|
||||
.wrap_err("Couldn't build reqwest client!")?;
|
||||
debug!("Making request to {url}");
|
||||
|
||||
debug!("Making request to {}", req.url());
|
||||
let resp = REQWEST_CLIENT
|
||||
.execute(req)
|
||||
.get(format!("{RORY}{PURR}/{target}"))
|
||||
.send()
|
||||
.await?;
|
||||
resp.error_for_status_ref()?;
|
||||
|
||||
let data: Response = resp
|
||||
.json()
|
||||
.await
|
||||
.wrap_err("Couldn't make request for rory!")?;
|
||||
.wrap_err("Couldn't parse the rory response!")?;
|
||||
|
||||
let status = resp.status();
|
||||
|
||||
if let StatusCode::OK = status {
|
||||
let data = resp
|
||||
.json::<Response>()
|
||||
.await
|
||||
.wrap_err("Couldn't parse the rory response!")?;
|
||||
|
||||
Ok(data)
|
||||
} else {
|
||||
Err(eyre!(
|
||||
"Failed to get rory from {RORY}{ENDPOINT}/{target} with {status}",
|
||||
))
|
||||
}
|
||||
Ok(data)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue