egs_api/api/
mod.rs

1use reqwest::header::HeaderMap;
2use reqwest::{Client, ClientBuilder, RequestBuilder};
3use types::account::UserData;
4use url::Url;
5
6/// Module holding the API types
7pub mod types;
8
9/// Various API Utils
10pub mod utils;
11
12/// Error type
13pub mod error;
14
15/// Fab Methods
16pub mod fab;
17
18///Account methods
19pub mod account;
20
21/// EGS Methods
22pub mod egs;
23/// Session Handling
24pub mod login;
25
26#[derive(Default, Debug, Clone)]
27pub(crate) struct EpicAPI {
28    client: Client,
29    pub(crate) user_data: UserData,
30}
31
32impl EpicAPI {
33    pub fn new() -> Self {
34        let client = EpicAPI::build_client().build().unwrap();
35        EpicAPI {
36            client,
37            user_data: Default::default(),
38        }
39    }
40
41    fn build_client() -> ClientBuilder {
42        let mut headers = HeaderMap::new();
43        headers.insert(
44            "User-Agent",
45            "UELauncher/17.0.1-37584233+++Portal+Release-Live Windows/10.0.19043.1.0.64bit"
46                .parse()
47                .unwrap(),
48        );
49        headers.insert(
50            "X-Epic-Correlation-ID",
51            "UE4-c176f7154c2cda1061cc43ab52598e2b-93AFB486488A22FDF70486BD1D883628-BFCD88F649E997BA203FF69F07CE578C".parse().unwrap()
52        );
53        reqwest::Client::builder()
54            .default_headers(headers)
55            .cookie_store(true)
56    }
57
58    fn authorized_get_client(&self, url: Url) -> RequestBuilder {
59        let client = EpicAPI::build_client().build().unwrap();
60        self.set_authorization_header(client.get(url))
61    }
62
63    fn authorized_post_client(&self, url: Url) -> RequestBuilder {
64        let client = EpicAPI::build_client().build().unwrap();
65        self.set_authorization_header(client.post(url))
66    }
67
68    fn set_authorization_header(&self, rb: RequestBuilder) -> RequestBuilder {
69        rb.header(
70            "Authorization",
71            format!(
72                "{} {}",
73                self.user_data
74                    .token_type
75                    .as_ref()
76                    .unwrap_or(&"bearer".to_string()),
77                self.user_data
78                    .access_token
79                    .as_ref()
80                    .unwrap_or(&"".to_string())
81            ),
82        )
83    }
84
85    
86}