use std::collections::HashMap;
use crate::{
get_authenticated, get_authenticated_paged, get_public, get_public_paged,
model::market::{
CharacterMarketOrder, CorporationMarketOrder, MarketGroup, MarketHistory, MarketOrder,
MarketOrderType, MarketPrice,
},
EsiResult, Paged,
};
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)
}
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)
}
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)
}
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)
}
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)
}
pub async fn get_market_orders(
order_type: &MarketOrderType,
region_id: i32,
type_id: Option<i32>,
page: Option<i32>,
) -> EsiResult<Paged<Vec<MarketOrder>>> {
let mut params: HashMap<&str, String> = vec![("order_type", order_type.to_string())]
.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)
}
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)
}
pub async fn get_market_groups() -> EsiResult<Paged<Vec<i32>>> {
let result = get_public_paged::<Vec<i32>>(&format!("markets/groups"), None).await?;
Ok(result)
}
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)
}
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(&MarketOrderType::All, 10000002, 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());
}
}