egs-api 0.14.0

Interface to the Epic Games API
Documentation
use crate::api::EpicAPI;
use crate::api::error::EpicAPIError;
use crate::api::types::account::{AccountData, AccountInfo, ExternalAuth, TokenVerification};
use crate::api::types::entitlement::Entitlement;
use crate::api::types::friends::Friend;

impl EpicAPI {
    /// Fetch account details for the logged-in user.
    pub async fn account_details(&mut self) -> Result<AccountData, EpicAPIError> {
        let id = match &self.user_data.account_id {
            Some(id) => id,
            None => return Err(EpicAPIError::InvalidParams),
        };
        let url = format!(
            "https://account-public-service-prod03.ol.epicgames.com/account/api/public/account/{}",
            id
        );
        self.authorized_get_json(&url).await
    }

    /// Fetch display names for a list of account IDs.
    pub async fn account_ids_details(
        &mut self,
        ids: Vec<String>,
    ) -> Result<Vec<AccountInfo>, EpicAPIError> {
        if ids.is_empty() {
            return Err(EpicAPIError::InvalidParams);
        }
        let url = format!(
            "https://account-public-service-prod03.ol.epicgames.com/account/api/public/account?accountId={}",
            ids.join("&accountId=")
        );
        self.authorized_get_json(&url).await
    }

    /// Fetch the friends list, optionally including pending requests.
    pub async fn account_friends(
        &mut self,
        include_pending: bool,
    ) -> Result<Vec<Friend>, EpicAPIError> {
        let id = match &self.user_data.account_id {
            Some(id) => id,
            None => return Err(EpicAPIError::InvalidParams),
        };
        let url = format!(
            "https://friends-public-service-prod06.ol.epicgames.com/friends/api/public/friends/{}?includePending={}",
            id, include_pending
        );
        self.authorized_get_json(&url).await
    }

    /// Fetch all entitlements for the logged-in user, paginating internally.
    pub async fn user_entitlements(&self) -> Result<Vec<Entitlement>, EpicAPIError> {
        let id = self
            .user_data
            .account_id
            .as_deref()
            .ok_or(EpicAPIError::InvalidCredentials)?;
        let mut all = Vec::new();
        let mut start: usize = 0;
        let count: usize = 1000;
        loop {
            let url = format!(
                "https://entitlement-public-service-prod08.ol.epicgames.com/entitlement/api/account/{}/entitlements?start={}&count={}",
                id, start, count
            );
            let batch: Vec<Entitlement> = self.authorized_get_json(&url).await?;
            let batch_len = batch.len();
            all.extend(batch);
            if batch_len < count {
                break;
            }
            start += count;
        }
        Ok(all)
    }

    /// Fetch external auth connections for an account.
    pub async fn external_auths(
        &self,
        account_id: &str,
    ) -> Result<Vec<ExternalAuth>, EpicAPIError> {
        let url = format!(
            "https://account-public-service-prod03.ol.epicgames.com/account/api/public/account/{}/externalAuths",
            account_id
        );
        self.authorized_get_json(&url).await
    }

    /// Fetch SSO domain list for Epic accounts.
    pub async fn sso_domains(&self) -> Result<Vec<String>, EpicAPIError> {
        self.authorized_get_json(
            "https://account-public-service-prod03.ol.epicgames.com/account/api/epicdomains/ssodomains",
        )
        .await
    }

    /// Verify the current OAuth token and get account info with permissions.
    pub async fn verify_token(
        &self,
        include_perms: bool,
    ) -> Result<TokenVerification, EpicAPIError> {
        let url = format!(
            "https://account-public-service-prod03.ol.epicgames.com/account/api/oauth/verify?includePerms={}",
            include_perms
        );
        self.authorized_get_json(&url).await
    }
}