pub struct Requestor { /* private fields */ }Expand description
Simple HTTP requestor with optional proxy support.
The Requestor provides methods to make HTTP requests with configurable timeout settings and optional proxy support. It handles error conversions, response validation, and timeouts in a consistent way.
§Examples
use gooty_proxy::io::requestor::Requestor;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new requestor with default timeout (30 seconds)
let requestor = Requestor::new()?;
// Make a GET request
let response = requestor.get(
"https://example.com",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
).await?;
println!("Response: {}", response);
Ok(())
}Implementations§
Source§impl Requestor
impl Requestor
Sourcepub fn new() -> Result<Self, RequestorError>
pub fn new() -> Result<Self, RequestorError>
Sourcepub fn with_timeout(timeout_secs: u64) -> Result<Self, RequestorError>
pub fn with_timeout(timeout_secs: u64) -> Result<Self, RequestorError>
Sourcepub async fn get(&self, url: &str, user_agent: &str) -> RequestResult<String>
pub async fn get(&self, url: &str, user_agent: &str) -> RequestResult<String>
Makes a GET request to the specified URL with the provided user agent.
This method makes a direct GET request without using a proxy.
§Arguments
url- The URL to requestuser_agent- The User-Agent header value to use
§Returns
The response body as a String if successful.
§Errors
Returns an error if:
- The request fails to send
- The response has a non-success status code
- The response body cannot be read as text
- The request times out
Sourcepub async fn get_with_proxy(
&self,
url: &str,
user_agent: &str,
proxy: &Proxy,
) -> RequestResult<String>
pub async fn get_with_proxy( &self, url: &str, user_agent: &str, proxy: &Proxy, ) -> RequestResult<String>
Makes a GET request using a proxy.
This method creates a new client configured to use the specified proxy, then makes a GET request through that proxy.
§Arguments
url- The URL to requestuser_agent- The User-Agent header value to useproxy- The proxy to use for the request
§Returns
The response body as a String if successful.
§Errors
Returns an error if:
- The proxy configuration is invalid
- The request fails to send
- The response has a non-success status code
- The response body cannot be read as text
- The request times out
- There’s a proxy connection error
Sourcepub async fn measure_latency(&self, url: &str) -> RequestResult<u128>
pub async fn measure_latency(&self, url: &str) -> RequestResult<u128>
Measures the latency to a URL in milliseconds.
This method makes a lightweight HEAD request to the specified URL and measures how long it takes to get a response.
§Arguments
url- The URL to measure latency to
§Returns
The latency in milliseconds.
§Errors
Returns an error if the request fails to send or times out.