use serde::Deserialize;
use crate::{client::SteamClient, errors::SteamError};
const ENDPOINT_GAME_NEWS: &str = "https://api.steampowered.com/ISteamNews/GetNewsForApp/v0002";
#[derive(Debug, Deserialize)]
struct GameNewsResponse {
#[serde(rename(deserialize = "appnews"))]
response: Option<GameNews>,
}
#[derive(Debug, Default, Deserialize)]
pub struct GameNews {
#[serde(rename(deserialize = "newsitems"))]
pub game_news: Vec<News>,
pub count: i16,
}
impl From<GameNewsResponse> for GameNews {
fn from(value: GameNewsResponse) -> Self {
let v = value.response.unwrap_or_default();
Self {
game_news: v.game_news,
count: v.count,
}
}
}
#[derive(Debug, Deserialize)]
pub struct News {
#[serde(rename(deserialize = "gid"))]
pub news_id: String,
pub title: String,
pub url: String,
pub author: String,
pub contents: String,
pub date: i64,
#[serde(rename(deserialize = "feedname"))]
pub feed_name: String,
}
impl SteamClient {
pub fn get_game_news(
&self,
app_id: &str,
news_count: u16,
max_length: u16,
) -> Result<GameNews, SteamError> {
let response = self.get_request(
ENDPOINT_GAME_NEWS,
vec![
("appid", app_id),
("count", &news_count.to_string()),
("maxlength", &max_length.to_string()),
],
)?;
let news = self
.parse_response::<GameNewsResponse, GameNews>(response)
.unwrap();
Ok(news)
}
}