lychee/examples/builder/builder.rs
MichaIng 961f12e58e
Remove cache from collector and remove custom reqwest client pool
* Reqwest comes with its own request pool, so there's no need in adding
another layer of indirection. This also gets rid of a lot of allocs.
* Remove cache from collector
* Improve error handling and documentation
* Add back test for request caching in single file

Signed-off-by: MichaIng <micha@dietpi.com>
Co-authored-by: Matthias <matthias-endler@gmx.net>
2021-10-07 18:07:18 +02:00

47 lines
1.3 KiB
Rust

use http::header::{self, HeaderMap};
use http::StatusCode;
use lychee_lib::{ClientBuilder, Result};
use regex::RegexSet;
use reqwest::Method;
use std::iter::FromIterator;
use std::{collections::HashSet, time::Duration};
#[tokio::main]
#[allow(clippy::trivial_regex)]
async fn main() -> Result<()> {
// Excludes
let excludes = Some(RegexSet::new(&[r"example"]).unwrap());
// Includes take precedence over excludes
let includes = Some(RegexSet::new(&[r"example.org"]).unwrap());
// Set custom request headers
let mut headers = HeaderMap::new();
headers.insert(header::ACCEPT, "text/html".parse().unwrap());
let accepted = Some(HashSet::from_iter(vec![
StatusCode::OK,
StatusCode::NO_CONTENT,
]));
let client = ClientBuilder::builder()
.excludes(excludes)
.includes(includes)
.max_redirects(3u8)
.user_agent("custom useragent")
.allow_insecure(true)
.custom_headers(headers)
.method(Method::HEAD)
.timeout(Duration::from_secs(5))
.schemes(HashSet::from_iter(vec![
"http".to_string(),
"https".to_string(),
]))
.accepted(accepted)
.build()
.client()?;
let response = client.check("https://example.org").await?;
dbg!(&response);
assert!(response.status().is_success());
Ok(())
}