sponsor_block/client/
mod.rs1#[cfg(feature = "user")]
5mod user;
6#[cfg(feature = "vip")]
7mod vip;
8
9use core::time::Duration;
11
12use reqwest::{Client as ReqwestClient, ClientBuilder as ReqwestClientBuilder};
13
14#[cfg(feature = "user")]
16pub use self::user::*;
17#[cfg(feature = "vip")]
18pub use self::vip::*;
19
20pub type VideoId = String;
23pub type VideoIdSlice = str;
25pub type PublicUserId = String;
28pub type PublicUserIdSlice = str;
30pub type LocalUserId = String;
33pub type LocalUserIdSlice = str;
35pub type SegmentUuid = String;
37pub type SegmentUuidSlice = str;
39
40pub struct Client {
42 http: ReqwestClient,
44
45 user_id: String,
47 base_url: String,
48 #[cfg(feature = "private_searches")]
49 hash_prefix_length: u8,
50 service: String,
51}
52
53impl Client {
54 #[must_use]
56 pub fn new<U: Into<LocalUserId>>(user_id: U) -> Self {
57 ClientBuilder::new(user_id).build()
58 }
59
60 #[must_use]
62 pub fn builder<U: Into<LocalUserId>>(user_id: U) -> ClientBuilder {
63 ClientBuilder::new(user_id)
64 }
65}
66
67pub struct ClientBuilder {
69 user_agent: String,
71
72 user_id: LocalUserId,
74 base_url: String,
75 #[cfg(feature = "private_searches")]
76 hash_prefix_length: u8,
77 service: String,
78 timeout: Option<Duration>,
79}
80
81impl ClientBuilder {
82 pub const DEFAULT_BASE_URL: &'static str = "https://sponsor.ajay.app/api";
84 #[cfg(feature = "private_searches")]
86 pub const DEFAULT_HASH_PREFIX_LENGTH: u8 = 4;
87 pub const DEFAULT_SERVICE: &'static str = "YouTube";
89 pub const DEFAULT_USER_AGENT: &'static str =
91 concat!(env!("CARGO_PKG_NAME"), "-rs/", env!("CARGO_PKG_VERSION"));
92
93 #[must_use]
96 pub fn new<U: Into<LocalUserId>>(user_id: U) -> Self {
97 Self {
98 user_agent: Self::DEFAULT_USER_AGENT.to_owned(),
99 user_id: user_id.into(),
100 base_url: Self::DEFAULT_BASE_URL.to_owned(),
101 #[cfg(feature = "private_searches")]
102 hash_prefix_length: Self::DEFAULT_HASH_PREFIX_LENGTH,
103 service: Self::DEFAULT_SERVICE.to_owned(),
104 timeout: None,
105 }
106 }
107
108 #[must_use]
110 pub fn build(&self) -> Client {
111 let mut http = ReqwestClientBuilder::new().user_agent(self.user_agent.clone());
112 if let Some(timeout) = self.timeout {
113 http = http.timeout(timeout);
114 }
115 Client {
116 http: http.build().expect("unable to build the HTTP client"),
117 user_id: self.user_id.clone(),
118 base_url: self.base_url.clone(),
119 #[cfg(feature = "private_searches")]
120 hash_prefix_length: self.hash_prefix_length,
121 service: self.service.clone(),
122 }
123 }
124
125 pub fn base_url(&mut self, base_url: &str) -> &mut Self {
131 self.base_url = base_url.trim_end_matches('/').to_owned();
132 self
133 }
134
135 #[cfg(feature = "private_searches")]
144 pub fn hash_prefix_length(&mut self, hash_prefix_length: u8) -> &mut Self {
145 assert!(hash_prefix_length >= 4);
146 assert!(hash_prefix_length <= 32);
147 self.hash_prefix_length = hash_prefix_length;
148 self
149 }
150
151 pub fn service(&mut self, service: &str) -> &mut Self {
155 self.service = service.to_owned();
156 self
157 }
158
159 pub fn timeout(&mut self, timeout: Duration) -> &mut Self {
166 self.timeout = Some(timeout);
167 self
168 }
169}