pub trait ProxyConfig:
Debug
+ Send
+ Sync {
// Required methods
fn to_requests_dict(&self) -> HashMap<String, String>;
fn as_any(&self) -> &dyn Any;
// Provided methods
fn prevent_keeping_connections_alive(&self) -> bool { ... }
fn retries_when_blocked(&self) -> i32 { ... }
}
Expand description
§ProxyConfig
Trait for defining proxy configurations to route YouTube requests through proxies.
This trait provides methods for configuring how HTTP requests are routed through proxies, which is essential for bypassing geographical restrictions or IP blocks that YouTube might impose.
§Implementing Types
The library provides two built-in implementations:
GenericProxyConfig
: For standard HTTP/HTTPS proxiesWebshareProxyConfig
: For Webshare’s rotating residential proxies
§Custom Implementations
You can implement this trait for your own proxy providers by:
- Creating a struct with your proxy configuration details
- Implementing the required methods to generate proxy URLs
- Pass your custom implementation to
YouTubeTranscriptApi::new
§Example
#[derive(Debug)]
struct MyCustomProxy {
server: String,
port: u16,
username: String,
password: String,
}
impl ProxyConfig for MyCustomProxy {
fn to_requests_dict(&self) -> HashMap<String, String> {
let url = format!(
"http://{}:{}@{}:{}",
self.username, self.password, self.server, self.port
);
let mut map = HashMap::new();
map.insert("http".to_string(), url.clone());
map.insert("https".to_string(), url);
map
}
fn prevent_keeping_connections_alive(&self) -> bool {
false // We want persistent connections
}
fn retries_when_blocked(&self) -> i32 {
3 // Retry up to 3 times if blocked
}
fn as_any(&self) -> &dyn Any {
self
}
}
Required Methods§
Sourcefn to_requests_dict(&self) -> HashMap<String, String>
fn to_requests_dict(&self) -> HashMap<String, String>
Converts the proxy configuration to a reqwest-compatible proxy URL map.
This method should return a HashMap with keys “http” and/or “https” containing the formatted proxy URLs for each protocol.
§Returns
HashMap<String, String>
- Map of protocol names to proxy URLs
§Expected Format
The URL format typically follows: protocol://[username:password@]host:port
Sourcefn as_any(&self) -> &dyn Any
fn as_any(&self) -> &dyn Any
Type conversion for dynamic dispatch and type identification.
This method is used internally to determine the concrete type of the proxy configuration, which is needed for specific error handling and behavior.
§Returns
&dyn Any
- Reference to the concrete type asAny
Provided Methods§
Sourcefn prevent_keeping_connections_alive(&self) -> bool
fn prevent_keeping_connections_alive(&self) -> bool
Controls whether HTTP connections should be closed after each request.
Setting this to true
ensures you get a new connection (and potentially
a new IP address) for each request, which is useful for rotating proxies
to prevent IP-based blocking.
§Returns
bool
-true
to close connections after each request,false
to reuse connections
§Default Implementation
The default implementation returns false
, which keeps connections alive.
Sourcefn retries_when_blocked(&self) -> i32
fn retries_when_blocked(&self) -> i32
Specifies how many retries to attempt when a request is blocked by YouTube.
When using rotating residential proxies, this allows the library to retry the request with different IPs when YouTube blocks a request.
§Returns
i32
- The number of retries to attempt when blocked (0 = no retries)
§Default Implementation
The default implementation returns 0
, meaning no retries.