-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathchain.rs
34 lines (27 loc) · 1001 Bytes
/
chain.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use async_trait::async_trait;
use lychee_lib::{chain::RequestChain, ChainResult, ClientBuilder, Handler, Result, Status};
use reqwest::{Method, Request};
#[derive(Debug)]
struct MyHandler {}
#[async_trait]
impl Handler<Request, Status> for MyHandler {
async fn handle(&mut self, mut request: Request) -> ChainResult<Request, Status> {
// Handle special case of some website (fictional example)
if request.url().domain() == Some("wikipedia.org") && request.url().path() == "/home" {
request.url_mut().set_path("/foo-bar");
*request.method_mut() = Method::PUT;
}
ChainResult::Next(request)
}
}
#[tokio::main]
async fn main() -> Result<()> {
let chain = RequestChain::new(vec![Box::new(MyHandler {})]);
let client = ClientBuilder::builder()
.plugin_request_chain(chain)
.build()
.client()?;
let result = client.check("https://wikipedia.org/home").await;
println!("{:?}", result);
Ok(())
}