eversal-esi 0.1.3

Eve Online's ESI API library for Rust and Eversal projects
Documentation
use std::collections::HashMap;

use crate::{
  get_authenticated, get_authenticated_paged, get_public, get_public_paged, EsiResult, Paged,
};

use super::{
  CharacterMarketOrder, CorporationMarketOrder, MarketGroup, MarketHistory, MarketOrder,
  MarketOrderType, MarketPrice,
};

/**
 * Get a list of orders in a character's orders
 * esi: https://esi.evetech.net/latest/characters/{character_id}/orders/
 */
pub async fn get_character_orders(
  access_token: &str,
  character_id: i32,
) -> EsiResult<Vec<CharacterMarketOrder>> {
  let result = get_authenticated::<Vec<CharacterMarketOrder>>(
    access_token,
    &format!("characters/{}/orders", character_id),
    None,
  )
  .await?;
  Ok(result)
}

/**
 * Get a list of historical orders in a character's orders
 * esi: https://esi.evetech.net/latest/characters/{character_id}/orders/history/
 */
pub async fn get_character_orders_history(
  access_token: &str,
  character_id: i32,
  page: Option<i32>,
) -> EsiResult<Paged<Vec<CharacterMarketOrder>>> {
  let result = get_authenticated_paged::<Vec<CharacterMarketOrder>>(
    access_token,
    &format!("characters/{}/orders/history", character_id),
    Some(
      vec![("page", page.unwrap_or(1).to_string())]
        .into_iter()
        .collect(),
    ),
  )
  .await?;
  Ok(result)
}

/**
 * Get a list of orders in a corporation's orders
 * esi: https://esi.evetech.net/latest/corporations/{corporation_id}/orders/
 */
pub async fn get_corporation_orders(
  access_token: &str,
  corporation_id: i32,
  page: Option<i32>,
) -> EsiResult<Paged<Vec<CorporationMarketOrder>>> {
  let result = get_authenticated_paged::<Vec<CorporationMarketOrder>>(
    access_token,
    &format!("characters/{}/orders", corporation_id),
    Some(
      vec![("page", page.unwrap_or(1).to_string())]
        .into_iter()
        .collect(),
    ),
  )
  .await?;
  Ok(result)
}

/**
 * Get a list of historical orders in a corporation's orders
 * esi: https://esi.evetech.net/latest/corporations/{corporation_id}/orders/history/
 */
pub async fn get_corporation_orders_history(
  access_token: &str,
  corporation_id: i32,
  page: Option<i32>,
) -> EsiResult<Paged<Vec<CorporationMarketOrder>>> {
  let result = get_authenticated_paged::<Vec<CorporationMarketOrder>>(
    access_token,
    &format!("characters/{}/orders/history", corporation_id),
    Some(
      vec![("page", page.unwrap_or(1).to_string())]
        .into_iter()
        .collect(),
    ),
  )
  .await?;
  Ok(result)
}

/**
 * Get a list of historical market statistics for a type in a region
 * esi: https://esi.evetech.net/latest/markets/{region_id}/history/
 */
pub async fn get_market_history(region_id: i32, type_id: i32) -> EsiResult<Vec<MarketHistory>> {
  let result = get_public::<Vec<MarketHistory>>(
    &format!("markets/{}/history", region_id),
    Some(vec![("type_id", type_id.to_string())].into_iter().collect()),
  )
  .await?;
  Ok(result)
}

/**
 * Get a list of orders in a region
 * esi: https://esi.evetech.net/latest/markets/{region_id}/orders/
 */
pub async fn get_market_orders(
  region_id: i32,
  order_type: Option<&MarketOrderType>,
  type_id: Option<i32>,
  page: Option<i32>,
) -> EsiResult<Paged<Vec<MarketOrder>>> {
  let order_type = order_type.unwrap_or(&MarketOrderType::All).to_string();
  let mut params: HashMap<&str, String> = vec![("order_type", order_type)].into_iter().collect();
  let page = page.unwrap_or(1);
  params.insert("page", page.to_string());
  if let Some(type_id) = type_id {
    let type_id = type_id.to_string();
    params.insert("type_id", type_id);
  }
  let result =
    get_public_paged::<Vec<MarketOrder>>(&format!("markets/{}/orders", region_id), Some(params))
      .await?;
  Ok(result)
}

/**
 * Get a list of market types for a region
 * esi: https://esi.evetech.net/latest/markets/{region_id}/types/
 */
pub async fn get_market_types(region_id: i32, page: Option<i32>) -> EsiResult<Paged<Vec<i32>>> {
  let page = page.unwrap_or(1);
  let result = get_public_paged::<Vec<i32>>(
    &format!("markets/{}/types", region_id),
    Some(vec![("page", page.to_string())].into_iter().collect()),
  )
  .await?;
  Ok(result)
}

/**
 * Get a list of market groups
 * esi: https://esi.evetech.net/latest/markets/groups/
 */
pub async fn get_market_groups() -> EsiResult<Paged<Vec<i32>>> {
  let result = get_public_paged::<Vec<i32>>(&format!("markets/groups"), None).await?;
  Ok(result)
}

/**
 * Get a list of types in a market group
 * esi: https://esi.evetech.net/latest/markets/groups/{market_group_id}/
 */
pub async fn get_market_group(market_group_id: i32) -> EsiResult<MarketGroup> {
  let result =
    get_public::<MarketGroup>(&format!("markets/groups/{}", market_group_id), None).await?;
  Ok(result)
}

pub async fn get_market_prices() -> EsiResult<Vec<MarketPrice>> {
  let result = get_public::<Vec<MarketPrice>>("markets/prices", None).await?;
  Ok(result)
}

/**
 * Get a list of orders in a structure
 * esi: https://esi.evetech.net/latest/markets/structures/{structure_id}/
 */
pub async fn get_market_structure(
  access_token: &str,
  structure_id: i64,
) -> EsiResult<Vec<MarketOrder>> {
  let result = get_authenticated::<Vec<MarketOrder>>(
    access_token,
    &format!("markets/structures/{}", structure_id),
    None,
  )
  .await?;
  Ok(result)
}

#[cfg(test)]
mod tests {
  use super::*;

  #[tokio::test]
  async fn test_get_market_history() {
    let result = get_market_history(10000002, 34).await;
    assert!(result.is_ok());
  }

  #[tokio::test]
  async fn test_get_market_orders() {
    let result = get_market_orders(10000002, None, None, None).await;
    assert!(result.is_ok());
  }

  #[tokio::test]
  async fn test_get_market_types() {
    let result = get_market_types(10000002, None).await;
    assert!(result.is_ok());
  }

  #[tokio::test]
  async fn test_get_market_groups() {
    let result = get_market_groups().await;
    assert!(result.is_ok());
  }

  #[tokio::test]
  async fn test_get_market_group() {
    let result = get_market_group(2).await;
    assert!(result.is_ok());
  }

  #[tokio::test]
  async fn test_get_market_prices() {
    let result = get_market_prices().await;
    assert!(result.is_ok());
  }
}