Improve error handling
This commit is contained in:
parent
8b3427867d
commit
d96fcf79dd
18
src/lib.rs
18
src/lib.rs
@ -13,7 +13,9 @@ use failure::Error;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub fn player(name: &str) -> Result<Player, Error> {
|
pub fn player(name: &str) -> Result<Player, Error> {
|
||||||
let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=playerStats&command={}", name))?.text().unwrap();
|
let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=playerStats&command={}", name))?
|
||||||
|
.error_for_status()?
|
||||||
|
.text().unwrap();
|
||||||
|
|
||||||
if let Ok(error) = serde_json::from_str::<APIError>(&resp) {
|
if let Ok(error) = serde_json::from_str::<APIError>(&resp) {
|
||||||
bail!("API error ({})", error.error);
|
bail!("API error ({})", error.error);
|
||||||
@ -23,7 +25,9 @@ pub fn player(name: &str) -> Result<Player, Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn guild(name: &str) -> Result<Option<Guild>, Error> {
|
pub fn guild(name: &str) -> Result<Option<Guild>, Error> {
|
||||||
let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=guildStats&command={}", name))?.text().unwrap();
|
let resp = reqwest::get(&format!("https://api.wynncraft.com/public_api.php?action=guildStats&command={}", name))?
|
||||||
|
.error_for_status()?
|
||||||
|
.text().unwrap();
|
||||||
|
|
||||||
if let Ok(error) = serde_json::from_str::<APIError>(&resp) {
|
if let Ok(error) = serde_json::from_str::<APIError>(&resp) {
|
||||||
bail!("API error ({})", error.error);
|
bail!("API error ({})", error.error);
|
||||||
@ -33,7 +37,9 @@ pub fn guild(name: &str) -> Result<Option<Guild>, Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn guild_list() -> Result<Vec<String>, Error> {
|
pub fn guild_list() -> Result<Vec<String>, Error> {
|
||||||
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=guildList")?.text().unwrap();
|
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=guildList")?
|
||||||
|
.error_for_status()?
|
||||||
|
.text().unwrap();
|
||||||
|
|
||||||
if let Ok(error) = serde_json::from_str::<APIError>(&resp) {
|
if let Ok(error) = serde_json::from_str::<APIError>(&resp) {
|
||||||
bail!("API error ({})", error.error);
|
bail!("API error ({})", error.error);
|
||||||
@ -56,7 +62,7 @@ pub fn guild_by_prefix(prefix: &str) -> Result<Option<GuildEntry>, Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn guild_leaderboard() -> Result<Top100Guilds, Error> {
|
pub fn guild_leaderboard() -> Result<Top100Guilds, Error> {
|
||||||
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=statsLeaderboard&type=guild&timeframe=alltime")?;
|
let resp = reqwest::get("https://api.wynncraft.com/public_api.php?action=statsLeaderboard&type=guild&timeframe=alltime")?.error_for_status()?;
|
||||||
|
|
||||||
Ok(serde_json::from_reader(resp)?)
|
Ok(serde_json::from_reader(resp)?)
|
||||||
}
|
}
|
||||||
@ -105,10 +111,10 @@ pub struct Guild {
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Member {
|
pub struct Member {
|
||||||
pub name: Option<String>,
|
pub name: String,
|
||||||
pub rank: Option<Rank>,
|
pub rank: Option<Rank>,
|
||||||
pub contributed: u64,
|
pub contributed: u64,
|
||||||
pub joined: Option<String>,
|
pub joined: String,
|
||||||
pub joined_friendly: String,
|
pub joined_friendly: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user