roblox_api/
client.rs

1use reqwest::{
2    Response,
3    header::{self, HeaderMap, HeaderValue},
4};
5
6use crate::Error;
7use serde::de::DeserializeOwned;
8
9#[derive(Default)]
10pub struct Cookie(String);
11impl ToString for Cookie {
12    fn to_string(&self) -> String {
13        self.0.clone()
14    }
15}
16
17impl From<&str> for Cookie {
18    fn from(value: &str) -> Self {
19        Self(format!(".ROBLOSECURITY={}", value))
20    }
21}
22
23#[derive(Default, Debug)]
24pub struct ClientRequestor {
25    pub(crate) client: reqwest::Client,
26    pub(crate) default_headers: HeaderMap,
27}
28
29#[derive(Default, Debug)]
30pub struct Client {
31    pub requestor: ClientRequestor,
32}
33
34impl Client {
35    pub fn from_cookie(cookie: Cookie) -> Self {
36        let client = reqwest::Client::new();
37        let mut default_headers = HeaderMap::new();
38
39        default_headers.insert(
40            header::USER_AGENT,
41            HeaderValue::from_str("Roblox/WinInet").unwrap(),
42        );
43
44        default_headers.insert(
45            header::COOKIE,
46            HeaderValue::from_str(&cookie.to_string()).unwrap(),
47        );
48
49        Client {
50            requestor: ClientRequestor {
51                client,
52                default_headers,
53            },
54        }
55    }
56}
57
58impl ClientRequestor {
59    pub(crate) async fn parse_json<T: DeserializeOwned>(
60        &self,
61        response: Response,
62    ) -> Result<T, Error> {
63        match response.json::<T>().await {
64            Ok(result) => Ok(result),
65            Err(_) => Err(Error::BadResponse),
66        }
67    }
68}