egs-api 0.14.0

Interface to the Epic Games API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use crate::api::EpicAPI;
use crate::api::error::EpicAPIError;
use crate::api::types::download_manifest::DownloadManifest;
use crate::api::types::fab_asset_manifest::DownloadInfo;
use crate::api::types::fab_library::FabLibrary;
use log::{debug, error, warn};
use std::borrow::BorrowMut;
use url::Url;

impl EpicAPI {
    /// Fetch Fab asset manifest with signed distribution points. Returns `FabTimeout` on 403.
    pub async fn fab_asset_manifest(
        &self,
        artifact_id: &str,
        namespace: &str,
        asset_id: &str,
        platform: Option<&str>,
    ) -> Result<Vec<DownloadInfo>, EpicAPIError> {
        let url = format!("https://www.fab.com/e/artifacts/{}/manifest", artifact_id);
        let parsed_url = Url::parse(&url).map_err(|_| EpicAPIError::InvalidParams)?;
        match self
            .authorized_post_client(parsed_url)
            .json(&serde_json::json!({
                "item_id": asset_id,
                "namespace": namespace,
                "platform": platform.unwrap_or("Windows"),
            }))
            .send()
            .await
        {
            Ok(response) => {
                if response.status() == reqwest::StatusCode::OK {
                    let text = response.text().await.unwrap_or_default();
                    match serde_json::from_str::<
                        crate::api::types::fab_asset_manifest::FabAssetManifest,
                    >(&text)
                    {
                        Ok(manifest) => Ok(manifest.download_info),
                        Err(e) => {
                            error!("{:?}", e);
                            debug!("{}", text);
                            Err(EpicAPIError::DeserializationError(format!("{}", e)))
                        }
                    }
                } else if response.status() == reqwest::StatusCode::FORBIDDEN {
                    Err(EpicAPIError::FabTimeout)
                } else {
                    debug!("{:?}", response.headers());
                    let status = response.status();
                    let body = response.text().await.unwrap_or_default();
                    warn!("{} result: {}", status, body);
                    Err(EpicAPIError::HttpError { status, body })
                }
            }
            Err(e) => {
                error!("{:?}", e);
                Err(EpicAPIError::NetworkError(e))
            }
        }
    }

    /// Download and parse a Fab manifest from a distribution point.
    pub async fn fab_download_manifest(
        &self,
        download_info: DownloadInfo,
        distribution_point_url: &str,
    ) -> Result<DownloadManifest, EpicAPIError> {
        match download_info.get_distribution_point_by_base_url(distribution_point_url) {
            None => {
                error!("Distribution point not found");
                Err(EpicAPIError::InvalidParams)
            }
            Some(point) => {
                if point.signature_expiration < time::OffsetDateTime::now_utc() {
                    error!("Expired signature");
                    Err(EpicAPIError::InvalidParams)
                } else {
                    let data = self.get_bytes(&point.manifest_url).await?;
                    match DownloadManifest::parse(data) {
                        None => {
                            error!("Unable to parse the Download Manifest");
                            Err(EpicAPIError::DeserializationError(
                                "Unable to parse the Download Manifest".to_string(),
                            ))
                        }
                        Some(mut man) => {
                            man.set_custom_field("SourceURL", distribution_point_url);
                            Ok(man)
                        }
                    }
                }
            }
        }
    }

    /// Fetch all Fab library items, paginating internally.
    pub async fn fab_library_items(
        &mut self,
        account_id: String,
    ) -> Result<FabLibrary, EpicAPIError> {
        let mut library = FabLibrary::default();

        loop {
            let url = match &library.cursors.next {
                None => {
                    format!(
                        "https://www.fab.com/e/accounts/{}/ue/library?count=100",
                        account_id
                    )
                }
                Some(c) => {
                    format!(
                        "https://www.fab.com/e/accounts/{}/ue/library?cursor={}&count=100",
                        account_id, c
                    )
                }
            };

            match self.authorized_get_json::<FabLibrary>(&url).await {
                Ok(mut api_library) => {
                    library.cursors.next = api_library.cursors.next;
                    library.results.append(api_library.results.borrow_mut());
                }
                Err(e) => {
                    error!("{:?}", e);
                    library.cursors.next = None;
                }
            }
            if library.cursors.next.is_none() {
                break;
            }
        }

        Ok(library)
    }

    /// Fetch download info for a specific file within a Fab listing.
    pub async fn fab_file_download_info(
        &self,
        listing_id: &str,
        format_id: &str,
        file_id: &str,
    ) -> Result<DownloadInfo, EpicAPIError> {
        let url = format!(
            "https://www.fab.com/p/egl/listings/{}/asset-formats/{}/files/{}/download-info",
            listing_id, format_id, file_id
        );
        self.authorized_get_json(&url).await
    }

    /// Search Fab listings. Public endpoint — no auth required.
    ///
    /// Use `FabSearchParams` to specify filters, sorting, and pagination.
    pub async fn fab_search(
        &self,
        params: &crate::api::types::fab_search::FabSearchParams,
    ) -> Result<crate::api::types::fab_search::FabSearchResults, EpicAPIError> {
        let mut url = "https://www.fab.com/i/listings/search?".to_string();
        let mut query_parts = Vec::new();

        if let Some(ref q) = params.q {
            query_parts.push(format!("q={}", q));
        }
        if let Some(ref channels) = params.channels {
            query_parts.push(format!("channels={}", channels));
        }
        if let Some(ref listing_types) = params.listing_types {
            query_parts.push(format!("listing_types={}", listing_types));
        }
        if let Some(ref categories) = params.categories {
            query_parts.push(format!("categories={}", categories));
        }
        if let Some(ref sort_by) = params.sort_by {
            query_parts.push(format!("sort_by={}", sort_by));
        }
        if let Some(count) = params.count {
            query_parts.push(format!("count={}", count));
        }
        if let Some(ref cursor) = params.cursor {
            query_parts.push(format!("cursor={}", cursor));
        }
        if let Some(ref aggregate_on) = params.aggregate_on {
            query_parts.push(format!("aggregate_on={}", aggregate_on));
        }
        if let Some(ref in_filter) = params.in_filter {
            query_parts.push(format!("in={}", in_filter));
        }
        if let Some(is_discounted) = params.is_discounted
            && is_discounted
        {
            query_parts.push("is_discounted=true".to_string());
        }
        if let Some(is_free) = params.is_free
            && is_free
        {
            query_parts.push("is_free=1".to_string());
        }
        if let Some(pct) = params.min_discount_percentage {
            query_parts.push(format!("min_discount_percentage={}", pct));
        }
        if let Some(ref seller) = params.seller {
            query_parts.push(format!("seller={}", seller));
        }

        url.push_str(&query_parts.join("&"));
        self.get_json(&url).await
    }

    /// Get full listing detail. Public endpoint — no auth required.
    pub async fn fab_listing(
        &self,
        uid: &str,
    ) -> Result<crate::api::types::fab_search::FabListingDetail, EpicAPIError> {
        let url = format!("https://www.fab.com/i/listings/{}", uid);
        self.get_json(&url).await
    }

    /// Get UE-specific format details for a listing. Public endpoint.
    pub async fn fab_listing_ue_formats(
        &self,
        uid: &str,
    ) -> Result<Vec<crate::api::types::fab_search::FabListingUeFormat>, EpicAPIError> {
        let url = format!(
            "https://www.fab.com/i/listings/{}/asset-formats/unreal-engine",
            uid
        );
        self.get_json(&url).await
    }

    /// Get user's listing state (ownership, wishlist, review). Requires Fab session.
    pub async fn fab_listing_state(
        &self,
        uid: &str,
    ) -> Result<crate::api::types::fab_search::FabListingState, EpicAPIError> {
        let url = format!("https://www.fab.com/i/users/me/listings-states/{}", uid);
        self.authorized_get_json(&url).await
    }

    /// Bulk check listing states for multiple IDs. Requires Fab session.
    pub async fn fab_listing_states_bulk(
        &self,
        listing_ids: &[&str],
    ) -> Result<Vec<crate::api::types::fab_search::FabListingState>, EpicAPIError> {
        let ids = listing_ids.join(",");
        let url = format!(
            "https://www.fab.com/i/users/me/listings-states?listing_ids={}",
            ids
        );
        self.authorized_get_json(&url).await
    }

    /// Bulk fetch pricing for multiple offer IDs. Public endpoint.
    pub async fn fab_bulk_prices(
        &self,
        offer_ids: &[&str],
    ) -> Result<crate::api::types::fab_search::FabBulkPricesResponse, EpicAPIError> {
        let ids = offer_ids
            .iter()
            .map(|id| format!("offer_ids={}", id))
            .collect::<Vec<_>>()
            .join("&");
        let url = format!("https://www.fab.com/i/listings/prices-infos?{}", ids);
        self.get_json(&url).await
    }

    /// Get listing ownership info. Requires Fab session.
    pub async fn fab_listing_ownership(
        &self,
        uid: &str,
    ) -> Result<crate::api::types::fab_search::FabOwnership, EpicAPIError> {
        let url = format!("https://www.fab.com/i/listings/{}/ownership", uid);
        self.authorized_get_json(&url).await
    }

    /// Get pricing for a specific listing. Public endpoint.
    pub async fn fab_listing_prices(
        &self,
        uid: &str,
    ) -> Result<Vec<crate::api::types::fab_search::FabPriceInfo>, EpicAPIError> {
        let url = format!("https://www.fab.com/i/listings/{}/prices-infos", uid);
        self.get_json(&url).await
    }

    /// Get reviews for a listing. Public endpoint.
    pub async fn fab_listing_reviews(
        &self,
        uid: &str,
        sort_by: Option<&str>,
        cursor: Option<&str>,
    ) -> Result<crate::api::types::fab_search::FabReviewsResponse, EpicAPIError> {
        let mut query_parts = Vec::new();
        if let Some(sort) = sort_by {
            query_parts.push(format!("sort_by={}", sort));
        }
        if let Some(c) = cursor {
            query_parts.push(format!("cursor={}", c));
        }
        let url = if query_parts.is_empty() {
            format!("https://www.fab.com/i/store/listings/{}/reviews", uid)
        } else {
            format!(
                "https://www.fab.com/i/store/listings/{}/reviews?{}",
                uid,
                query_parts.join("&")
            )
        };
        self.get_json(&url).await
    }

    /// Fetch available license types. Public endpoint.
    pub async fn fab_licenses(
        &self,
    ) -> Result<Vec<crate::api::types::fab_taxonomy::FabLicenseType>, EpicAPIError> {
        self.get_json("https://www.fab.com/i/taxonomy/licenses")
            .await
    }

    /// Fetch asset format groups. Public endpoint.
    pub async fn fab_format_groups(
        &self,
    ) -> Result<Vec<crate::api::types::fab_taxonomy::FabFormatGroup>, EpicAPIError> {
        self.get_json("https://www.fab.com/i/taxonomy/asset-format-groups")
            .await
    }

    /// Fetch tag groups with nested tags. Public endpoint.
    pub async fn fab_tag_groups(
        &self,
    ) -> Result<Vec<crate::api::types::fab_taxonomy::FabTagGroup>, EpicAPIError> {
        let wrapper: crate::api::types::fab_taxonomy::FabResultsWrapper<
            crate::api::types::fab_taxonomy::FabTagGroup,
        > = self.get_json("https://www.fab.com/i/tags/groups").await?;
        Ok(wrapper.results)
    }

    /// Fetch available UE versions. Public endpoint.
    pub async fn fab_ue_versions(&self) -> Result<Vec<String>, EpicAPIError> {
        self.get_json("https://www.fab.com/i/unreal-engine/versions")
            .await
    }

    /// Fetch channel info by slug. Public endpoint.
    pub async fn fab_channel(
        &self,
        slug: &str,
    ) -> Result<crate::api::types::fab_taxonomy::FabChannel, EpicAPIError> {
        let url = format!("https://www.fab.com/i/channels/{}", slug);
        self.get_json(&url).await
    }

    /// Search library entitlements with filters and aggregations.
    /// Uses the browser-path Fab API. Requires Fab session cookies for full results.
    pub async fn fab_library_entitlements(
        &self,
        params: &crate::api::types::fab_entitlement::FabEntitlementSearchParams,
    ) -> Result<crate::api::types::fab_entitlement::FabEntitlementResults, EpicAPIError> {
        let mut query_parts = Vec::new();
        if let Some(ref sort_by) = params.sort_by {
            query_parts.push(format!("sort_by={}", sort_by));
        }
        if let Some(ref cursor) = params.cursor {
            query_parts.push(format!("cursor={}", cursor));
        }
        if let Some(ref listing_types) = params.listing_types {
            query_parts.push(format!("listing_types={}", listing_types));
        }
        if let Some(ref categories) = params.categories {
            query_parts.push(format!("categories={}", categories));
        }
        if let Some(ref tags) = params.tags {
            query_parts.push(format!("tags={}", tags));
        }
        if let Some(ref licenses) = params.licenses {
            query_parts.push(format!("licenses={}", licenses));
        }
        if let Some(ref asset_formats) = params.asset_formats {
            query_parts.push(format!("asset_formats={}", asset_formats));
        }
        if let Some(ref source) = params.source {
            query_parts.push(format!("source={}", source));
        }
        if let Some(ref aggregate_on) = params.aggregate_on {
            query_parts.push(format!("aggregate_on={}", aggregate_on));
        }
        if let Some(count) = params.count {
            query_parts.push(format!("count={}", count));
        }
        if let Some(ref added_since) = params.added_since {
            query_parts.push(format!("added_since={}", added_since));
        }

        let url = if query_parts.is_empty() {
            "https://www.fab.com/i/library/entitlements/search".to_string()
        } else {
            format!(
                "https://www.fab.com/i/library/entitlements/search?{}",
                query_parts.join("&")
            )
        };
        self.authorized_get_json(&url).await
    }

    /// Initialize Fab CSRF token. Sets `fab_csrftoken` cookie on the client.
    pub async fn fab_csrf(&self) -> Result<(), EpicAPIError> {
        let parsed_url = url::Url::parse("https://www.fab.com/i/csrf")
            .map_err(|_| EpicAPIError::InvalidParams)?;
        let response = Self::send(self.client.get(parsed_url)).await?;
        if response.status().is_success() {
            Ok(())
        } else {
            Err(Self::error_response(response).await)
        }
    }

    /// Fetch Fab user context (country, currency, feature flags). Works with just CSRF token.
    pub async fn fab_user_context(
        &self,
    ) -> Result<crate::api::types::fab_search::FabUserContext, EpicAPIError> {
        self.get_json("https://www.fab.com/i/users/context").await
    }

    /// Add a free listing to the user's library. Returns `Ok(())` on success (HTTP 204).
    pub async fn fab_add_to_library(&self, listing_uid: &str) -> Result<(), EpicAPIError> {
        let url = format!(
            "https://www.fab.com/i/listings/{}/add-to-library",
            listing_uid
        );
        let parsed_url = Url::parse(&url).map_err(|_| EpicAPIError::InvalidParams)?;
        let response = self
            .authorized_post_client(parsed_url)
            .send()
            .await
            .map_err(|e| {
                error!("{:?}", e);
                EpicAPIError::NetworkError(e)
            })?;
        if response.status() == reqwest::StatusCode::NO_CONTENT
            || response.status() == reqwest::StatusCode::OK
        {
            Ok(())
        } else {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            warn!("{} result: {}", status, body);
            Err(EpicAPIError::HttpError { status, body })
        }
    }

    /// Fetch all available asset formats for a listing (UE, Unity, FBX, Blender, etc.).
    pub async fn fab_listing_formats(
        &self,
        listing_uid: &str,
    ) -> Result<Vec<crate::api::types::fab_search::FabListingFormat>, EpicAPIError> {
        let url = format!(
            "https://www.fab.com/i/listings/{}/asset-formats",
            listing_uid
        );
        self.authorized_get_json(&url).await
    }
}