hypixel 0.1.0

Rust wrapper for the Hypixel public API.
Documentation
//! Handler for access to Hypixel skyblock information.
//! 
//! This is probably the biggest reason why the Hypixel API is used.
//! 
//! Although there is a `Client` struct provided for you here, certain
//! functions do not require it.
use crate::api::http;
use crate::objects::{
    SkillsResponse, 
    Skills, 
    ItemsResponse, 
    Item, 
    ElectionResponse, 
    Mayor, 
    Election,
    BingoResponse,
    Bingo,
    ActiveAuctionsResponse,
    Auction,
    EndedAuctionsResponse,
    EndedAuction,
    Bazaar
};
use crate::Error;
use serde_json::Value;

/// Stores the API key to make communication with the skyblock endpoints
/// easier.
/// 
/// Only handles information that is related to Hypixel skyblock and 
/// requires API key authentication.
pub struct Client<'a> {
    key: &'a str
}

impl<'a> Client<'a> {
    /// Constructs a Client struct with the key provided.
    /// 
    /// # Arguments
    /// 
    /// * `key` - Your API key.
    pub fn login(key: &'a str) -> Self {
        Self { key: key }
    }

    /// Returns a list of auctions from the provided auction ID.
    /// 
    /// # Arguments
    /// 
    /// * `auction_uuid` - UUID of the auction.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// let auction = client.auction_by_uuid("auction-id").await?;
    /// // Something...
    /// ```
    pub async fn auction_by_uuid(&self, auction_uuid: &str) -> Result<Vec<Auction>, Error> {
        let res = http::fetch::<ActiveAuctionsResponse>(
            &format!("skyblock/auction?uuid={auction_uuid}&key={}", &self.key)
        ).await?;
    
        Ok(res.auctions)
    } 

    /// Returns a list of auctions made by the player by their UUID.
    /// 
    /// # Arguments
    /// 
    /// * `player_uuid` - UUID of the player. 
    pub async fn auction_by_player(&self, player_uuid: &str) -> Result<Vec<Auction>, Error> {
        let res = http::fetch::<ActiveAuctionsResponse>(
            &format!("skyblock/auction?player={player_uuid}&key={}", &self.key)
        ).await?;
    
        Ok(res.auctions)
    } 
    
    /// Returns a list of auctions made by the player from their profile ID.
    /// 
    /// # Arguments
    /// 
    /// * `profile_Id` - ID of the player's profile.
    pub async fn auction_by_profile(&self, profile_id: &str) -> Result<Vec<Auction>, Error> {
        let res = http::fetch::<ActiveAuctionsResponse>(
            &format!("skyblock/auction?profile={profile_id}&key={}", &self.key)
        ).await?;
    
        Ok(res.auctions)
    }

    /* 
    pub async fn profile_by_uuid(&self, profile: &str) {}

    pub async fn profile_by_player(&self, uuid: &str) {} 

    pub async fn bingo_data_of(&self, uuid: &str) {}
    */
}

/// Returns all skills.
/// 
/// # Example
/// 
/// ```no_run
/// let skills = skyblock::skills().await?;
/// // Something...
/// ```
pub async fn skills() -> Result<Skills, Error> {
    let res = http::fetch::<SkillsResponse>("resources/skyblock/skills").await?;

    Ok(res.skills)
}

/// Returns a list of items and it's information.
/// 
/// # Example
/// 
/// ```no_run
/// let items = skyblock::items().await?;
/// // Something...
/// ```
pub async fn items() -> Result<Vec<Item>, Error> {
    let res = http::fetch::<ItemsResponse>("resources/skyblock/items").await?;

    Ok(res.items)
}

/// Returns the current mayor and their election.
/// 
/// # Example
/// 
/// ```no_run
/// let mayor_name = skyblock::current_mayor().await?.name;
/// // Something...
/// ```
pub async fn current_mayor() -> Result<Mayor, Error> {
    let res = http::fetch::<ElectionResponse>("resources/skyblock/election").await?;

    Ok(res.mayor)
}

/// Returns the ongoing election.
pub async fn current_election() -> Result<Election, Error> {
    let res = http::fetch::<ElectionResponse>("resources/skyblock/election").await?;

    Ok(res.current)
} 

/// Returns a list of active bingo goals.
pub async fn active_bingo() -> Result<Vec<Bingo>, Error> {
    let res = http::fetch::<BingoResponse>("resources/skyblock/bingo").await?;

    Ok(res.goals)
}

/// Returns a list of active auction items.
/// 
/// # Arguments
/// 
/// * `page` - Page of the auction to view.
/// 
/// # Example
/// 
/// ```no_run
/// let auction_items = skyblock::active_auctions(0).await?;
/// // Something...
/// ```
pub async fn active_auctions(page: usize) -> Result<Vec<Auction>, Error> {
    let res = http::fetch::<ActiveAuctionsResponse>(
        &format!("skyblock/auctions?page={page}")
    ).await?;

    Ok(res.auctions)
}

/// Returns a list of recently ended auctions.
pub async fn recently_ended_auctions() -> Result<Vec<EndedAuction>, Error> {
    let res = http::fetch::<EndedAuctionsResponse>("skyblock/auctions_ended").await?;

    Ok(res.auctions)
}

/// Returns all products from the bazaar.
/// 
/// # Example
/// 
/// ```no_run
/// let corrupted_bait = skyblock::bazaar().await?["CORRUPTED_BAIT"];
/// // Something...
/// ```
pub async fn bazaar() -> Result<Value, Error> {
    let res = http::fetch::<Bazaar>("skyblock/bazaar").await?;

    Ok(res.products)
}