Skip to main content

egs_api/api/
account.rs

1use crate::api::error::EpicAPIError;
2use crate::api::types::account::{AccountData, AccountInfo, ExternalAuth, TokenVerification};
3use crate::api::types::entitlement::Entitlement;
4use crate::api::types::friends::Friend;
5use crate::api::EpicAPI;
6
7impl EpicAPI {
8    /// Fetch account details for the logged-in user.
9    pub async fn account_details(&mut self) -> Result<AccountData, EpicAPIError> {
10        let id = match &self.user_data.account_id {
11            Some(id) => id,
12            None => return Err(EpicAPIError::InvalidParams),
13        };
14        let url = format!(
15            "https://account-public-service-prod03.ol.epicgames.com/account/api/public/account/{}",
16            id
17        );
18        self.authorized_get_json(&url).await
19    }
20
21    /// Fetch display names for a list of account IDs.
22    pub async fn account_ids_details(
23        &mut self,
24        ids: Vec<String>,
25    ) -> Result<Vec<AccountInfo>, EpicAPIError> {
26        if ids.is_empty() {
27            return Err(EpicAPIError::InvalidParams);
28        }
29        let url = format!(
30            "https://account-public-service-prod03.ol.epicgames.com/account/api/public/account?accountId={}",
31            ids.join("&accountId=")
32        );
33        self.authorized_get_json(&url).await
34    }
35
36    /// Fetch the friends list, optionally including pending requests.
37    pub async fn account_friends(
38        &mut self,
39        include_pending: bool,
40    ) -> Result<Vec<Friend>, EpicAPIError> {
41        let id = match &self.user_data.account_id {
42            Some(id) => id,
43            None => return Err(EpicAPIError::InvalidParams),
44        };
45        let url = format!(
46            "https://friends-public-service-prod06.ol.epicgames.com/friends/api/public/friends/{}?includePending={}",
47            id, include_pending
48        );
49        self.authorized_get_json(&url).await
50    }
51
52    /// Fetch all entitlements for the logged-in user, paginating internally.
53    pub async fn user_entitlements(&self) -> Result<Vec<Entitlement>, EpicAPIError> {
54        let id = self
55            .user_data
56            .account_id
57            .as_deref()
58            .ok_or(EpicAPIError::InvalidCredentials)?;
59        let mut all = Vec::new();
60        let mut start: usize = 0;
61        let count: usize = 1000;
62        loop {
63            let url = format!(
64                "https://entitlement-public-service-prod08.ol.epicgames.com/entitlement/api/account/{}/entitlements?start={}&count={}",
65                id, start, count
66            );
67            let batch: Vec<Entitlement> = self.authorized_get_json(&url).await?;
68            let batch_len = batch.len();
69            all.extend(batch);
70            if batch_len < count {
71                break;
72            }
73            start += count;
74        }
75        Ok(all)
76    }
77
78    /// Fetch external auth connections for an account.
79    pub async fn external_auths(
80        &self,
81        account_id: &str,
82    ) -> Result<Vec<ExternalAuth>, EpicAPIError> {
83        let url = format!(
84            "https://account-public-service-prod03.ol.epicgames.com/account/api/public/account/{}/externalAuths",
85            account_id
86        );
87        self.authorized_get_json(&url).await
88    }
89
90    /// Fetch SSO domain list for Epic accounts.
91    pub async fn sso_domains(&self) -> Result<Vec<String>, EpicAPIError> {
92        self.authorized_get_json(
93            "https://account-public-service-prod03.ol.epicgames.com/account/api/epicdomains/ssodomains",
94        )
95        .await
96    }
97
98    /// Verify the current OAuth token and get account info with permissions.
99    pub async fn verify_token(
100        &self,
101        include_perms: bool,
102    ) -> Result<TokenVerification, EpicAPIError> {
103        let url = format!(
104            "https://account-public-service-prod03.ol.epicgames.com/account/api/oauth/verify?includePerms={}",
105            include_perms
106        );
107        self.authorized_get_json(&url).await
108    }
109}