hypixel 0.1.0

Rust wrapper for the Hypixel public API.
Documentation
use serde::de::DeserializeOwned;
use super::error::{HypixelError, HypixelError::*};

const API_BASE: &str = "https://api.hypixel.net/";

pub async fn fetch<T: DeserializeOwned>(path: &str) -> Result<T, HypixelError> {
    let endpoint = &format!("{API_BASE }{path}");

    let res = reqwest::get(endpoint)
        .await.map_err(Reqwest)?;
    
    if res.status() != 200 {
        let status = res.status().as_u16();
        let err: serde_json::Value = serde_json::from_str(&res.text().await.map_err(Reqwest)?)
            .map_err(Json)?;
        
        return Err(FailedResponse(status, err["cause"].to_string()));
    }

    let body: T = res.json().await.map_err(Reqwest)?;

    Ok(body)
}