Trait ProxyConfig

Source
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 proxies
  • WebshareProxyConfig: For Webshare’s rotating residential proxies

§Custom Implementations

You can implement this trait for your own proxy providers by:

  1. Creating a struct with your proxy configuration details
  2. Implementing the required methods to generate proxy URLs
  3. 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§

Source

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

Source

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 as Any

Provided Methods§

Source

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.

Source

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.

Implementors§