1use reqwest::{
2 Method, Response,
3 header::{self, HeaderMap, HeaderValue},
4};
5use serde::{Serialize, de::DeserializeOwned};
6
7use crate::{Error, ratelimit::Ratelimit};
8
9#[derive(Default)]
10pub struct Cookie(String);
11
12impl std::fmt::Display for Cookie {
13 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14 write!(f, "{}", self.0)
15 }
16}
17
18impl From<&str> for Cookie {
19 fn from(value: &str) -> Self {
20 Self(format!(".ROBLOSECURITY={}", value))
21 }
22}
23
24#[derive(Default, Debug)]
25pub struct ClientRequestor {
26 pub(crate) client: reqwest::Client,
27 pub(crate) default_headers: HeaderMap,
28 pub(crate) ratelimit: Option<Ratelimit>,
29}
30
31#[derive(Default, Debug)]
32pub struct Client {
33 pub requestor: ClientRequestor,
34}
35
36impl Client {
37 pub fn from_cookie(cookie: Cookie) -> Self {
38 let client = reqwest::Client::new();
39 let mut default_headers = HeaderMap::new();
40
41 default_headers.insert(
42 header::USER_AGENT,
43 HeaderValue::from_str("Roblox/WinInet").unwrap(),
44 );
45
46 default_headers.insert(
47 header::COOKIE,
48 HeaderValue::from_str(&cookie.to_string()).unwrap(),
49 );
50
51 default_headers.append(
53 header::COOKIE,
54 HeaderValue::from_str("RBXEventTrackerV2=&browserid=2").unwrap(),
55 );
56
57 Client {
58 requestor: ClientRequestor {
59 client,
60 default_headers,
61 ratelimit: None,
62 },
63 }
64 }
65
66 pub async fn ensure_token(&mut self) -> Result<(), Error> {
67 self.requestor.ensure_token().await
68 }
69
70 pub async fn ratelimits(&self) -> Option<Ratelimit> {
71 self.requestor.ratelimits().await
72 }
73
74 }
78
79pub(crate) struct ResponseWrapped(Response);
80impl ResponseWrapped {
81 pub(crate) async fn json<T: DeserializeOwned>(self) -> Result<T, Error> {
82 Ok(self.0.json::<T>().await?)
83 }
84
85 pub(crate) async fn bytes(self) -> Result<Vec<u8>, Error> {
86 let bytes = self.0.bytes().await;
87 match bytes {
88 Ok(bytes) => Ok(bytes.to_vec()),
89 Err(error) => Err(Error::ReqwestError(error)),
90 }
91 }
92}
93
94impl ClientRequestor {
95 pub(crate) async fn parse_json<T: DeserializeOwned>(
96 &self,
97 response: Response,
98 ) -> Result<T, Error> {
99 Ok(response.json::<T>().await?)
100 }
101
102 pub(crate) async fn request<'a, R: Serialize>(
103 &mut self,
104 method: Method,
105 url: &str,
106 request: Option<&'a R>,
107 query: Option<&'a [(&'a str, &'a str)]>,
108 headers: Option<HeaderMap>,
109 ) -> Result<ResponseWrapped, Error> {
110 let mut builder = self
113 .client
114 .request(method, url)
115 .headers(headers.unwrap_or(self.default_headers.clone()));
116
117 if let Some(request) = request {
119 builder = builder.json(&request);
120 }
121
122 if let Some(query) = query {
123 builder = builder.query(&query);
124 }
125
126 let response = self.validate_response(builder.send().await).await?;
127 Ok(ResponseWrapped(response))
128 }
129}