#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]
use crate::api::types::account::{AccountData, AccountInfo, UserData};
use crate::api::types::epic_asset::EpicAsset;
use crate::api::types::friends::Friend;
use crate::api::EpicAPI;
use api::types::asset_info::{AssetInfo, GameToken};
use api::types::asset_manifest::AssetManifest;
use api::types::download_manifest::DownloadManifest;
use api::types::entitlement::Entitlement;
use api::types::library::Library;
use log::{error, info, warn};
pub mod api;
#[derive(Default, Debug, Clone)]
pub struct EpicGames {
egs: EpicAPI,
}
impl EpicGames {
pub fn new() -> Self {
EpicGames {
egs: EpicAPI::new(),
}
}
pub fn is_logged_in(&self) -> bool {
if let Some(exp) = self.egs.user_data.expires_at {
let now = chrono::offset::Utc::now();
let td = exp - now;
if td.num_seconds() > 600 {
return true;
}
}
false
}
pub fn user_details(&self) -> UserData {
self.egs.user_data.clone()
}
pub fn set_user_details(&mut self, user_details: UserData) {
self.egs.user_data.update(user_details);
}
pub async fn auth_code(
&mut self,
exchange_token: Option<String>,
authorization_code: Option<String>,
) -> bool {
self.egs
.start_session(exchange_token, authorization_code)
.await
.unwrap_or(false)
}
pub async fn logout(&mut self) -> bool {
self.egs.invalidate_sesion().await
}
pub async fn login(&mut self) -> bool {
if let Some(exp) = self.egs.user_data.expires_at {
let now = chrono::offset::Utc::now();
let td = exp - now;
if td.num_seconds() > 600 {
info!("Trying to re-use existing login session... ");
match self.egs.resume_session().await {
Ok(b) => {
if b {
info!("Logged in");
return true;
}
return false;
}
Err(e) => {
warn!("{}", e)
}
};
}
}
info!("Logging in...");
if let Some(exp) = self.egs.user_data.refresh_expires_at {
let now = chrono::offset::Utc::now();
let td = exp - now;
if td.num_seconds() > 600 {
match self.egs.start_session(None, None).await {
Ok(b) => {
if b {
info!("Logged in");
return true;
}
return false;
}
Err(e) => {
error!("{}", e)
}
}
}
}
false
}
pub async fn list_assets(
&mut self,
platform: Option<String>,
label: Option<String>,
) -> Vec<EpicAsset> {
match self.egs.assets(platform, label).await {
Ok(b) => b,
Err(_) => Vec::new(),
}
}
pub async fn asset_manifest(
&mut self,
platform: Option<String>,
label: Option<String>,
namespace: Option<String>,
item_id: Option<String>,
app: Option<String>,
) -> Option<AssetManifest> {
match self
.egs
.asset_manifest(platform, label, namespace, item_id, app)
.await
{
Ok(a) => Some(a),
Err(_) => None,
}
}
pub async fn asset_info(&mut self, asset: EpicAsset) -> Option<AssetInfo> {
match self.egs.asset_info(asset.clone()).await {
Ok(mut a) => a.remove(asset.catalog_item_id.as_str()),
Err(_) => None,
}
}
pub async fn account_details(&mut self) -> Option<AccountData> {
match self.egs.account_details().await {
Ok(a) => Some(a),
Err(_) => None,
}
}
pub async fn account_ids_details(&mut self, ids: Vec<String>) -> Option<Vec<AccountInfo>> {
match self.egs.account_ids_details(ids).await {
Ok(a) => Some(a),
Err(_) => None,
}
}
pub async fn account_friends(&mut self, include_pending: bool) -> Option<Vec<Friend>> {
match self.egs.account_friends(include_pending).await {
Ok(a) => Some(a),
Err(_) => None,
}
}
pub async fn game_token(&mut self) -> Option<GameToken> {
match self.egs.game_token().await {
Ok(a) => Some(a),
Err(_) => None,
}
}
pub async fn ownership_token(&mut self, asset: EpicAsset) -> Option<String> {
match self.egs.ownership_token(asset).await {
Ok(a) => Some(a.token),
Err(_) => None,
}
}
pub async fn user_entitlements(&mut self) -> Vec<Entitlement> {
match self.egs.user_entitlements().await {
Ok(a) => a,
Err(_) => Vec::new(),
}
}
pub async fn library_items(&mut self, include_metadata: bool) -> Option<Library> {
match self.egs.library_items(include_metadata).await {
Ok(a) => Some(a),
Err(_) => None,
}
}
pub async fn asset_download_manifests(&self, manifest: AssetManifest) -> Vec<DownloadManifest> {
self.egs.asset_download_manifests(manifest).await
}
}